python matplotlib从函数更新散点图

hgncfbus  于 2023-10-24  发布在  Python
关注(0)|答案(3)|浏览(155)

我试图自动更新散点图。我的X和Y值的来源是外部的,数据是以非预测的时间间隔(轮次)自动推送到我的代码中的。
我只是在整个过程结束时才设法绘制所有数据,而我试图不断地将数据添加到画布中。
我得到的(在整个运行结束时)是:

而我所追求的是:

我的代码的简化版本:

  1. import matplotlib.pyplot as plt
  2. def read_data():
  3. #This function gets the values of xAxis and yAxis
  4. xAxis = [some values] #these valuers change in each run
  5. yAxis = [other values] #these valuers change in each run
  6. plt.scatter(xAxis,yAxis, label = 'myPlot', color = 'k', s=50)
  7. plt.xlabel('x')
  8. plt.ylabel('y')
  9. plt.show()
3b6akqbq

3b6akqbq1#

有几种方法可以制作matplotlib图的动画。下面让我们看两个使用散点图的最小示例。

(a)使用交互模式plt.ion()

要使动画发生,我们需要一个事件循环。(“交互式打开”)。然后需要首先绘制图形,然后可以在循环中更新绘图。在循环中,我们需要绘制画布,并为窗口引入一个小停顿来处理其他事件(像鼠标交互等)。没有这个暂停窗口将冻结。最后我们调用plt.waitforbuttonpress()让窗口保持打开,即使动画已经完成。

  1. import matplotlib.pyplot as plt
  2. import numpy as np
  3. plt.ion()
  4. fig, ax = plt.subplots()
  5. x, y = [],[]
  6. sc = ax.scatter(x,y)
  7. plt.xlim(0,10)
  8. plt.ylim(0,10)
  9. plt.draw()
  10. for i in range(1000):
  11. x.append(np.random.rand(1)*10)
  12. y.append(np.random.rand(1)*10)
  13. sc.set_offsets(np.c_[x,y])
  14. fig.canvas.draw_idle()
  15. plt.pause(0.1)
  16. plt.waitforbuttonpress()

(B)使用FuncAnimation

以上大部分都可以使用matplotlib.animation.FuncAnimation自动化。FuncAnimation将负责循环和重绘,并在给定的时间间隔后不断调用函数(在本例中为animate())。动画将仅在调用plt.show()时启动,从而自动在绘图窗口的事件循环中运行。

  1. import matplotlib.pyplot as plt
  2. import matplotlib.animation
  3. import numpy as np
  4. fig, ax = plt.subplots()
  5. x, y = [],[]
  6. sc = ax.scatter(x,y)
  7. plt.xlim(0,10)
  8. plt.ylim(0,10)
  9. def animate(i):
  10. x.append(np.random.rand(1)*10)
  11. y.append(np.random.rand(1)*10)
  12. sc.set_offsets(np.c_[x,y])
  13. ani = matplotlib.animation.FuncAnimation(fig, animate,
  14. frames=2, interval=100, repeat=True)
  15. plt.show()
展开查看全部
qq24tv8q

qq24tv8q2#

据我所知,你想以交互方式更新你的图。如果是这样,你可以使用图而不是散点图,并像这样更新你的图的数据。

  1. import numpy
  2. import matplotlib.pyplot as plt
  3. fig = plt.figure()
  4. axe = fig.add_subplot(111)
  5. X,Y = [],[]
  6. sp, = axe.plot([],[],label='toto',ms=10,color='k',marker='o',ls='')
  7. fig.show()
  8. for iter in range(5):
  9. X.append(numpy.random.rand())
  10. Y.append(numpy.random.rand())
  11. sp.set_data(X,Y)
  12. axe.set_xlim(min(X),max(X))
  13. axe.set_ylim(min(Y),max(Y))
  14. raw_input('...')
  15. fig.canvas.draw()

如果这是你正在寻找的行为,你只需要创建一个函数,附加sp的数据,并在该函数中获得你想要绘制的新点(无论是I/O管理还是你正在使用的任何通信过程)。

展开查看全部
1u4esq0p

1u4esq0p3#

下面是一种在笔记本电脑中创建交互式绘图的方法

  1. # Import Libraries
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from IPython.display import display, clear_output
  5. # Create figure and subplot
  6. fig = plt.figure()
  7. ax = fig.add_subplot(1, 1, 1)
  8. # Define and update plot
  9. for i in range(20):
  10. x = np.linspace(0, i, 100);
  11. y = np.cos(x)
  12. ax.set_xlim(0, i)
  13. ax.cla()
  14. ax.plot(x, y)
  15. display(fig)
  16. clear_output(wait = True)
  17. plt.pause(0.1)

这将迭代地更新相同的图。详细描述在这里https://pythonguides.com/matplotlib-update-plot-in-loop/

展开查看全部

相关问题