gpt4 book ai didi

python - 如何以防止 xdata 为 None 的方式更新 matplotlib 图?

转载 作者:太空宇宙 更新时间:2023-11-03 18:39:36 27 4
gpt4 key购买 nike

我正在编写一个应用程序,该应用程序定期(大约每秒一次)从硬件设备收集一些数据,并使用 matplotlib 将其显示在图表中。用户将定期单击图表,当发生这种情况时,我需要知道单击的 xy 坐标(在图表的坐标系中测量)。 p>

这很容易做到:如果 canvas 是一个 FigureCanvas 实例,你可以说

canvas.mpl_connect('button_release_event', mouse_up_handler)

并定义

def mouse_up_handler(event):
print('The user clicked at ({}, {})'.format(event.xdata, event.ydata))

显示图形坐标系中的点击坐标。但有时,用户会在图表更新时单击鼠标右键,然后 event.xdata.ydataNone。现在我只是放弃这些点击,但显然当应用程序忽略他们的点击时,这对用户来说非常令人沮丧。

我附上了完整的示例程序,以便您可以看到我如何用新图表替换当前图表。 (如果您运行它并反复单击图表,您会发现报告的 x 坐标有时为 None。)我可以以一种永远不会发生的方式替换图形的数据吗?给出 None 坐标?我只想一次显示一组数据。

示例程序

from __future__ import print_function
from math import exp
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg \
as FigureCanvas
from matplotlib.figure import Figure
from random import normalvariate
from threading import Timer
from time import sleep
import wx

class GraphView(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)

self.figure = Figure()
canvas = FigureCanvas(self, -1, self.figure)
canvas.mpl_connect('button_release_event', self.mouse_up_handler)

self.axes = None

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(canvas, 1, wx.GROW)
self.SetSizer(sizer)

self.generate_new_data()

def generate_new_data(self):
x_values = range(16384)
y_values = [exp(normalvariate(0, 10)) for _ in range(16384)]

if self.axes is not None:
self.figure.delaxes(self.axes)

sleep(0.4) # simulate some additional work being done with the data

self.axes = self.figure.add_subplot(1, 1, 1)
self.axes.set_yscale('log')
self.axes.autoscale(enable=True)
self.axes.set_xlim(x_values[0], x_values[-1])
self.axes.plot(x_values, y_values, antialiased=False)

self.figure.canvas.draw()

Timer(1.0, self.generate_new_data).start()

def mouse_up_handler(self, event):
print('Click at x = {}'.format(event.xdata))


class GraphWindow(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent)
graph_view = GraphView(self)


if __name__ == '__main__':
app = wx.App(False)
GraphWindow(None).Show()
app.MainLoop()

最佳答案

类似于:

def __init__(self, parent):
wx.Panel.__init__(self, parent)

self.figure = Figure()
canvas = FigureCanvas(self, -1, self.figure)
canvas.mpl_connect('button_release_event', self.mouse_up_handler)

# make axes
self.ax = self.figure.add_subplot(1, 1, 1)
# set up details
self.ax.set_yscale('log')
self.ax.autoscale(enable=True)
self.ax.set_xlim(x_values[0], x_values[-1])
# make the line
self.ln, = self.ax.plot([], []) # add what ever parameters you want

sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(canvas, 1, wx.GROW)
self.SetSizer(sizer)

self.generate_new_data()



def generate_new_data(self):
x_values = range(16384)
y_values = [exp(normalvariate(0, 10)) for _ in range(16384)]

sleep(0.4) # simulate some additional work being done with the data

# update your existing line
self.ln.set_data(x_values, y_values, antialiased=False)

self.figure.canvas.draw()

Timer(1.0, self.generate_new_data).start()

重复使用坐标区和Line2D对象。

关于python - 如何以防止 xdata 为 None 的方式更新 matplotlib 图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20806141/

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