I have a array of values
2515, 2513, 2513, 2513, 2513, 2512, 2515, 2511, 2526, 2513, 2511, 2500, 2521, 2511, 2523, 2512, 2529, 2513, 2526, 2514, 2518, 2512, 2524, 2512, 2527, 2512, 2521, 2512, 2517, 2514, 2522, 2512, 2521, 2512, 2528, 2511, 2523, 2512, 2518, 2513, 2522, 2512, 2511, 2512, 2524, 2512, 2515, 2512, 2509, 2512, 2515, 2512, 2528, 2512, 2516, 2512, 2527, 2512, 2526, 2512, 2528, 2512, 2529, 2512, 2523, 2511, 2526, 2512, 2521, 2513, 2510, 2512, 2523, 2513, 2500, 2511, 2518, 2512, 2513, 2512, 2526, 2512, 2526, 2512, 2520, 2512, 2526, 2512, 2519, 2500, 2529, 2511, 2514, 2512, 2522, 2512, 2513, 2512, 2515, 2512]
When I am using matplotlib I am getting a graph like this
The code to get this graph from matplotlib import pyplot as plt plt.plot(new3[:100]) plt.show()
What should I do to plot this graph in say 25 seconds. I mean there should be a live plotting for a similar graph and it should be completed in 25 seconds. I am not looking for multiple graph I want all the updates to be made in a single graph only
CodePudding user response:
This is what I have. You need matplotlib's animation module and, if you are using Jupyter Notebook and want to see the animation, you need to use %matplotlib notebook
at the top of your cell.
%matplotlib notebook
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
data = [2515, 2513, 2513, 2513, 2513, 2512, 2515, 2511, 2526, 2513, 2511, 2500, 2521, 2511, 2523,
2512, 2529, 2513, 2526, 2514, 2518, 2512, 2524, 2512, 2527, 2512, 2521, 2512, 2517, 2514,
2522, 2512, 2521, 2512, 2528, 2511, 2523, 2512, 2518, 2513, 2522, 2512, 2511, 2512, 2524,
2512, 2515, 2512, 2509, 2512, 2515, 2512, 2528, 2512, 2516, 2512, 2527, 2512, 2526, 2512,
2528, 2512, 2529, 2512, 2523, 2511, 2526, 2512, 2521, 2513, 2510, 2512, 2523, 2513, 2500,
2511, 2518, 2512, 2513, 2512, 2526, 2512, 2526, 2512, 2520, 2512, 2526, 2512, 2519, 2500,
2529, 2511, 2514, 2512, 2522, 2512, 2513, 2512, 2515, 2512]
timePeriod = 25 # 25 seconds
frequency = timePeriod/len(data)
x = np.arange(0, timePeriod, frequency)
fig, ax = plt.subplots()
signal, = ax.plot(x, data)
viewWindow = 5 # seconds
windowRange = int(viewWindow/frequency) # takes the size of the window (x limit range) and divides it by the frequency
def animate(i):
signal.set_ydata(data[i:i windowRange]) # update the data.
signal.set_xdata(x[i:i windowRange])
plt.xlim(i*frequency, (i windowRange)*frequency)
return signal,
ani = animation.FuncAnimation(fig, animate, interval=(frequency*1000), frames = 100-windowRange)
ani.save(filename = "movie.gif",writer=animation.PillowWriter(fps=5)) # how to save it as a gif, you can remove this if you want
plt.show()