Skip to content

Matplotlib 3.2.1 savefig empty image when fig size matches data size exactly #17542

Closed
@rdgraham

Description

@rdgraham

Bug report

Bug summary

Saving a figure to an image file with the same pixel dimensions as data loaded with imshow does not work in 3.2.1, producing an empty image. If the dimensions are changed by even one pixel, normal behavior returns.

This bug appeared in 3.2.1 and I have confirmed that it does not occur with 3.1.1.

I noticed in the release notes for 3.2.0 that some changes were made to the default image interpolation; perhaps an unhandled condition was introduced here when no interpolation is required. However, I note that changing the interpolation mode had no effect on this bug.

Code for reproduction

import numpy as np
from matplotlib import pylab as plt

x_size, y_size = (100, 100)
fig = plt.figure(frameon=False, dpi=100, figsize=(y_size/100, x_size/100))
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
ax.set_axis_off()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)

data = np.random.rand(100, 100)
ax.imshow(data)
fig.savefig('/tmp/image.png', bbox_inches='tight', pad_inches=0)

# In matplotlib 3.2.1, empty file with size 100x100 pixels output
# if random data shape is changed to something other than 100x100, output
# works correctly.

# In matplotlib 3.1.1, file output is 100x100 with data shown correctly.

Actual outcome
bug_image

Expected outcome

working_image

Produced with matplotlib 3.1.1

Matplotlib version

  • Operating system: Linux version 5.6.13-arch1-1
  • Matplotlib version: 3.2.1
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.8.3
  • Other libraries: numpy 1.18.4

Default system install on Arch linux package python-matplotlib-3.2.1-1-x86_64

Metadata

Metadata

Assignees

No one assigned

    Labels

    Release criticalFor bugs that make the library unusable (segfaults, incorrect plots, etc) and major regressions.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions