- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用嵌入 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/
我正在尝试编写一个函数来制作绘图并将其自动保存到文件中。 我努力用它来动态地做的技巧[plotname=varname & filename=varname &], 并使其与从循环中调用它兼容。 #
有人可以帮助我如何在绘图条形图中添加“下拉”菜单。 我在以下链接 ( https://plot.ly/python/v3/dropdowns/ ) 上找到了一些信息,但我正在努力调整代码,因此下拉选项
我不确切知道如何表达这一点,但我本质上希望根据其他数据之前的列值将数据分组为 Excel 图的系列。例如: size weight apple 3 35 orange 4
我正在为出版物创建图表并希望它们具有相同的字体大小。 当我用多图创建图形时,字体大小会减小,即使我没有更改tiff() 分辨率或pointsize 参数。我根据最终适合的地 block 数量增加了图形
我用 glm::perspective(80.0f, 4.0f/3.0f, 1.0f, 120.0f);并乘以 glm::mat4 view = glm::lookAt( glm::vec3(
我在 Shiny UI 中有一个情节。如果我更改任何输入参数并且通过 react 性图将会改变。但是让我们考虑以下情况:- Shiny UI 中的绘图可以说股票的日内价格变动。为此,您查询一些实时数据
我对 R 有点陌生。我在以下两个线程中跟踪并实现了结果: http://tolstoy.newcastle.edu.au/R/e17/help/12/03/7984.html http://lukem
我想在 WPF 控件中使用 GDI+ 绘图。 最佳答案 有多种方法可以做到这一点,最简单的方法是锁定您使用 GDI 操作的位图,获取像素缓冲区(从锁定中获取的 BitmapData 中的 Scan0
如何在以下取自其网站的绘图示例中隐藏颜色条? df % layout(title = '2014 Global GDPSource:CIA World Factbook',
我有两列数据,X 和 Y,每个条目在两个向量的小数点后都有 4 位数据。 当我使用 plot(x,y) 绘制简单图时,轴上显示的数据精确到小数点后两位。如何在两个轴上将其更改为小数点后 4 位精度?
我目前正在使用 Canvas 处理 JavaFX-Drawing-Application。在 GraphicsContext 的帮助下,我使用 beginPath() 和 lineTo() 方法绘制线
如果这个问题已经得到解答,但我无法找到我需要的东西,我提前道歉。我想从名为 data1.dat、data2.dat 的文件中绘制一些结果......我设法通过循环导入数据,但我无法使用循环绘制结果。仅
我的 pandas 数据框中有一个功能,可以(可能)具有多个级别。我可以使用以下方法获得独特的级别: reasons = df["Reason"].unique() 我可以通过执行以下操作在单个图表上
我在 Ubuntu 14 和 Windows 7(均为 64 位)中用 Python 绘制结果时遇到问题。作为一个简单的比较,我做了: from tvb.simulator.lab import *
以下代码相当简单 - 它用随机选择的像素填充设计表面 - 没什么特别的(暂时忽略第二种方法中的 XXXXXXX)。 private void PaintBackground() { Rando
我正在尝试制作一个绘制函数图形的 swing 应用程序(现在很简单,例如 x+2)但我在根据屏幕坐标制作我的点的数学坐标时遇到问题。我希望它在我的图表中简单地画一条从 P1(0,1) 到 P2(1,2
编辑 4:问题的新格式 背景:我有一个扩展 JFrame 的类 Window,在 JFrame 中我有一个 Canvas 。我向 Canvas 添加自定义对象。这个对象的唯一目的(为了争论)是在 Ca
我需要为即将到来的锦标赛标记阶梯,但我找不到任何方法来语义标记它。到目前为止我看到的唯一方法是 mark it up as a table ,我想不惜一切代价避免这种情况。 有什么想法吗? 最佳答案
我目前正在为一个小型 uC 项目编写 UI。在计算垂直线的位置时遇到一些问题。这个想法是将红线沿 x 轴移动到矩形的末端。 使用无限旋转编码器递增的值,范围为 0 到 800,增量为 1。矩形的左侧是
我正在尝试绘制光分布图。我想准确地执行此问题的第一步所要求的:Statistical analysis on Bell shaped (Gaussian) curve . 现在我有一组值。我希望数组元
我是一名优秀的程序员,十分优秀!