Heatmap colorbar issue using Matplotlib - python

I am plotting a heatmap using the array T which carries the same element. But the colorbar seems to show a range of values. How can I adjust it so that it shows only one value i.e. 0.01109?
import numpy as np
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
T=np.array([[0.01109, 0.01109, 0.01109],
[0.01109, 0.01109, 0.01109],
[0.01109, 0.01109, 0.01109]])
fig, ax = plt.subplots()
im = ax.imshow(T)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
ax.set_title('\u03C3' ' ' "(N/m)")

Related

Aligning colormap consistently with the plot

I am trying to align the matplotlib plot with its colorbar. However, when there is a tick on the top of the colormap, the figure itself shrinks a little bit:
Is there a way to equalize this distance (blue arrows) consistently?
For generating the plot, I am using following code:
import matplotlib as mpl
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, ax = plt.subplots(1, 1, figsize=(8, 8))
ax.plot(...)
divider = make_axes_locatable(plt.gca())
cax = divider.append_axes('right', '5%', pad='3%')
sm = plt.cm.ScalarMappable(cmap=plt.get_cmap('viridis'),
norm=mpl.colors.Normalize(vmin=0, vmax=60))
sm.set_array([])
fig.colorbar(sm, cax=cax)
plt.tight_layout()
plt.savefig('pic.png', dpi=500)

Colorbar adjustment in heatmap using matplotlib

I am generating a heatmap based on array T. However, there is one value (1e-9) which is much lower than the rest (ignoring NaN). How do I adjust the colorbar so that I can see the minor changes in the remaining values of the array and also including 1e-9?
import numpy as np
from numpy import NaN
import matplotlib
from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt
T=np.array([[6.19314835e+02, 6.19229656e+02, 6.19220233e+02],
[6.14626547e+02, 6.18217141e+02, 6.19029892e+02],
[1.00000000e-09, NaN, NaN]])
fig, ax = plt.subplots()
im = ax.imshow(T)
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
You can use vmin and vmax to set a range for the color map. The extreme low values can be indicated via a 'lower' color in the color map together with extend='min' in the colorbar.
import numpy as np
import matplotlib.pyplot as plt
T = np.array([[6.19314835e+02, 6.19229656e+02, 6.19220233e+02],
[6.14626547e+02, 6.18217141e+02, 6.19029892e+02],
[1.00000000e-09, np.NaN, np.NaN]])
cmap = plt.get_cmap('viridis').copy()
cmap.set_under('red')
vmin = np.nanmin(T[T>1e-8])
vmax = np.nanmax(T)
fig, ax = plt.subplots()
im = ax.imshow(T, cmap=cmap, vmin=vmin, vmax=vmax)
plt.colorbar(im, ax=ax,extend='min')
plt.tight_layout()
plt.show()

How to invert the lengend from Max to Min [duplicate]

This question already has answers here:
Change the labels of a colorbar from increasing to decreasing values
(3 answers)
Closed 2 years ago.
This is an example in the web https://matplotlib.org/gallery/axes_grid1/simple_colorbar.html#sphx-glr-gallery-axes-grid1-simple-colorbar-py
I want to invert the legend make it look like this:
That means the max in the bottom and min in the top in the legend.
making the colorbar "mirrored"
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
plt.show()
You can access the ax of the colorbar and then call invert_yaxis(). This will invert both the colors and the tick labels of the colorbar.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(im, cax=cax)
cax.invert_yaxis()
plt.show()
PS: If the ax of the colorbar isn't provided, you can access it as
cbar = plt.colorbar(im)
cbar.ax.invert_yaxis()

matplotlib colorbar and histogram with shared axis

I would like to display a 2D np.array with imshow and the respective colorbar which should share its axis with a histogram of the np.array. Here is an attempt, however, without shared axes.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
fig, ax = plt.subplots(figsize=(7,10))
data = np.random.normal(0, 0.2, size=(100,100))
cax = ax.imshow(data, interpolation='nearest', cmap=cm.jet)
divider = make_axes_locatable(plt.gca())
axBar = divider.append_axes("bottom", '5%', pad='7%')
axHist = divider.append_axes("bottom", '30%', pad='7%')
cbar = plt.colorbar(cax, cax=axBar, orientation='horizontal')
axHist.hist(np.ndarray.flatten(data), bins=50)
plt.show()
I tried to use the sharex argument in axHist with axHist = divider.append_axes("bottom", '30%', pad='7%', sharex=axBar) but this somehow shifts the histogram data:
Besides the shared axis x, how could one modify the histogram to take the same colors as the colormap, similar to here?
You may color every patch of histogram by bin value without sharex:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib.colors import Normalize
fig, ax = plt.subplots(figsize=(7,10))
data = np.random.normal(0, 0.2, size=(100,100))
cax = ax.imshow(data, interpolation='nearest', cmap=cm.jet)
divider = make_axes_locatable(plt.gca())
axBar = divider.append_axes("bottom", '5%', pad='7%')
axHist = divider.append_axes("bottom", '30%', pad='7%')
cbar = plt.colorbar(cax, cax=axBar, orientation='horizontal')
# get hist data
N, bins, patches = axHist.hist(np.ndarray.flatten(data), bins=50)
norm = Normalize(bins.min(), bins.max())
# set a color for every bar (patch) according
# to bin value from normalized min-max interval
for bin, patch in zip(bins, patches):
color = cm.jet(norm(bin))
patch.set_facecolor(color)
plt.show()
For more information look for manual page: https://matplotlib.org/xkcd/examples/pylab_examples/hist_colormapped.html

How to adjust size of two subplots, one with colorbar and another without, in pyplot ?

Consider this example
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
plt.subplot(121)
img = plt.imshow([np.arange(0,1,.1)],aspect="auto")
ax = plt.gca()
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="3%", pad=0.5)
plt.colorbar(img, cax=cax, orientation='horizontal')
plt.subplot(122)
plt.plot(range(2))
plt.show()
I want to make these two figures (plot region without colorbar) of the same size.
The size is automatically adjusted if the colorbar is plotted vertically or if two rows are used (211, 212) instead of two columns.
One can basically do the same for the second subplot as for the first, i.e. create a divider and append an axes with identical parameters, just that in this case, we don't want a colorbar in the axes, but instead simply turn the axis off.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
ax = plt.subplot(121)
img = ax.imshow([np.arange(0,1,.1)],aspect="auto")
divider = make_axes_locatable(ax)
cax = divider.append_axes("bottom", size="3%", pad=0.5)
plt.colorbar(img, cax=cax, orientation='horizontal')
ax2 = plt.subplot(122)
ax2.plot(range(2))
divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("bottom", size="3%", pad=0.5)
cax2.axis('off')
plt.show()
You can now do this without recourse to an extra toolkit by using constrained_layout:
import numpy as np
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2, constrained_layout=True)
ax = axs[0]
img = ax.imshow([np.arange(0,1,.1)],aspect="auto")
fig.colorbar(img, ax=ax, orientation='horizontal')
axs[1].plot(range(2))
plt.show()

Categories