8000 fig.set_dpi() does not set the dpi correctly · Issue #11227 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
fig.set_dpi() does not set the dpi correctly #11227
Closed
@ImportanceOfBeingErnest

Description

Bug report

Bug summary

Changing the figure's dpi via fig.set_dpi() does not have the desired effect. There are strange things happening when saving or showing the figure as detailed below.

Bug 1 - saving

In the following a figure with dpi=200 should be saved in all 3 cases.

import matplotlib.pyplot as plt

get_img_size = lambda path: print(plt.imread(path).shape)

fig1, ax1 = plt.subplots(dpi=200)
ax1.plot([1,2])
fig1.savefig("fig1.png")
get_img_size("fig1.png")

fig2, ax2 = plt.subplots(dpi=100)
ax2.plot([1,2])
fig2.savefig("fig2.png", dpi=200)
get_img_size("fig2.png")

fig3, ax3 = plt.subplots(dpi=100)
ax3.plot([1,2])
fig3.set_dpi(200)
fig3.savefig("fig3.png", dpi="figure")
get_img_size("fig3.png")

Qt5Agg

The outcome using the Qt5Agg backend of this is

(960, 1280, 4)
(960, 1280, 4)
(480, 640, 4)

i.e. in the first two cases we get the expected 200 dpi figure. In the last case, where the figure dpi is set via fig.set_dpi it seems to be ignored and the output image has a dpi of 100.

TkAgg

Using the TkAgg backend, the saved figures are as expected ((960, 1280, 4) in all cases.)

Bug 2 - showing

In the following we would expect to get two identical figures shown, both with dpi=100 and 640x480 pixels large.

plt.close("all")

fig1, ax1 = plt.subplots(num="expected", dpi=100)
ax1.plot([1,2])

fig3, ax3 = plt.subplots(num="buggy", dpi=50)
ax3.plot([1,2])
fig3.set_dpi(100)
print(fig3.get_dpi())
fig3.savefig("fig3a.png", dpi="figure")
print(fig3.get_dpi())
plt.show()
print(fig3.get_dpi())

Qt5Agg

The output (with Qt5Agg) is the following:

100
100
50

image

The printed dpi is correctly 100 before showing the figure. After showing, it prints 50. The shown figures have both the correct size, but the figure where dpi is set via fig.set_dpi has narrower linewidth and smaller fontsize. It looks exaclty like a figure that would have been expected for dpi=50, just with the wrong pixel size.

TkAgg

Using TkAgg backend, the output is correctly printed as 100 in all cases, but the shown figures differ in size; the left (expected) figure is 640x480 pixels, while the right figure is 640x445 pixels.

image

Matplotlib version

  • Operating system: Windows 8.1
  • Matplotlib version: master (2.2.2.post1088.dev0+g9ec4b95d6)
  • Matplotlib backend: Qt5Agg & TkAgg (see text)
  • Python version: 3.6.4

Running the same with matplotlib 2.0.2 (all other versions the same) I get
Qt5Agg, saving: (960, 1280, 4), (960, 1280, 4), (480, 640, 4) - same bug as above with master.
Qt5Agg, showing 100, 100, 100 - printed dpi are all the same, shown figures are the same.
TkAgg, saving: (960, 1280, 4) in all cases. - same expected behaviour as above.
TkAgg, showing 100, 100, 100 - printed dpi are all the same, shown figures differ in same manner as above.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0