使用 matplotlib 绘制 3D 绘图,draw() 需要一个附加参数

发布于 2024-11-06 20:51:11 字数 472 浏览 0 评论 0原文

各位。昨天我正在探索 matplotlib 的 3D 绘图功能(很惊喜,它看起来不错)。需要明确的是

from mpl_toolkits.mplot3d import Axes3D

,当我尝试使用draw()连续重绘绘图时,我收到错误,draw()(对于3d图)除了self之外还需要一个额外的参数。该参数称为渲染器,并且不是可选的。查看 3d 轴的代码,我找不到应该作为渲染器放置的内容的规范。

您知道我应该如何解决这个问题吗?需要明确的是,我可以将 draw() 与普通绘图一起使用(打开 ion() 等),所以我的问题仅与 Axes3D 有关。

基本上,我想模仿我已经在 matlab 中编写的一些代码,它绘制 3d 绘图,然后更新它(使用drawow())。

编辑:我意识到渲染器参数可能是特定于计算机的。我坐在 Windows 上,安装了 Enthought Python。如果您需要更多信息,请告诉我。

Dear all. I was exploring matplotlib's 3d plot capabilities yesterday (and was pleasantly surprised, it looks good). Just to be clear, with

from mpl_toolkits.mplot3d import Axes3D

However, when I try to continuously redraw the plot with draw(), I get the error that draw() (for 3d plots) takes an additional argument aside from self. This argument is called renderer, and is not optional. Looking through the code for 3d axes, I couldn't find a specification of what I should put as renderer.

Do you have any idea how I should work around this problem? Just to be clear, I can use draw() with normal plots (turning ion() on etc.), so my issue is only with Axes3D.

Basically, I want to mimick some code I already wrote in matlab, which draws a 3d plot and then updates it (with drawnow()).

EDIT: I realized that the renderer argument might be computer specific. I am sitting on a Windows with the Enthought installation of Python. If you need more information, let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙厌世 2024-11-13 20:51:11

编辑:我收回它。您可以从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

EDIT: I take it back. You can get it from fig.canvas.renderer. I'll leave the example below, though.


There does not appear to be a documented way to get the renderer, but you can probably just peek within the axes structure at the _cachedRenderer. Note that you have to cause a plot to draw before this is set.

See the last line of this example:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文