FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[Bug]: Setting bbox_inches to a Bbox in fig.savefig resizes colorbar · Issue #22625 · matplotlib/matplotlib · GitHub

Repository navigation

[Bug]: Setting bbox_inches to a Bbox in fig.savefig resizes colorbar #22625

Closed

Description

Bug summary

Setting bbox_inches in fig.savefig to a specified Bbox rather than "tight" resizes the colorbar relative to when bbox_inches is not set—the resulting colorbar is a lot larger than it should be.

Code for reproduction

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

x = np.linspace(0, 2*np.pi, 100)
y = np.linspace(0, 2*np.pi, 100)

X, Y = np.meshgrid(x,y)

fig, ax = plt.subplots(figsize=(4,4))

pc = ax.pcolormesh(x, y, np.sin(X)*np.sin(Y))
fig.colorbar(pc, ax=ax, aspect=40)

# Uncomment the first fig.savefig to get the correct output on the second call
# fig.savefig('nobbox_inches.png')
fig.savefig('bbox_inches.png', bbox_inches=mpl.transforms.Bbox([[0, 0], [4, 4]]))

Actual outcome

Expected outcome

Additional information

Calling fig.savefig without bbox_inches set first and then calling it again with bbox_inches set produces expected outcome.
Calling plt.show() prior to savefig also works.

Operating system

MacOS 12.2.1 (Monterey)

Matplotlib Version

3.5.1

Matplotlib Backend

inline (for interactive), png for plot, also a problem with pdf

Python version

3.9.10

Jupyter version

Jupyter lab 3.2.9

Installation

conda

Activity

  1. jklymak commented on Mar 8, 2022

    Member

    Do you know if this ever worked? We changed some colorbar things recently, but not sure what would have caused this to happen. Thanks

  2. pittwolfe commented on Mar 8, 2022

    Author

    Not sure. This is the first time I've tried to set bbox_inches to anything other than "tight".

  3. added this to the v3.7.2 milestone on Mar 20, 2023
  4. irina-nesterova commented on Sep 1, 2023

    It helps whet the figure is cut on the left and at the top, but doesn'i if it cut the xlabel :(

  5. rcomer commented on Sep 1, 2023

    Member

    @irina-nesterova if you are still having problems with this in Matplotlib v3.7.2, please open a new issue with some code that demonstrates the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL