先看下效果:
代码:
import sys
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QPushButton
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from PyQt5.QtCore import QTimer
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.init_ui()
def init_ui(self):
self.resize(1000, 500)
self.setWindowTitle("动态数据显示")
# 解决无法显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 解决无法显示负号
plt.rcParams['axes.unicode_minus'] = False
# 设置画布部分
self.fig = plt.figure()
self.canvas = FC(self.fig)
# 设置布局,将组件添加到布局中
self.layout = QVBoxLayout()
self.layout.addWidget(self.canvas)
self.setLayout(self.layout)
# 设置定时器,用于更新
self.timer1 = QTimer(self)
self.timer1.timeout.connect(self.update_plot)
self.timer1.start(100)
self.x = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'] # x轴
self.y = [0,1,2,3,4,5,6,7,8,9,10,11] # y轴
def update_plot(self):
for i in range(len(self.y)-1):
firstNumber = self.y[0]
self.y[i] = self.y[i+1]
self.y[-1] = firstNumber
plt.cla() # 清空画布
plt.plot(self.x, self.y, color='blue', linewidth=1.0, linestyle='-', marker='o')
plt.xlabel("年份")
plt.ylabel("数量")
self.canvas.draw() # 绘制
if __name__ == '__main__':
app = QApplication(sys.argv)
m = MyWidget()
m.show()
sys.exit(app.exec_())
问:如果想让一个界面上同时显示两个图该怎么做呢?
答:可以通过添加子图的方式实现
效果如下:
代码:
import sys
import matplotlib.pyplot as plt
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QPushButton,QHBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FC
from PyQt5.QtCore import QTimer
class MyWidget(QWidget):
def __init__(self):
super(MyWidget, self).__init__()
self.init_ui()
def init_ui(self):
self.resize(1000, 500)
self.setWindowTitle("动态数据显示")
# 解决无法显示中文
plt.rcParams['font.sans-serif'] = ['SimHei']
# 解决无法显示负号
plt.rcParams['axes.unicode_minus'] = False
# 设置画布部分
self.fig = plt.figure()
self.ax1 = self.fig.add_subplot(121)
self.ax2 = self.fig.add_subplot(122)
self.canvas = FC(self.fig)
# self.canvas2 = FC(self.fig2)
# 设置布局,将组件添加到布局中
self.layout = QHBoxLayout()
self.layout.addWidget(self.canvas)
# self.layout.addWidget(self.canvas2)
# 给窗体设置布局
self.setLayout(self.layout)
# 设置定时器,用于更新
self.timer1 = QTimer(self)
self.timer1.timeout.connect(self.update_plot)
self.timer1.start(100)
self.x = ['2010', '2011', '2012', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021'] # x轴
self.y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] # y轴
def update_plot(self):
for i in range(len(self.y) - 1):
firstNumber = self.y[0]
self.y[i] = self.y[i + 1]
self.y[-1] = firstNumber
# 更新折线图
self.ax1.cla() # 清空画布
self.ax1.plot(self.x, self.y, color='blue', linewidth=1.0, linestyle='-', marker='o')
self.ax1.set_xlabel("年份")
self.ax1.set_ylabel("数量")
# 更新条形图
self.ax2.cla()
self.ax2.bar(self.x, self.y, color='red', linewidth=1.0, linestyle='-')
self.ax2.set_xlabel("年份")
self.ax2.set_ylabel("数量")
self.canvas.draw() # 绘制
if __name__ == '__main__':
app = QApplication(sys.argv)
m = MyWidget()
m.show()
sys.exit(app.exec_())
喜欢的麻烦点个赞或收藏哦,谢谢~~