|
1 | 1 | from __future__ import (absolute_import, division, print_function, |
2 | 2 | unicode_literals) |
3 | 3 |
|
| 4 | +import copy |
| 5 | + |
| 6 | +import matplotlib |
4 | 7 | from matplotlib import pyplot as plt |
5 | 8 | from matplotlib._pylab_helpers import Gcf |
6 | | -import matplotlib |
7 | | -import copy |
| 9 | + |
| 10 | +from numpy.testing import assert_equal |
8 | 11 |
|
9 | 12 | import pytest |
10 | 13 | try: |
@@ -95,3 +98,60 @@ def receive(event): |
95 | 98 |
|
96 | 99 | qt_canvas.mpl_connect('key_press_event', receive) |
97 | 100 | 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