File: legendExample.py

package info (click to toggle)
taurus-pyqtgraph 0.9.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,284 kB
  • sloc: python: 5,234; makefile: 82
file content (79 lines) | stat: -rw-r--r-- 2,230 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env python

#############################################################################
##
# This file is part of Taurus
##
# http://taurus-scada.org
##
# Copyright 2011 CELLS / ALBA Synchrotron, Bellaterra, Spain
##
# Taurus is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
##
# Taurus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
##
# You should have received a copy of the GNU Lesser General Public License
# along with Taurus.  If not, see <http://www.gnu.org/licenses/>.
##
#############################################################################


"""
Example on how to use a separate widget (LegendItem) for the legend of a plot.
(Pure Qt)
"""

import pyqtgraph as pg
from pyqtgraph.Qt import QtGui
import sys


if __name__ == "__main__":

    app = QtGui.QApplication([])

    # instantiate the main plot
    plt = pg.PlotWidget()
    plt.setWindowTitle("pyqtgraph example: PLOT")

    # instantiate a graphics view to contain the legend
    gv = QtGui.QGraphicsView(QtGui.QGraphicsScene())
    gv.setWindowTitle("pyqtgraph example: Legend")
    gv.setBackgroundBrush(QtGui.QBrush(QtGui.QColor("black")))

    legend = pg.LegendItem(size=(100, 60), offset=(70, 30))
    gv.scene().addItem(legend)

    # create 3 curves
    c1 = plt.plot(
        [1, 3, 2, 4],
        pen="r",
        symbol="o",
        symbolPen="r",
        symbolBrush=0.5,
        name="red plot",
    )
    c2 = plt.plot(
        [2, 1, 4, 3],
        pen="g",
        fillLevel=0,
        fillBrush=(255, 255, 255, 30),
        name="green plot",
    )
    c3 = plt.plot(list(range(7)), pen="c", fillLevel=0)

    # add the **named** curves to the legend
    for dataitem in plt.getPlotItem().listDataItems():
        if dataitem.name():
            legend.addItem(dataitem, dataitem.name())

    plt.show()
    gv.show()

    sys.exit(app.exec_())