带有变化颜色的动画matplotlib图?

时间:2019-02-01 10:29:31

标签: matplotlib

x64,W10,Python 3.7

我一直在使用this question答案中的代码。

import matplotlib.pyplot as plt
plt.ion()
class DynamicUpdate():
    #Suppose we know the x range
    min_x = 0
    max_x = 10

    def on_launch(self):
        #Set up plot
        self.figure, self.ax = plt.subplots()
        self.lines, = self.ax.plot([],[], 'ro')
        #Autoscale on unknown axis and known lims on the other
        self.ax.set_autoscaley_on(True)
        self.ax.set_xlim(self.min_x, self.max_x)
        #Other stuff
        self.ax.grid()

    def on_running(self, xdata, ydata):
        #Update data (with the new _and_ the old points)
        self.lines.set_xdata(xdata)
        self.lines.set_ydata(ydata)
        #Need both of these in order to rescale
        self.ax.relim()
        self.ax.autoscale_view()
        #We need to draw *and* flush
        self.figure.canvas.draw()
        self.figure.canvas.flush_events()

    #Example
    def __call__(self):
        import numpy as np
        import time
        self.on_launch()
        xdata = []
        ydata = []
        for x in np.arange(0,10,0.5):
            xdata.append(x)
            ydata.append(np.exp(-x**2)+10*np.exp(-(x-7)**2))
            self.on_running(xdata, ydata)
            time.sleep(1)
        return xdata, ydata

d = DynamicUpdate()
d()

这几乎就是我所追求的,并且在我的应用程序中表现很好,只是我希望绘图也可以更新标记的颜色。我尝试了以下更改...

on_launch

self.lines, = self.ax.plot([],[],[])

然后在on_running(self, xdata, ydata)

def on_running(self, xdata, ydata, col):

..并替换

   self.lines.set_xdata(xdata)
    self.lines.set_ydata(ydata)

使用

    self.lines.set_data(xdata, ydata, col)

我也尝试过

    self.lines.set_data([xdata, ydata, col])

然后从def __call__(self):

我试图通过。

self.on_running(xdata, ydata, 'bo')

我收到以下错误信息...

  File "C:/Users/Technical/.spyder-py3/temp.py", line 11, in on_launch
    self.lines, = self.ax.plot([],[], [])

ValueError: too many values to unpack (expected 1)

在这种情况下,如何正确传递marker参数?

0 个答案:

没有答案