Dynamic plotting with matplotlib
Matplotlib is a great tool to visualise two-dimensional geometric data (and 3D data to some extent). You can also use it to dynamically visualise the convergence of an iterative solver.
Surprisingly, you don’t need any fancy functionality to accomplish this, such as, for example, the FuncAnimation
object of the animation
package.
import matplotlib.pyplot as plt import time import random ysample = random.sample(xrange(-50, 50), 100) xdata = [] ydata = [] plt.show() axes = plt.gca() axes.set_xlim(0, 100) axes.set_ylim(-50, +50) line, = axes.plot(xdata, ydata, 'r-') for i in range(100): xdata.append(i) ydata.append(ysample[i]) line.set_xdata(xdata) line.set_ydata(ydata) plt.draw() plt.pause(1e-17) time.sleep(0.1) # add this if you don't want the window to disappear at the end plt.show()
import matplotlib.pyplot as plt
plt.ion()
for i in range(10):
plt.scatter(i, i)
plt.pause(0.5)
this also works but only for scatter()
Thank you Aditya,
I was searching for a simple way to refresh of plot, and yours is the simplest that works!
I think it needs to be
range
instead ofxrange
.Also: Thanks a lot for posting this example!
This post was very helpful! Is there a way I can plot 2 lines and animate them?
That’s very simple and awesome.
Thank you so much!
That’s what I’m looking for! Thanks a lot 😀
I’m looking for something similar. This is just plotting the plot with all x and y values from a txt file at once. But it is not updating the values. I want something which draws (3,4) as an example, then wait for 1 maybe 2 seconds, and then it should plot (3,4) and (2,3) as an example.