Mik*_*ike 10 python 3d plot matplotlib
我有一个模拟计算每次模拟迭代的表面数据.我想连续将该数据绘制为同一窗口的表面图(在每次迭代中更新图),以便了解它是如何演变的并检查算法.
我的想法是创建一个类来初始化窗口/绘图,然后从模拟循环内部重绘到该窗口.这是我提出的课程:
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)
Run Code Online (Sandbox Code Playgroud)
我对这段代码的问题是,代码在'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)
Run Code Online (Sandbox Code Playgroud)
plt.show()
如果它是动画(交互式)情节,则不需要.您还希望交互式设置为True,而不是False,这与ion()
在您的2D示例中调用相同.此外,remove()
如果您不想全部看到它们,则需要使用之前帧的曲面图.
否则你非常接近.
这对我有用:
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, time
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 )
rng = np.arange( 0, self.systemSideLength, self.lowerCutoffLength )
self.X, self.Y = np.meshgrid(rng,rng)
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 )
# plt.draw() maybe you want to see this frame?
def drawNow( self, heightR ):
self.surf.remove()
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)
matplotlib.interactive(True)
p = plot3dClass(5,1)
for i in range(2):
p.drawNow(np.random.random(p.X.shape))
Run Code Online (Sandbox Code Playgroud)
我遇到了类似的问题,这对我有用:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for k in xrange(0,X_range):
ax.plot(x_input, y_input, z_input)
plt.draw()
plt.pause(0.02)
ax.cla()
Run Code Online (Sandbox Code Playgroud)
对于你来说,我想解决方案与最上面的答案类似,除了替换time.sleep()
为plt.pause()
,这将在睡觉前完成图形的绘制。
归档时间: |
|
查看次数: |
18558 次 |
最近记录: |