在Python中每n分钟更新一次3D图表

时间:2016-08-30 01:23:50

标签: python matplotlib plot

我有以下代码在Python中绘制3D图形

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()

我希望情节每1分钟更新一次。我可以每1分钟使用一个线程从表中获取数据,但我无法更新绘图。

你能帮我一个建议吗?

提前致谢!

2 个答案:

答案 0 :(得分:0)

试试这个

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()

matplotlib.pyplot.pause

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.

答案 1 :(得分:0)

经过一些研究后,我发现了一种使用matplotlib.animation进行此操作的方法。

下面的Youtube链接非常有用。

https://www.youtube.com/watch?v=7w8jk0r4lxA