5

I'm trying to get a colorbar for an image, which is supposed to have the same height as the image. There are many solutions suggested here, but none of them work for an image which has an aspect ratio smaller than 1.

If you use the accepted answer from the linked question like this...

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.4375)

# 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("asdf.png", bbox_inches = "tight")

... (Note the aspect in imshow call!), I get this:

enter image description here

Leaving aspect out, it works just fine, but for my data, I need to set the aspect ratio, as the step size for the x-axis is much larger than for the y-axis.

Other solutions, like plt.colorbar(im,fraction=0.046, pad=0.04) or adding a seperate axis don't work either and produce similiar results.

How do I get the colorbar to have the same height in this case?

2

2 Answers 2

8

I finally found a solution here:

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

...

fig, ax = plt.subplots(1, 1)
im = ax.imshow(data, aspect = aspectRatio)
axins = inset_axes(ax, width = "5%", height = "100%", loc = 'lower left',
                   bbox_to_anchor = (1.02, 0., 1, 1), bbox_transform = ax.transAxes,
                   borderpad = 0)
fig.colorbar(im, cax = axins)

... where data is your array of values and 1.02 is the padding between the figure and the colorbar.

This creates colorbars with perfect height, regardless of the aspect ratio. No fiddling with magic numbers or anything of that sort.

0
0

After a bit of exploration, this works:

plt.figure()
ax = plt.gca()
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)

ax.set_aspect(0.4375)
cax.set_aspect(8.75)

plt.savefig("asdf.png", bbox_inches = "tight")

Adding the .set_aspect() works. You may need to tweak the parameters. The result is:

plt

For more information, see this.

2
  • 1
    The ax.set_aspect doesn't seem to do anything and the cax.set_aspect value really is just guess work, so not much more than setting the height manually. In my real use-case for this, I have to use a completely different value. I'm looking for an automatic way to do this. Commented Jul 28, 2020 at 8:16
  • The ax.set_aspect() was added instead of the aspect argument in .imshow()
    – Parth Shah
    Commented Jul 28, 2020 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.