python - 动态更新饼图
问题描述
我每秒都在接收数据,我需要用接收到的每个数据更新同一个饼图。我阅读了一些论坛并有一个代码。然而,目前每个接收值,我得到另一个饼图,即之前的饼图值没有被刷新。我想刷新相同的饼图值
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
labels=['a','b']
def animate(i):
nums=[x1,x2]
ax.clear()
ax1.pie(nums, labels=labels,
autopct='%1.1f%%', shadow=True, startangle=140)
ani = animation.FuncAnimation(fig, animate, interval=1000)
plt.show()
的价值x1
和x2
不断变化。所有这些都在一个for loop
监听新数据的内部x1
,x2
我在做什么错?
解决方案
请检查片段。这将每 2 秒更新一次图表。
您可以调用plt.pause(2)
来绘制新数据并运行 GUI 的事件循环
import random
import matplotlib.pyplot as plt
fig = plt.figure()
ax1 = fig.add_subplot(1,1,1)
labels=['a','b']
for i in range(10):
nums=[random.randint(5,10),random.randint(5,10)]
ax1.clear()
ax1.pie(nums, labels=labels, autopct='%1.1f%%', shadow=True, startangle=140)
plt.pause(2)
plt.draw()
推荐阅读
- flutter - 嵌套的 StreamBuilder 是好的模式吗?
- mysql - my.cnf 位于 macOS High Sierra 上的哪个位置?
- c# - 在 SQL 数据库的插入语句中调用 FILETOCLOB 函数的正确方法是什么?
- wordpress - 过滤器应用于意外查询
- java - 在 Azure 搜索上上传文档后,我可以查询它吗?
- pact - 如何处理 Pact 上的多态端点?
- r - 如何修复 R 中的“找不到对象”错误
- qt - 如何在 Qt 5.12 中使用 /std:c++latest
- linux - 为什么我的 Raspberry Pi SPI 写入循环会暂停大约 15 毫秒?上下文切换?
- html - 如何修复这个第二层下拉菜单背景?