def test_dpi_ratio_change():
    """
    Make sure that if _dpi_ratio changes, the figure dpi changes but the
    widget remains the same physical size.
    """

    prop = 'matplotlib.backends.backend_qt5.FigureCanvasQT._dpi_ratio'

    with mock.patch(prop, new_callable=mock.PropertyMock) as p:

        p.return_value = 3

        fig = plt.figure(figsize=(5, 2), dpi=120)
        qt_canvas = fig.canvas
        qt_canvas.show()

        from matplotlib.backends.backend_qt5 import qApp

        # Make sure the mocking worked
        assert qt_canvas._dpi_ratio == 3

        size = qt_canvas.size()

        qt_canvas.manager.show()
        qt_canvas.draw()
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 360
        assert qt_canvas.renderer.width == 1800
        assert qt_canvas.renderer.height == 720

        # The actual widget size and figure physical size don't change
        assert size.width() == 600
        assert size.height() == 240
        assert_equal(qt_canvas.get_width_height(), (600, 240))
        assert_equal(fig.get_size_inches(), (5, 2))

        p.return_value = 2

        assert qt_canvas._dpi_ratio == 2

        qt_canvas.draw()
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 240
        assert qt_canvas.renderer.width == 1200
        assert qt_canvas.renderer.height == 480

        # The actual widget size and figure physical size don't change
        assert size.width() == 600
        assert size.height() == 240
        assert_equal(qt_canvas.get_width_height(), (600, 240))
        assert_equal(fig.get_size_inches(), (5, 2))
示例#2
0
def test_dpi_ratio_change():
    """
    Make sure that if _dpi_ratio changes, the figure dpi changes but the
    widget remains the same physical size.
    """

    prop = 'matplotlib.backends.backend_qt5.FigureCanvasQT._dpi_ratio'

    with mock.patch(prop, new_callable=mock.PropertyMock) as p:

        p.return_value = 3

        fig = plt.figure(figsize=(5, 2), dpi=120)
        qt_canvas = fig.canvas
        qt_canvas.show()

        from matplotlib.backends.backend_qt5 import qApp

        # Make sure the mocking worked
        assert qt_canvas._dpi_ratio == 3

        size = qt_canvas.size()

        qt_canvas.manager.show()
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 360
        assert qt_canvas.renderer.width == 1800
        assert qt_canvas.renderer.height == 720

        # The actual widget size and figure physical size don't change
        assert size.width() == 200
        assert size.height() == 80
        assert_equal(qt_canvas.get_width_height(), (600, 240))
        assert_equal(fig.get_size_inches(), (5, 2))

        p.return_value = 2

        assert qt_canvas._dpi_ratio == 2

        qt_canvas.draw()
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 240
        assert qt_canvas.renderer.width == 1200
        assert qt_canvas.renderer.height == 480

        # The actual widget size and figure physical size don't change
        assert size.width() == 200
        assert size.height() == 80
        assert_equal(qt_canvas.get_width_height(), (600, 240))
        assert_equal(fig.get_size_inches(), (5, 2))
def test_dpi_ratio_change():
    """
    Make sure that if _dpi_ratio changes, the figure dpi changes but the
    widget remains the same physical size.
    """

    prop = 'matplotlib.backends.backend_qt5.FigureCanvasQT._dpi_ratio'

    with mock.patch(prop, new_callable=mock.PropertyMock) as p:

        p.return_value = 3

        fig = plt.figure(figsize=(5, 2), dpi=120)
        qt_canvas = fig.canvas
        qt_canvas.show()

        from matplotlib.backends.backend_qt5 import qApp

        # Make sure the mocking worked
        assert qt_canvas._dpi_ratio == 3

        size = qt_canvas.size()

        qt_canvas.manager.show()
        qt_canvas.draw()
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 360
        assert qt_canvas.renderer.width == 1800
        assert qt_canvas.renderer.height == 720

        # The actual widget size and figure physical size don't change
        assert size.width() == 600
        assert size.height() == 240
        assert qt_canvas.get_width_height() == (600, 240)
        assert (fig.get_size_inches() == (5, 2)).all()

        p.return_value = 2

        assert qt_canvas._dpi_ratio == 2

        qt_canvas.draw()
        qApp.processEvents()
        # this second processEvents is required to fully run the draw.
        # On `update` we notice the DPI has changed and trigger a
        # resize event to refresh, the second processEvents is
        # required to process that and fully update the window sizes.
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 240
        assert qt_canvas.renderer.width == 1200
        assert qt_canvas.renderer.height == 480

        # The actual widget size and figure physical size don't change
        assert size.width() == 600
        assert size.height() == 240
        assert qt_canvas.get_width_height() == (600, 240)
        assert (fig.get_size_inches() == (5, 2)).all()
示例#4
0
def test_dpi_ratio_change():
    """
    Make sure that if _dpi_ratio changes, the figure dpi changes but the
    widget remains the same physical size.
    """

    prop = 'matplotlib.backends.backend_qt5.FigureCanvasQT._dpi_ratio'

    with mock.patch(prop, new_callable=mock.PropertyMock) as p:

        p.return_value = 3

        fig = plt.figure(figsize=(5, 2), dpi=120)
        qt_canvas = fig.canvas
        qt_canvas.show()

        from matplotlib.backends.backend_qt5 import qApp

        # Make sure the mocking worked
        assert qt_canvas._dpi_ratio == 3

        size = qt_canvas.size()

        qt_canvas.manager.show()
        qt_canvas.draw()
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 360
        assert qt_canvas.renderer.width == 1800
        assert qt_canvas.renderer.height == 720

        # The actual widget size and figure physical size don't change
        assert size.width() == 600
        assert size.height() == 240
        assert qt_canvas.get_width_height() == (600, 240)
        assert (fig.get_size_inches() == (5, 2)).all()

        p.return_value = 2

        assert qt_canvas._dpi_ratio == 2

        qt_canvas.draw()
        qApp.processEvents()
        # this second processEvents is required to fully run the draw.
        # On `update` we notice the DPI has changed and trigger a
        # resize event to refresh, the second processEvents is
        # required to process that and fully update the window sizes.
        qApp.processEvents()

        # The DPI and the renderer width/height change
        assert fig.dpi == 240
        assert qt_canvas.renderer.width == 1200
        assert qt_canvas.renderer.height == 480

        # The actual widget size and figure physical size don't change
        assert size.width() == 600
        assert size.height() == 240
        assert qt_canvas.get_width_height() == (600, 240)
        assert (fig.get_size_inches() == (5, 2)).all()
示例#5
0
def test_resize_qt():

    # This test just ensures that the code runs, but doesn't check for now
    # that the behavior is correct.

    pytest.importorskip('PyQt5')

    from PyQt5.QtWidgets import QMainWindow

    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qt5 import FigureManagerQT
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg

    fig = Figure()
    canvas = FigureCanvasQTAgg(fig)
    canvas.manager = FigureManagerQT(canvas, 0)  # noqa
    ax = fig.add_subplot(1, 1, 1)

    canvas.draw = Mock(side_effect=canvas.draw)

    from matplotlib.backends.backend_qt5 import qApp

    window = QMainWindow()
    window.setCentralWidget(canvas)
    window.show()

    x1 = np.random.normal(0, 1, 10000000)
    y1 = np.random.normal(0, 1, 10000000)

    a = ScatterDensityArtist(ax, x1, y1)
    ax.add_artist(a)

    canvas.draw()
    assert not a.stale
    assert canvas.draw.call_count == 1

    window.resize(300, 300)

    # We can't actually check that stale is set to True since it only remains
    # so for a short amount of time, but we can check that draw is called twice
    # (once at the original resolution then once with the updated resolution).
    start = time.time()
    while time.time() - start < 1:
        qApp.processEvents()

    assert canvas.draw.call_count == 3

    assert not a.stale

    start = time.time()
    while time.time() - start < 1:
        qApp.processEvents()

    a.remove()
    qApp.processEvents()
def test_resize_qt():

    # This test just ensures that the code runs, but doesn't check for now
    # that the behavior is correct.

    pytest.importorskip('PyQt5')

    from PyQt5.QtWidgets import QMainWindow

    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qt5 import FigureManagerQT
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg

    fig = Figure()
    canvas = FigureCanvasQTAgg(fig)
    canvas.manager = FigureManagerQT(canvas, 0)  # noqa
    ax = fig.add_subplot(1, 1, 1)

    canvas.draw = Mock(side_effect=canvas.draw)

    from matplotlib.backends.backend_qt5 import qApp

    window = QMainWindow()
    window.setCentralWidget(canvas)
    window.show()

    x1 = np.random.normal(0, 1, 10000000)
    y1 = np.random.normal(0, 1, 10000000)

    a = ScatterDensityArtist(ax, x1, y1)
    ax.add_artist(a)

    canvas.draw()
    assert not a.stale
    assert canvas.draw.call_count == 1

    window.resize(300, 300)

    # We can't actually check that stale is set to True since it only remains
    # so for a short amount of time, but we can check that draw is called twice
    # (once at the original resolution then once with the updated resolution).
    start = time.time()
    while time.time() - start < 1:
        qApp.processEvents()

    assert canvas.draw.call_count == 3

    assert not a.stale

    start = time.time()
    while time.time() - start < 1:
        qApp.processEvents()

    a.remove()
    qApp.processEvents()