Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 31408af

Browse files
committed
Added a regression test to make sure the renderer size and figure dpi change when _dpi_ratio changes
1 parent 7f4a73a commit 31408af

1 file changed

Lines changed: 62 additions & 2 deletions

File tree

lib/matplotlib/tests/test_backend_qt5.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from __future__ import (absolute_import, division, print_function,
22
unicode_literals)
33

4+
import copy
5+
6+
import matplotlib
47
from matplotlib import pyplot as plt
58
from matplotlib._pylab_helpers import Gcf
6-
import matplotlib
7-
import copy
9+
10+
from numpy.testing import assert_equal
811

912
import pytest
1013
try:
@@ -95,3 +98,60 @@ def receive(event):
9598

9699
qt_canvas.mpl_connect('key_press_event', receive)
97100
qt_canvas.keyPressEvent(event)
101+
102+
103+
@pytest.mark.backend('Qt5Agg')
104+
def test_dpi_ratio_change():
105+
"""
106+
Make sure that if _dpi_ratio changes, the figure dpi changes but the
107+
widget remains the same physical size.
108+
"""
109+
110+
prop = 'matplotlib.backends.backend_qt5.FigureCanvasQT._dpi_ratio'
111+
112+
with mock.patch(prop, new_callable=mock.PropertyMock) as p:
113+
114+
p.return_value = 3
115+
116+
fig = plt.figure(figsize=(5, 2), dpi=120)
117+
qt_canvas = fig.canvas
118+
qt_canvas.show()
119+
120+
from matplotlib.backends.backend_qt5 import qApp
121+
122+
# Make sure the mocking worked
123+
assert qt_canvas._dpi_ratio == 3
124+
125+
size = qt_canvas.size()
126+
127+
qt_canvas.manager.show()
128+
qApp.processEvents()
129+
130+
# The DPI and the renderer width/height change
131+
assert fig.dpi == 360
132+
assert qt_canvas.renderer.width == 1800
133+
assert qt_canvas.renderer.height == 720
134+
135+
# The actual widget size and figure physical size don't change
136+
assert size.width() == 200
137+
assert size.height() == 80
138+
assert_equal(qt_canvas.get_width_height(), (600, 240))
139+
assert_equal(fig.get_size_inches(), (5, 2))
140+
141+
p.return_value = 2
142+
143+
assert qt_canvas._dpi_ratio == 2
144+
145+
qt_canvas.draw()
146+
qApp.processEvents()
147+
148+
# The DPI and the renderer width/height change
149+
assert fig.dpi == 240
150+
assert qt_canvas.renderer.width == 1200
151+
assert qt_canvas.renderer.height == 480
152+
153+
# The actual widget size and figure physical size don't change
154+
assert size.width() == 200
155+
assert size.height() == 80
156+
assert_equal(qt_canvas.get_width_height(), (600, 240))
157+
assert_equal(fig.get_size_inches(), (5, 2))

0 commit comments

Comments
 (0)