【发布时间】:2011-07-07 23:11:52
【问题描述】:
我有一个模拟,它为模拟的每次迭代计算表面数据。 我想将该数据作为曲面图连续绘制到同一窗口(在每次迭代中更新该图),以查看它如何演变并检查算法。
我的想法是创建一个类来初始化窗口/绘图,然后从模拟循环内部重绘到该窗口。这是我想出的课程:
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()”的调用是否会按照我的意愿更新图形。
那么这个类是正确的方向吗?
如果是:需要进行哪些修改?
如果没有:有人可以给我建议如何实现我想要的吗?
我意识到这个问题对其他人来说可能看起来微不足道,但我(老实说)昨天确实花了一整天时间在 Google 上尝试,但我不知所措......
任何帮助将不胜感激,这样我就可以回到我的实际工作中。
提前很多坦克。
作为参考:
我还发现了下面的代码,我想要什么,但它是二维的,所以它不能直接帮助我:
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)
【问题讨论】:
标签: python 3d matplotlib plot