1#!/usr/local/bin/python
2#
3# A simple PLplot/PyQt example
4#
5# Copyright (C) 2016 Hazen Babcock
6#
7# This file is part of PLplot.
8#
9# PLplot is free software; you can redistribute it and/or modify
10# it under the terms of the GNU Library General Public License as published
11# by the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# PLplot is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU Library General Public License for more details.
18#
19# You should have received a copy of the GNU Library General Public License
20# along with PLplot; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22#
23
24import sys
25import numpy
26
27import plplot_python_start
28import plplot
29
30# An indirect side effect of this import statement is a consistent set
31# of Qt4 libraries are loaded (e.g., if those are from some downloaded
32# non-system version in a non-standard location).
33import plplot_pyqt5
34
35# This import statement must go after the one above to insure a
36# consistent set of Qt4 libraries are used for the case of the
37# downloaded, non-standard location of those libraries.
38from PyQt5 import QtCore, QtGui, QtWidgets
39
40
41class QPlot(QtWidgets.QMainWindow):
42    def __init__(self):
43        QtWidgets.QMainWindow.__init__(self, None)
44
45        # UI setup
46        self.curvesAct = QtWidgets.QAction("Curves", self)
47        self.histogramAct = QtWidgets.QAction("Histogram", self)
48        self.interactiveAct = QtWidgets.QAction("Interactive Selection", self)
49        self.plotMenu = self.menuBar().addMenu("Plot")
50        self.plotMenu.addAction(self.curvesAct)
51        self.plotMenu.addAction(self.histogramAct)
52        self.plotMenu.addAction(self.interactiveAct)
53        self.curvesAct.triggered.connect(self.plotCurves)
54        self.histogramAct.triggered.connect(self.plotHistogram)
55        self.interactiveAct.triggered.connect(self.interactive)
56
57        # PLplot setup
58        self.plot = plplot_pyqt5.QtExtWidget(842, 595, self)
59        self.setCentralWidget(self.plot)
60        plplot_pyqt5.plsetqtdev(self.plot)
61        plplot.plsdev("extqt")
62        plplot.plinit()
63
64        self.resize(600,600)
65        plplot.pladv(0)
66
67    def cleanup(self):
68        plplot.plend()
69        plplot_pyqt5.plfreeqtdev()
70
71    def paintEvent(self, event):
72#        plplot.pladv(0)
73#        plplot.plenv(0, 10, 0, 10, 0, 0)
74#        plplot.pllab("X", "Y", "Y vs X")
75        self.plot.show()
76
77    def plotCurves(self):
78        self.plot.clearWidget()
79
80        # 1st plot
81        indexes = numpy.arange(0, 360.1, 1.0)
82        sine = numpy.sin(indexes * 3.14159 / 180.0)
83        cosine = numpy.cos(indexes * 3.14159 / 180.0)
84
85        plplot.pladv(0)
86        plplot.plvpor(0.05, 0.95, 0.05, 0.45)
87        plplot.plwind(0.0, 360.0, -1.2, 1.2)
88
89        plplot.plcol0(2)
90        plplot.plbox("bcnst", 0., 0, "bcnst", 0., 0)
91
92        plplot.plcol0(1)
93        plplot.plwidth(2)
94        plplot.plline(indexes, sine)
95
96        plplot.plcol0(3)
97        plplot.plwidth(1)
98        plplot.pllsty(2)
99        plplot.plline(indexes, cosine)
100        plplot.pllsty(1)
101
102        plplot.plcol0(2)
103
104        plplot.plmtex("t", 1., 0.5, 0.5, "Sines")
105
106        # 2nd plot
107        indexes = numpy.arange(-1.0, 1.0, 0.01)
108        square = indexes * indexes
109        cubic = square * indexes
110
111        plplot.plvpor(0.05, 0.95, 0.55, 0.95)
112        plplot.plwind(-1., 1., -1., 1.)
113
114        plplot.plcol0(2)
115        plplot.plbox("bcnst", 0., 0, "bcnst", 0., 0)
116
117        plplot.plcol0(1)
118        plplot.plwidth(2)
119        plplot.plline(indexes, square)
120
121        plplot.plcol0(3)
122        plplot.plwidth(1)
123        plplot.pllsty(2)
124        plplot.plline(indexes, cubic)
125        plplot.pllsty(1)
126
127        plplot.plcol0(2)
128
129        plplot.plmtex("t", 1., 0.5, 0.5, "Square & Cubic")
130
131        self.update()
132
133    def plotHistogram(self):
134
135        def plfbox(x0, y0):
136            x = numpy.array([x0, x0, x0 + 1.0, x0 + 1.0])
137            y = numpy.array([0.0, y0, y0, 0.0])
138            plplot.plfill(x, y)
139            plplot.plcol0(1)
140            plplot.pllsty(1)
141            plplot.plline(x, y)
142
143        self.plot.clearWidget();
144
145        y0 = numpy.array([5, 15, 12, 24, 28, 30, 20, 8, 12, 3])
146        pos = numpy.array([0.0, 0.25, 0.5, 0.75, 1.0])
147        red = numpy.array([0.0, 0.25, 0.5, 1.0, 1.0])
148        green = numpy.array([1.0, 0.5, 0.5, 0.5, 1.0])
149        blue = numpy.array([1.0, 1.0, 0.5, 0.25, 0.0])
150
151        plplot.pladv(0)
152        plplot.plvsta()
153
154        plplot.plcol0(2)
155
156        plplot.plwind(1980.0, 1990.0, 0.0, 35.0)
157        plplot.plbox("bc", 1.0, 0, "bcnv", 10.0, 0)
158        plplot.plcol0(2)
159        plplot.pllab("Year", "Widget Sales (millions)", "#frPLplot Example 12")
160
161        plplot.plscmap1l(1,pos,red,green,blue)
162
163        for i in range(10):
164            plplot.plcol1(i/9.0)
165            plplot.plpsty(0)
166            plfbox((1980. + i), y0[i])
167            string = "%.0f" % (y0[i])
168            plplot.plptex((1980. + i + .5), (y0[i] + 1.), 1.0, 0.0, .5, string)
169            string = "%d" % (1980 + i)
170            plplot.plmtex("b", 1.0, ((i + 1) * .1 - .05), 0.5, string)
171
172        self.update()
173
174    def interactive(self):
175        [mx, my] = self.plot.captureMousePlotCoords()
176        msg_box = QtWidgets.QMessageBox()
177        msg_box.setText("Selection: (%f, %f)" % (mx, my))
178        msg_box.exec_()
179
180
181app = QtWidgets.QApplication(sys.argv)
182plot = QPlot()
183plot.show()
184app.exec_()
185plot.cleanup()
186
187