使用matplotlib绘制3D图时,draw()需要额外参数
大家好。我昨天在研究matplotlib的3D绘图功能,结果发现效果挺不错的。为了说明一下,使用
from mpl_toolkits.mplot3d import Axes3D
不过,当我尝试用draw()不断重绘图形时,出现了一个错误,提示draw()(针对3D图)除了self之外还需要一个额外的参数。这个参数叫做renderer,而且是必须的。在查看3D坐标轴的代码时,我找不到应该把什么放在renderer的位置。
你们有没有什么建议,能帮我解决这个问题?需要说明的是,我在普通图形上可以用draw()(开启ion()等),所以我遇到的问题只是在Axes3D上。
基本上,我想模仿我在matlab中写的一段代码,这段代码绘制了一个3D图形,然后更新它(用drawnow())。
补充:我意识到renderer参数可能和电脑有关。我现在用的是Windows系统,安装的是Enthought的Python。如果你们需要更多信息,请告诉我。
1 个回答
0
编辑: 我收回之前的话。你可以通过 fig.canvas.renderer
来获取它。不过我还是会把下面的例子留着。
目前似乎没有官方文档说明如何获取渲染器,但你可以试着在坐标轴的结构中查看一下 _cachedRenderer。请注意,你必须先让图形绘制出来,这个值才会被设置。
请看这个例子的最后一行:
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
X = np.arange(-5, 5, 0.25)
xlen = len(X)
Y = np.arange(-5, 5, 0.25)
ylen = len(Y)
X, Y = np.meshgrid(X, Y)
R = np.sqrt(X**2 + Y**2)
Z = np.sin(R)
colortuple = ('y', 'b')
colors = np.empty(X.shape, dtype=str)
for y in range(ylen):
for x in range(xlen):
colors[x, y] = colortuple[(x + y) % len(colortuple)]
surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors,
linewidth=0, antialiased=False)
ax.set_zlim3d(-1, 1)
ax.w_zaxis.set_major_locator(LinearLocator(6))
ax.w_zaxis.set_major_formatter(FormatStrFormatter('%.03f'))
plt.draw()
print ax._cachedRenderer