我有一个模拟计算每次模拟迭代的表面数据。 我想连续将该数据作为曲面图绘制到同一个窗口中(更新每次迭代中的绘图),以便了解它如何演变并检查算法。使用python-matplotlib进行连续3D绘图(即图形更新)?
我的想法是创建一个类,它将初始化窗口/绘图,然后从模拟循环内部重新绘制到该窗口。这里是我想出了类:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib
matplotlib.interactive(False)
class plot3dClass(object):
def __init__(self, systemSideLength, lowerCutoffLength):
self.systemSideLength = systemSideLength
self.lowerCutoffLength = lowerCutoffLength
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111, projection='3d')
self.ax.set_zlim3d(-10e-9, 10e9)
X = np.arange(0, self.systemSideLength, self.lowerCutoffLength)
Y = X
self.X, self.Y = np.meshgrid(X, Y)
self.ax.w_zaxis.set_major_locator(LinearLocator(10))
self.ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))
heightR = np.zeros(self.X.shape)
self.surf = self.ax.plot_surface(self.X, self.Y, heightR, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False)
#~ self.fig.colorbar(self.surf, shrink=0.5, aspect=5)
plt.show()
def drawNow(self, heightR):
self.surf = self.ax.plot_surface(self.X, self.Y, heightR, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False)
plt.draw() # redraw the canvas
time.sleep(1)
我有这个代码的问题,是代码停在“plt.show()”,只有继续下去,当我关闭情节窗口。此外,我不确定'self.ax.plot_surface(...)'和'plt.draw()'的调用是否会按照我的意愿更新该图。
那么这堂课是否正确?
如果是:需要进行哪些修改?
如果不是的话:有人可以请给我建议如何实现我想要的?
我意识到这个问题似乎微不足道的人,但我(老实说)确实花了整整一天昨日在谷歌和尝试,我不知所措......
任何帮助将不胜感激,这样我才能回到我的实际工作。
坦克很多提前。
作为参考:
我还发现以下代码呢,我想要的,但它是2D的,所以它不会帮我直接:
from pylab import *
import time
ion()
tstart = time.time() # for profiling
x = arange(0,2*pi,0.01) # x-array
line, = plot(x,sin(x))
for i in arange(1,200):
line.set_ydata(sin(x+i/10.0)) # update the data
draw() # redraw the canvas
print 'FPS:' , 200/(time.time()-tstart)
这不适用于我的电脑(Win XP,python 2.7.3):我看到的只是最终的图 – 2012-07-25 12:44:38
它需要plt.draw()行后面的self.fig.canvas.flush_events()。 (信用http://stackoverflow.com/a/4098938/415551) – 2017-01-20 18:49:04