File: test_matplotlib.py

package info (click to toggle)
python-pyqtgraph 0.13.7-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,072 kB
  • sloc: python: 54,043; makefile: 127; ansic: 40; sh: 2
file content (79 lines) | stat: -rw-r--r-- 2,246 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from importlib.metadata import version

import pytest

import pyqtgraph as pg
from pyqtgraph.exporters import MatplotlibExporter

pytest.importorskip("matplotlib")
import matplotlib

app = pg.mkQApp()

skip_qt6 = pytest.mark.skipif(
    # availability of QtAgg signifies Qt6 support
    pg.Qt.QT_LIB in ["PySide6", "PyQt6"] and "QtAgg" not in matplotlib.rcsetup.interactive_bk,
    reason= (
        "installed version of Matplotlib does not support Qt6, "
        "see https://github.com/matplotlib/matplotlib/pull/19255"
    )
)

# see https://github.com/matplotlib/matplotlib/pull/24172
if (
    pg.Qt.QT_LIB == "PySide6"
    and tuple(map(int, pg.Qt.PySide6.__version__.split("."))) > (6, 4)
    and tuple(map(int, version("matplotlib").split("."))) < (3, 6, 2)
):
    pytest.skip(
        "matplotlib + PySide6 6.4 bug",
        allow_module_level=True
    )


@skip_qt6
def test_MatplotlibExporter():
    plt = pg.PlotWidget()
    plt.show()

    # curve item
    plt.plot([0, 1, 2], [0, 1, 2])
    # scatter item
    plt.plot([0, 1, 2], [1, 2, 3], pen=None, symbolBrush='r')
    # curve + scatter
    plt.plot([0, 1, 2], [2, 3, 4], pen='k', symbolBrush='r')

    exp = MatplotlibExporter(plt.getPlotItem())
    exp.export()

@skip_qt6
def test_MatplotlibExporter_nonplotitem():
    # attempting to export something other than a PlotItem raises an exception
    plt = pg.PlotWidget()
    plt.show()
    plt.plot([0, 1, 2], [2, 3, 4])
    exp = MatplotlibExporter(plt.getPlotItem().getViewBox())
    with pytest.raises(Exception):
        exp.export()

@skip_qt6
@pytest.mark.parametrize('scale', [1e10, 1e-9])
def test_MatplotlibExporter_siscale(scale):
    # coarse test to verify that plot data is scaled before export when
    # autoSIPrefix is in effect (so mpl doesn't add its own multiplier label)
    plt = pg.PlotWidget()
    plt.show()
    plt.plot([0, 1, 2], [(i+1)*scale for i in range(3)])
    # set the label so autoSIPrefix works
    plt.setLabel('left', 'magnitude')
    exp = MatplotlibExporter(plt.getPlotItem())
    exp.export()

    mpw = MatplotlibExporter.windows[-1]
    fig = mpw.getFigure()
    ymin, ymax = fig.axes[0].get_ylim()

    if scale < 1:
        assert ymax > scale
    else:
        assert ymax < scale