You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: colorbar for matshow taller than plot unless using fignum=0 #22972
When using plt.colorbar along with plt.matshow, the colorbar is taller than the plot. If I use plt.matshow(A, fig=0), then it is the same height as the plot.
Code for reproduction
# Basically https://matplotlib.org/stable/gallery/images_contours_and_fields/matshow.html#sphx-glr-gallery-images-contours-and-fields-matshow-py with a colorbar addedimportmatplotlib.pyplotaspltimportnumpyasnp# a 2D array with linearly increasing values on the diagonala=np.diag(range(15))
plt.matshow(a) # this triggers the bug# plt.matshow(a, fignum=None) # this too# plt.matshow(a, fignum=1) # this too# plt.matshow(a, fignum=False) # this triggers the expected outcome# plt.matshow(a, fignum=0) # this tooplt.colorbar()
plt.show()
changed the title [-][Bug]: colorbar for `matshow` taller than plot unless using `fig=0`[/-][+][Bug]: colorbar for `matshow` taller than plot unless using `fignum=0`[/+]on May 4, 2022
For the record here is the description of the fignum argument from the doc:
fignum : None or int or False
If None, create a new figure window with automatic numbering.
If a nonzero integer, draw into the figure with the given number (create it if it does not exist).
If 0, use the current axes (or create one if it does not exist).
Since in my example no figure exist beforehand, according to the doc I would expect fignum=None( the default), fignum=1, fignum=0 and fignum=False to all behave the same...
Note that even fignum=0/False won't work for "wide" matrices, e.g. matshow(rand(3, 8), fignum=0); colorbar() still results in a too-tall colorbar. In general I agree with the comment above that one cannot rely on matshow getting the right colorbar size automatically.
(I also agree that the behavior of matshow wrt. fignum is confusing -- see #27244.)
Activity
jklymak commentedon May 4, 2022
I don;'t know what your fig=0 etc are doing, but the general behaviour is expected that fixed-aspect axes can have colorbars that are too tall (or too wide). Make the aspect ratio of the figure such that the blank space is horizontal instead of vertical. Please see: https://matplotlib.org/3.5.0/gallery/subplots_axes_and_figures/colorbar_placement.html#colorbars-with-fixed-aspect-ratio-axes. I'll close this here, but feel free to discuss at https://discourse.matplotlib.org
[-][Bug]: colorbar for `matshow` taller than plot unless using `fig=0`[/-][+][Bug]: colorbar for `matshow` taller than plot unless using `fignum=0`[/+]phil-blain commentedon May 4, 2022
Thanks for the reply.
For the record here is the description of the
fignumargument from the doc:Since in my example no figure exist beforehand, according to the doc I would expect
fignum=None( the default),fignum=1,fignum=0andfignum=Falseto all behave the same...anntzer commentedon Nov 1, 2023
Note that even fignum=0/False won't work for "wide" matrices, e.g.
matshow(rand(3, 8), fignum=0); colorbar()still results in a too-tall colorbar. In general I agree with the comment above that one cannot rely on matshow getting the right colorbar size automatically.(I also agree that the behavior of matshow wrt. fignum is confusing -- see #27244.)