forked from kenwaldek/pythonprogramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedding_in_qt50.py
More file actions
136 lines (103 loc) · 4.32 KB
/
embedding_in_qt50.py
File metadata and controls
136 lines (103 loc) · 4.32 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
"""
@author: Diego Castaneda ([email protected])
This script creates a basic GUI
Based on http://matplotlib.org/examples/user_interfaces/embedding_in_qt5.html
"""
from __future__ import unicode_literals
import sys
import os
import matplotlib
# Make sure that we are using QT5
matplotlib.use('Qt5Agg')
from PyQt5 import QtCore, QtWidgets
import numpy as np
import pylab as plt
# Load matplotlib Qt stuff:
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
# Set a name to out program based on its filename:
progname = os.path.basename(sys.argv[0])
class plotCanvas(FigureCanvas):
"""Simple canvas with a sine plot."""
def __init__(self, parent=None, width=5, height=5, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi,facecolor="white")
self.axes = fig.add_subplot(111)
#self.make_contour("/home/castaned/Documents/code_lab/qt/c_data.dat")
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,QtWidgets.QSizePolicy.Expanding,
QtWidgets.QSizePolicy.Expanding)
FigureCanvas.updateGeometry(self)
self.axes.axis('off')
def make_contour(self,fname):
"""
Loads the file at fname and plots its contents. It uses
matplotlib's contourf function to plot X, Y, Z contained in
the file. Each coordinate is expected to be a meshgrid.
"""
data = np.genfromtxt(fname)
l = len(data[:,0])/3
x = data[0:l,:]
y = data[l:2*l,:]
z = data[2*l:3*l,:]
self.axes.contourf(x,y,z, 100, cmap=plt.cm.gnuplot,
vmax=np.max(z), vmin=np.min(z))
self.axes.contourf(x,-y,z, 100, cmap=plt.cm.gnuplot,
vmax=np.max(z), vmin=np.min(z))
self.axes.contourf(-x,-y,z, 100, cmap=plt.cm.gnuplot,
vmax=np.max(z), vmin=np.min(z))
self.axes.contourf(-x,y,z, 100, cmap=plt.cm.gnuplot,
vmax=np.max(z), vmin=np.min(z))
self.draw()
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
"""
Setup the main window with Qt widgets
"""
QtWidgets.QMainWindow.__init__(self)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.setWindowTitle("Plot - countourf")
self.main_widget = QtWidgets.QWidget(self)
# Box will contain the elements we want to include in a vertical
# layout.
box = QtWidgets.QVBoxLayout(self.main_widget)
# Init. the plotCanvas class and add the Matplotlib toolbar:
self.sc = plotCanvas(self.main_widget, width=5, height=5, dpi=100)
mpl_toolbar = NavigationToolbar(self.sc, self.main_widget)
# Add a nice menu bar to open a file to plot and exit options:
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction('&Open', self.filePick,
QtCore.Qt.CTRL + QtCore.Qt.Key_O)
fileMenu.addAction('&Quit', self.fileQuit,
QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
# Add the Qt widgets to the main window
box.addWidget(self.sc)
box.addWidget(mpl_toolbar)
# Set the focus on the main widget:
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
def filePick(self):
"""
Generic code for a file picker. In this case
once a file is selected, the directory of the file is passed
to the make_contour function on the plotCanvas class for
plotting.
"""
curr_path = os.getcwd()
fname = QtWidgets.QFileDialog.getOpenFileName(self,
'Open file', curr_path)
if fname[0]:
self.sc.make_contour(fname[0])
def fileQuit(self):
"""
Exit the application
"""
self.close()
qApp = QtWidgets.QApplication(sys.argv)
aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())