<p>我建议您创建一个单独的python文件<code>ui.py</code>,在该文件中设置<code>PyQt5</code>窗口,其中包含小部件、布局等。为此,我使用Qt Designer。<br/>
您应将工作目录组织为:</p>
<pre><code>├── workind_directory
├── main.py
└── ui.py
</code></pre>
<p><code>ui.py</code>文件的起点可以是:</p>
<pre><code>from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(600, 600)
MainWindow.setMinimumSize(QtCore.QSize(600, 600))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.FigureLayout = QtWidgets.QVBoxLayout()
self.FigureLayout.setObjectName("FigureLayout")
self.gridLayout.addLayout(self.FigureLayout, 0, 0, 1, 1)
self.ToolbarLayout = QtWidgets.QVBoxLayout()
self.ToolbarLayout.setObjectName("ToolbarLayout")
self.gridLayout.addLayout(self.ToolbarLayout, 1, 0, 1, 1)
self.plotButton = QtWidgets.QPushButton(self.centralwidget)
self.plotButton.setObjectName("plotButton")
self.gridLayout.addWidget(self.plotButton, 2, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.plotButton.setText("PLOT")
</code></pre>
<p>其中有绘图布局、工具栏布局(如果需要)和更新绘图按钮。<br/>
然后您可以设置<code>main.py</code>文件:</p>
<pre><code>from PyQt5.QtWidgets import QApplication, QMainWindow
import sys
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
import pandas as pd
import numpy as np
import ui
import matplotlib.pyplot as plt
import metpy.calc as mpcalc
from metpy.cbook import get_test_data
from metpy.plots import SkewT
from metpy.units import units
class Window(QMainWindow, ui.Ui_MainWindow):
def __init__(self, parent = None):
super(Window, self).__init__(parent)
self.setupUi(self)
self.plotButton.clicked.connect(self.plotting)
self.Figure = plt.figure()
self.Canvas = FigureCanvas(self.Figure)
self.FigureLayout.addWidget(self.Canvas)
self.Toolbar = NavigationToolbar(self.Canvas, self)
self.ToolbarLayout.addWidget(self.Toolbar)
def plotting(self):
###########################################
# Upper air data can be obtained using the siphon package, but for this example we will use
# some of MetPy's sample data.
col_names = ['pressure', 'height', 'temperature', 'dewpoint', 'direction', 'speed']
df = pd.read_fwf(get_test_data('jan20_sounding.txt', as_file_obj = False),
skiprows = 5, usecols = [0, 1, 2, 3, 6, 7], names = col_names)
# Drop any rows with all NaN values for T, Td, winds
df = df.dropna(subset = ('temperature', 'dewpoint', 'direction', 'speed'
), how = 'all').reset_index(drop = True)
###########################################
# We will pull the data out of the example dataset into individual variables and
# assign units.
p = df['pressure'].values*units.hPa
T = df['temperature'].values*units.degC
Td = df['dewpoint'].values*units.degC
wind_speed = df['speed'].values*units.knots
wind_dir = df['direction'].values*units.degrees
u, v = mpcalc.wind_components(wind_speed, wind_dir)
###########################################
skew = SkewT(fig = self.Figure)
# Plot the data using normal plotting functions, in this case using
# log scaling in Y, as dictated by the typical meteorological plot
skew.plot(p, T, 'r')
skew.plot(p, Td, 'g')
# skew.plot_barbs(p, u, v)
# Set spacing interval Every 50 mb from 1000 to 100 mb
my_interval = np.arange(100, 1000, 50) * units('mbar')
# Get indexes of values closest to defined interval
ix = mpcalc.resample_nn_1d(p, my_interval)
# Plot only values nearest to defined interval values
skew.plot_barbs(p[ix], u[ix], v[ix])
# Add the relevant special lines
skew.plot_dry_adiabats()
skew.plot_moist_adiabats()
skew.plot_mixing_lines()
skew.ax.set_ylim(1000, 100)
plt.draw()
App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec_())
</code></pre>
<p>这个<code>main.py</code>文件继承了<code>ui.py</code>的布局。请注意<code>__init__()</code>方法,在该方法中,图形、画布和工具栏被创建并放置在各自的布局中。<br/>
然后是<code>plotting()</code>方法,您可以在其中实际绘制所需的绘图</p>
<p><a href="https://i.stack.imgur.com/pFRL1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pFRL1.png" alt="enter image description here"/></a></p>