Skip to content

fig.colorbar() and using bbox='tight' in PDF export mess up figure dimensions #18165

@quantsnus

Description

@quantsnus

Bug report

Bug summary

After upgrading from matplotlib 3.2.2 to 3.3.0 a previously nicely working plot was messed up in it's dimensions location and depending on the figure dimensions parts could be cropped in the output.

I reduced the plotting to an example which should show, that this has something to do with adding a colorbar using fig.colorbar() and exporting to PDF applying the bbox='tight' option. If either of the two is left out the output is as expected.

Code for reproduction

import matplotlib.pyplot as plt
fig, axarr = plt.subplots(2,1, figsize=(8,10))
im0 = axarr[0].imshow([[0,1],[1,0]], aspect='auto')
im1 = axarr[1].imshow([[0,1],[1,0]], aspect='auto')
colorBar=fig.colorbar(im0, ax=axarr)
plt.savefig('test.png', bbox_inches='tight')
plt.savefig('test.pdf', bbox_inches='tight')
plt.savefig('test2.png', bbox_inches='tight')

Actual outcome

test2.png is output significantly cropped
test2

Expected outcome

test.png comes out as expected
test

For completeness also the pdf output, which is as expected:
test.pdf

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.0
  • Matplotlib backend: Qt5Agg
  • Python version: 3.7.8
  • Jupyter version (if applicable): -
  • Other libraries: -

Using conda, installed matplotlib from conda-forge

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions