Reputation: 315
I am receiving data every second and I need to update the same pie chart with each data received. I read some forums and have a code in place. However, currently with each recieving values, I am getting another pie chart i.e. the previous pie chart value is not getting refreshed. I want to refresh the same pie chart value
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()
The value of x1
and x2
keeps changing. All this is inside a for loop
that listens new data arising in x1
and x2
What am I doing wrong?
Upvotes: 1
Views: 1989
Reputation: 35155
I didn't have any experience with pie chart animation, but here's a great answer that I found helpful.
import random
from matplotlib import pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
fig,ax = plt.subplots()
labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
sizes = [15, 30, 45, 10]
explode = (0, 0.1, 0, 0)
def animate(i):
new_sizes = []
new_sizes = random.sample(sizes, len(sizes))
print(new_sizes)
ax.clear()
ax.axis('equal')
ax.pie(new_sizes, labels=labels, autopct='%1.1f%%', shadow=True, startangle=140)
anim = FuncAnimation(fig, animate, frames=100, repeat=False)
plt.show()
Upvotes: 1
Reputation: 2431
Please check the snippet. This will update graph in every 2 seconds.
You can call plt.pause(2)
to both draw the new data and it runs the GUI's event loop
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()
Upvotes: 1