gpt4 book ai didi

python - 更新和切换绘图

转载 作者:太空宇宙 更新时间:2023-11-03 20:42:07 25 4
gpt4 key购买 nike

我正在尝试使用嵌入 QT 小部件中的 matplotlibs 来更新多个绘图。现在我可以在窗口中更新一个绘图。但是,当我尝试通过单击按钮切换到另一个图时,程序卡住了。

这是一个测试脚本,我用来了解如何使用编程工具集成到更大的程序中。我修改了这个问题的代码:How to embed matplotlib in pyqt - for Dummies

我已经被这个问题困扰了一段时间了。我知道我错过了一些非常简单的事情。


import random
import sys
from PyQt4 import QtGui, QtCore

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure

class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)

# a figure instance to plot on
self.figure1 = Figure()
self.figure2 = Figure()
self.current = "fig1"
# this is the Canvas Widget that displays the `figure`
# it takes the `figure` instance as a parameter to __init__
self.canvas = FigureCanvas(self.figure1)
self.ax1 = self.figure1.add_subplot(111)
self.ax2 = self.figure2.add_subplot(111)
self.line1, = self.ax1.plot([], [], 'r', lw=2)
self.line2, = self.ax2.plot([], [], 'b', lw=2)
# this is the Navigation widget
# it takes the Canvas widget and a parent
self.toolbar = NavigationToolbar(self.canvas, self)

# Just some button connected to `plot` method
self.button = QtGui.QPushButton('Plot')
self.button.clicked.connect(self.plot)

# set the layout
layout = QtGui.QVBoxLayout()
layout.addWidget(self.toolbar)
layout.addWidget(self.canvas)
layout.addWidget(self.button)
self.setLayout(layout)
self.update()


def update(self):
datax = [random.random() for i in range(10)]
datay = [random.random() for i in range(10)]

self.line1.set_xdata(datax)
self.line1.set_ydata(datay)
self.ax1.relim()
self.ax1.autoscale_view()


self.line2.set_xdata(datax)
self.line2.set_ydata(datay)
self.ax2.relim()
self.ax2.autoscale_view()
self.canvas.draw()
QtCore.QTimer.singleShot(1, self.update)




def plot(self):

if self.current == "fig1":
self.canvas = FigureCanvas(self.figure2)
self.current = "fig2"
elif self.current == "fig2":
self.canvas = FigureCanvas(self.figure1)
self.current = "fig1"


if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)

main = Window()
main.show()

sys.exit(app.exec_())


当我单击此按钮时,它应该开始绘制另一个图。我没有收到任何错误消息。

最佳答案

你改变 Canvas 的逻辑:

def plot(self):
if self.current == "fig1":
self.canvas = FigureCanvas(self.figure2)
self.current = "fig2"
elif self.current == "fig2":
self.canvas = FigureCanvas(self.figure1)
self.current = "fig1"

与下面的逻辑类似:

class Foo:
def __init__(self):
self.m_a = 0

def setA(self, a):
self.m_a = a

def printA(self):
print(self.m_a)


class Bar:
def __init__(self):
self.a = 5
self.foo = Foo()
self.foo.setA(self.a)

def change(self):
self.a = 6


if __name__ == "__main__":
bar = Bar()
bar.foo.printA()
bar.change()
bar.foo.printA()

如果执行代码,您将得到以下结果:

5
5

正如您所看到的,变量的更改并不意味着其他类会收到通知,因为变量的名称是昵称,重要的是内存,在您的情况下,显示的 Canvas 占用的内存位置与按下按钮时创建的 Canvas ,与初始 Canvas 关联的 line1、line2、ax1 和 ax2 也会发生同样的情况。

<小时/>

另一方面,在 GUI 中不建议删除和创建小部件,而是最好隐藏一个小部件并显示占用相同空间的另一个小部件,为此我们可以使用 QStackedWidget 或 QStackedLayout。

import sys
import random
from PyQt4 import QtGui, QtCore

from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import (
NavigationToolbar2QT as NavigationToolbar,
)
from matplotlib.figure import Figure


class Window(QtGui.QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)

self.figure1 = Figure()
self.canvas1 = FigureCanvas(self.figure1)
self.ax1 = self.figure1.add_subplot(111)
self.line1, = self.ax1.plot([], [], "r", lw=2)
self.toolbar1 = NavigationToolbar(self.canvas1, self)

self.figure2 = Figure()
self.canvas2 = FigureCanvas(self.figure2)
self.ax2 = self.figure2.add_subplot(111)
self.line2, = self.ax2.plot([], [], "b", lw=2)
self.toolbar2 = NavigationToolbar(self.canvas2, self)

self.m_stacked_layout = QtGui.QStackedLayout()

for canvas, toolbar in (
(self.canvas1, self.toolbar1),
(self.canvas2, self.toolbar2),
):
widget = QtGui.QWidget()
lay = QtGui.QVBoxLayout(widget)
lay.addWidget(toolbar)
lay.addWidget(canvas)
self.m_stacked_layout.addWidget(widget)

self.button = QtGui.QPushButton("Plot", clicked=self.onClicled)

layout = QtGui.QVBoxLayout(self)
layout.addLayout(self.m_stacked_layout)
layout.addWidget(self.button)

timer = QtCore.QTimer(self, timeout=self.update_plot, interval=1)
timer.start()
self.update_plot()

@QtCore.pyqtSlot()
def update_plot(self):
datax = [random.random() for i in range(10)]
datay = [random.random() for i in range(10)]

self.line1.set_xdata(datax)
self.line1.set_ydata(datay)
self.ax1.relim()
self.ax1.autoscale_view()

self.line2.set_xdata(datax)
self.line2.set_ydata(datay)
self.ax2.relim()
self.ax2.autoscale_view()
self.canvas1.draw()
self.canvas2.draw()

@QtCore.pyqtSlot()
def onClicled(self):
ix = self.m_stacked_layout.currentIndex()
self.m_stacked_layout.setCurrentIndex(0 if ix == 1 else 1)


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)

main = Window()
main.show()

sys.exit(app.exec_())

关于python - 更新和切换绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56799811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com