Skip to content

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

Closed

Description

@phil-blain

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

Activity

  1. jklymak commented on May 4, 2022

    @jklymak
    Member

    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

  2. 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
  3. phil-blain commented on May 4, 2022

    @phil-blain
    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...

  4. anntzer commented on Nov 1, 2023

    @anntzer
    Contributor

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions