- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试修改以下示例以实现实时绘图。
Embedding a matplotlib figure inside a WxPython panel
我正在尝试读取来自 Arduino 的串行数据并绘制/更新收集的数据。问题是该图在 wx 应用程序之前出现,我需要关闭该图才能看到 wx 应用程序。
我认为问题与以下几行有关,但我不知道为什么。
self.figure = plt.figure(figsize=(20,20))
self.ax = plt.axes(xlim=(0, 1000), ylim=(0, 5000))
脚本如下。
import wx
from matplotlib.figure import Figure as Fig
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
from collections import deque
import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mlp
import numpy as np
# Class that inherits wx.Panel. The purpose is to embed it into
# a wxPython App. That part can be seen in main()
class Serial_Plot(wx.Panel):
def __init__(self, parent, strPort, id=-1, dpi=None, **kwargs):
super().__init__(parent, id=id, **kwargs)
self.figure = plt.figure(figsize=(20,20))
self.ax = plt.axes(xlim=(0, 1000), ylim=(0, 5000))
self.plot_data, = self.ax.plot([], [])
self.canvas = FigureCanvas(self, -1, self.figure)
self.toolbar = NavigationToolbar(self.canvas)
self.toolbar.Realize()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND)
sizer.Add(self.toolbar, 0, wx.RIGHT | wx.EXPAND)
self.SetSizer(sizer)
# Serial communication
self.ser = serial.Serial(strPort, 115200)
# Serial data initialized as deque. The serial readings from arduino
# are set to be one value per line.
self.vals = deque()
# matplotlib function animation
anim = animation.FuncAnimation(self.figure, self.update,
interval=20)
plt.show()
self.close
def update(self, i):
try:
# read serial line
data = float(self.ser.readline().decode('utf-8'))
self.vals.append(data)
# update plot data
self.plot_data.set_data(range(len(self.vals)), self.vals)
except:
pass
return self.plot_data
def close(self):
# close serial
self.ser.flush()
self.ser.close()
def main():
app = wx.App(False)
frame = wx.Frame(None, -1, "WX APP!")
demo_plot = Serial_Plot(frame,'COM3')
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()
这是 GUI 之前弹出的图形。
关闭后,figure wx 应用程序可见。
我试图摆脱弹出的图形,只看到嵌入在 wx 应用程序中的图形。我真的很感激任何帮助。
最佳答案
我认为您可能使用 animation.FuncAnimation
搞错了方向。因为我认为这是一个 matplotlib 函数,预计由 matplotlib 的主循环控制,但您使用的是 wxpython,它有自己的循环。 (在这一点上,我保留犯严重错误的权利:))
下面是您的代码,经过修改以使用 random
以避免串行端口并包含 wx.Timer
执行更新。
import wx
from matplotlib.figure import Figure as Fig
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wxagg import NavigationToolbar2WxAgg as NavigationToolbar
from collections import deque
#import serial
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib as mlp
import numpy as np
import random
# Class that inherits wx.Panel. The purpose is to embed it into
# a wxPython App. That part can be seen in main()
class Serial_Plot(wx.Panel):
def __init__(self, parent, strPort, id=-1, dpi=None, **kwargs):
super().__init__(parent, id=id, **kwargs)
self.figure = plt.figure(figsize=(20,20))
self.ax = plt.axes(xlim=(0, 10), ylim=(0, 50))
self.plot_data, = self.ax.plot([], [])
self.canvas = FigureCanvas(self, -1, self.figure)
self.toolbar = NavigationToolbar(self.canvas)
self.toolbar.Realize()
#
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update, self.timer)
#
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.canvas, 1, wx.EXPAND)
sizer.Add(self.toolbar, 0, wx.RIGHT | wx.EXPAND)
self.SetSizer(sizer)
# Serial communication
# self.ser = serial.Serial(strPort, 115200)
# Serial data initialized as deque. The serial readings from arduino
# are set to be one value per line.
self.vals = deque()
# matplotlib function animation
#anim = animation.FuncAnimation(self.figure, self.update,
# interval=2)
#plt.show()
plt.ion() #Turn on interactive plot
#self.close
#
self.timer.Start(1000)
def update(self,event):
#try:
# read serial line
#data = float(self.ser.readline().decode('utf-8'))
data = float(random.randint(1, 50))
self.vals.append(data)
# update plot data
length = len(self.vals)
self.plot_data.set_data(range(length), self.vals)
#Update x axis to follow interactive plot
self.ax.set_xlim(0.0,float(length + 1))
#except:
# pass
#return self.plot_data
plt.plot()
def close(self):
# close serial
self.ser.flush()
self.ser.close()
def main():
app = wx.App(False)
frame = wx.Frame(None, -1, "WX APP!")
demo_plot = Serial_Plot(frame,'COM3')
frame.Show()
app.MainLoop()
if __name__ == "__main__":
main()
注意
self.ax.set_xlim(0.0,float(length + 1))
可以调整为类似 self.ax.set_xlim(float(length - 10), float(length + 1))
它会遵循当前值,而不仅仅是不断延伸 x 轴。
关于python - 在wxPython中嵌入matplotlib FuncAnimation : Unwanted figure pop-up,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52047934/
我遇到了异常对话框的问题:我正在使用 RemObjects SDK for Client/Server -application。当出现连接问题时,客户端开始抛出“简单”异常对话框,只有文本和一个按钮
我遇到了问题。我的 JFrame 从其中的内容开始,但以透明方式显示,内容位于顶部。 package javagame; import java.awt.Color; import java.awt.
我开始研究 Python 3.5 的 logging 包。我在 main 文件中设置了这两个命令来登录 import logging logging.basicConfig(filename=r'fr
我一直在练习我的菜单 CSS 技巧,遇到了不需要的填充情况。菜单中“test2”和“test3”的下拉菜单在文本的左侧有填充。我不知道是什么原因造成的。有什么建议吗? 谢谢 HTML tes
我目前有一个非常奇怪的错误。 一个方法被调用,它应该通过停止它来隐藏一个 UIActivityIndicatorView(启用停止时自动隐藏)和一个名为 badIndicatorUIImageVi
所以我有两个 excel 工作簿 - 工作簿 A 用作用户表单,工作簿 B 用作包含所有数据条目的数据库(B 是合并文件)。每当我打开工作簿 A 时,我想自动从工作簿 B 中提取所有数据并将其复制到工
当您在Delphi XE6中编译DLL时,它会自动从System.Rtti.pas导出函数TMethodImplementationIntercept。我试图找到一种方法来避免这种导出,但没有找到任何
您如何定义“不需要的代码”? 编辑: 恕我直言,任何具有 0 个事件调用成员(递归检查)的代码成员都是不需要的代码。 (函数、方法、属性、变量都是成员) 最佳答案 这是我对不需要的代码的定义: 不执行
我正在尝试使用以下代码将字典值列表提取到文件中: import csv def function(file, output, deli = ','): dictionary = dict()
我正在编写一个程序,用户输入 2 到 1000000 之间的正整数,如果它是质数,则打印“该数字是质数”,如果不是质数,则打印因式分解的号码。每当我输入一个素数(即:3)时,它都会打印出它是一个素数,
在我的网站上 here ,我用 CSS 和 Javascript 实现了多层视差滚动。页面上下滚动,我有 6 个图层都在视差中移动。向后的每一层以比前面的层稍慢的速度滚动。 在我单击右上角的汉堡菜单之
这是我的应用: http://jsbin.com/axeWOwAN/1/edit http://jsbin.com/axeWOwAN/1 (全屏) 如您所见,在第二页中有一张 map ,它无法正常工作
我有这个简单的 bash 脚本:我在参数中传递的每个文件上运行 ns 模拟器,其中最后一个参数是要搜索的一些文本字符串。 #!/bin/bash nsloc="/home/ashish/ns-alli
我对Python很陌生。我尝试通过加载包含两个数字数组的 .txt 文件来绘制图。情节看起来不错,但还有一条我无法摆脱的额外线条。我已在此处附上我的代码。请帮忙!谢谢。 import numpy as
我目前正在尝试使用 SPNEGO library 将 Java 客户端实现到受 SPNEGO 保护的 Web 服务来自 SourceForge(服务器使用相同的库)。我无法让它成功验证,我的请求总是以
我有一个枚举类,它有几个常量,我想添加一些静态值 FOCUSED,它指示哪个枚举值具有焦点... 我找到了一个方法: package messagesystem; /** * * @author
我想校准我的相机。目标是消除失真并且不裁剪图像(如上一张照片)。我是这样做的: 1)加载图片,找到棋盘的角 import cv2 import numpy as np import os import
我正在使用DataTables显示我的数据。我将其宽度指定为 4 个引导列。尽管所有数据都可以适合指定的宽度,但我的表格现在在底部出现了不需要的水平滚动: 向右滚动,我看到顶部的搜索框是这次滚动的原因
有一个定制的导航栏,并且在 iOS 9 之前一直完美运行。横向模式下出现奇怪的边距。 使用标准代码来创建此布局。 - (UIBarButtonItem*)leftMenuButton { UI
我尝试修改以下示例以实现实时绘图。 Embedding a matplotlib figure inside a WxPython panel 我正在尝试读取来自 Arduino 的串行数据并绘制/更
我是一名优秀的程序员,十分优秀!