Skip to content

[Bug]: colorbar for matshow taller than plot unless using fignum=0 #22972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
phil-blain opened this issue May 4, 2022 · 3 comments
Closed

[Bug]: colorbar for matshow taller than plot unless using fignum=0 #22972

phil-blain opened this issue May 4, 2022 · 3 comments

Comments

@phil-blain
Copy link

phil-blain commented May 4, 2022

Bug summary

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 added

import matplotlib.pyplot as plt
import numpy as np

# a 2D array with linearly increasing values on the diagonal
a = 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 too

plt.colorbar()
plt.show()

Actual outcome

Figure_1

Expected outcome

Figure_2

Additional information

No response

Operating system

Ubuntu 20.04

Matplotlib Version

3.5.2

Matplotlib Backend

QtAgg

Python version

Python 3.10.4

Jupyter version

none involved

Installation

conda

@jklymak
Copy link
Member

jklymak commented 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

@jklymak jklymak closed this as completed May 4, 2022
@phil-blain phil-blain 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 May 4, 2022
@phil-blain
Copy link
Author

Thanks for the reply.

I don;'t know what your fig=0 etc are doing

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

@anntzer
Copy link
Contributor

anntzer commented 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.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants