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

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()

Related

Seaborn mixing up colorbar with axis divider?

Consider this MWE:
import seaborn
import matplotlib
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
a = np.random.randn(int(1e3))
b = np.random.rand(int(1e3))
vmin = -10
vmax = 10
seaborn.histplot(x=a, y=b, cmap='PuBu_r', cbar=True, vmin=vmin, vmax=vmax)
plt.show()
plt.close()
ax = seaborn.histplot(x=a, y=b)
# Add colorbar with help of axis divier.
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap='PuBu_r', norm=norm)
sm.set_array([])
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
plt.colorbar(sm, cax=cax)
plt.show()
plt.close()
Naively, I'd expect both histograms to look alike, with the only difference being that the first histogram should be somewhat smaller than the second one, since the second histogram puts the colorbar with an axis divider.
However, the output is:
To be honest, I cannot explain the difference in the color scheme between the two histograms to myself. Isn't seaborn (or matplotlib) mixing things up here?
EDIT: As was pointed out in the comments, putting vmin and vmax explicitly into the seaborn.histplot() calls fixed the problem. However, when comparing the output of
seaborn.histplot(x=a, y=b, cmap='PuBu_r', cbar=False, vmin=vmin, vmax=vmax)
and
ax = seaborn.histplot(x=a, y=b)
# Add colorbar with help of axis divier.
norm = matplotlib.colors.Normalize(vmin=vmin, vmax=vmax)
sm = plt.cm.ScalarMappable(cmap='PuBu_r', norm=norm)
sm.set_array([])
divider = make_axes_locatable(ax)
cax = divider.append_axes('right', size='5%', pad=0.05)
plt.colorbar(sm, cax=cax)
I noticed that the second image (leaving out the colorbar) does not have the same size as the first image:
Is there any way to enforce this?

How get an axis unsqueezed on a plot?

For plotting a heatmap I did:
f, ax = plt.subplots(1, 1, dpi=dpi)
im0 = ax.imshow(data, cmap='Wistia')
Due to the dimensionality of data with (300K, 20) resulting Y-axis was squeezed.
Maybe squeezing of Y-axis happened because of :
from mpl_toolkits.axes_grid1 import make_axes_locatable
# 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(im0, cax=cax)
How can I get heatmap more look like (with full Y-range [0, 20]):

height of colorbar (matplotlib)

Is it possible to have a colorbar smaller than the height of figure?
I know that we can adjust the size of colorbar with
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="3%", pad=0.05)
pl.colorbar(im, cax=cax)
size only adjust the width of colorbar. but how about the height?
I am looking for a colorbar at upper right, with the half height of the figure.
You can use fig.add_axes and fine-tune the parameters as you wish:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
fig = plt.figure()
ax = plt.subplot(111)
im = ax.imshow(np.arange(100).reshape((10, 10)))
c = plt.colorbar(im, cax = fig.add_axes([0.78, 0.5, 0.03, 0.38]))

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()

matplotlib imshow fixed aspect and vertical colorbar matching master axis height

I need to plot a mesh grid with "temperature map" values, currently I'm using imshow, with a colormap. This is described in the Matplotlib overview, so I modified the example to force custom aspect of the figure:
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)
# 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.savefig("test.png")
But the result is not what I want, the colorbar is higher than the master axis:
Interestingly, when the colormap is horizontal, it is scaled properly:
cax = divider.append_axes("bottom", size="5%", pad=0.05)
plt.colorbar(im, cax=cax, orientation="horizontal")
What happens here is that you applied an aspect of 0.5 to the imshow image. This divides the vertical extend of your image by 2, while the colorbar keeps the original extent. I see 2 solutions
you can manually set the size of the colorbar using:
cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])
...or you can apply an aspect to cax to keep its y-dimension consequent with the image. In your case as you set size to 5%, setting aspect=1 would give you an image with 1/20 of the original vertical-extent. To obtain 1/2 as for the image set aspect to 20*0.5= 10. You could create a variable for aspect, if you want to experiment with changing the aspect on the figure, the colorbar will follow.
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
fig = plt.figure()
ax = plt.gca()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)
# 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, aspect=10)
#cax = fig.add_axes([0.85, 0.3, 0.04, 0.4])
plt.colorbar(im, cax=cax)
plt.show()
You can match the size of the colorbar to the size of the axes containing your data using the following snippet.
def match_colorbar(cb, ax=None):
"""
Match the size of the colorbar with the size of the axes.
Args:
ax: Axes from which the colorbar "stole" space.
cb: Colorbar to match to `ax`.
"""
ax = ax or plt.gca()
bbox = ax.get_position()
cb_bbox = cb.ax.get_position()
if cb.orientation == "vertical":
# Update bottom and height.
left = cb_bbox.xmin
width = cb_bbox.width
bottom = bbox.ymin
height = bbox.height
else:
# Update left and width.
left = bbox.xmin
width = bbox.width
bottom = cb_bbox.ymin
height = cb_bbox.height
pos = [left, bottom, width, height]
cb.ax.set_position(pos)
Here is an example.
fig, ax = plt.subplots()
im = ax.imshow(np.arange(100).reshape((10,10)), aspect=0.5)
cb = fig.colorbar(im)
match_colorbar(cb)

Categories