Updating a 3D graph every n minutes in Python
<p>I have the following code to plot a 3D graph in Python</p>

<pre><code>data = # taken from an SQL query in (x, y, z) format #

x, y, z = zip(*data)
z = map(float, z)
grid_x, grid_y = np.mgrid[min(x):max(x):100j, min(y):max(y):100j]
grid_z = griddata((x, y), z, (grid_x, grid_y), method='cubic')

fig = plt.figure()
ax = fig.gca(projection='3d')
ax.plot_surface(grid_x, grid_y, grid_z, cmap=plt.cm.Spectral)
plt.show()
</code></pre>

<p>I want the plot to be undated every 1 minute. I can get the data from the table using a thread every 1 minute but I am unable to update the plot. </p>

<p>Can you help me with a suggestion?</p>

<p>Thanks in advance!</p>

<p>Try this</p>

<pre><code>while True:
    plt.pause(1)
    x1,x2,x3=random.uniform(-10,0),random.uniform(0,10),random.uniform(0,0.5)
    y1,y2,y3=random.uniform(-10,0),random.uniform(0,10),random.uniform(0,0.5)

    X,Y=np.arange(x1,x2,x3),np.arange(y1,y2,y3)
    X, Y = np.meshgrid(X, Y)
    R = np.sqrt(X**2 + Y**2)
    Z = np.sin(R)
    surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm,
                       linewidth=0, antialiased=False)

    plt.draw()
</code></pre>

<p><a href="http://matplotlib.org/api/pyplot_api.html?highlight=pause#matplotlib.pyplot.pause" rel="nofollow">matplotlib.pyplot.pause</a> </p>

<pre><code>Pause for interval seconds.
If there is an active figure it will be updated and displayed, and the GUI event loop will run during the pause.
If there is no active figure, or if a non-interactive backend is in use, this executes time.sleep(interval).
This can be used for crude animation. For more complex animation, see matplotlib.animation.
This function is experimental; its behavior may be changed or extended in a future release.
</code></pre>

<p>If it works,please accept this answer.</p>

python matplotlib plot