如何在 2D 和 3D 投影之间切换
我有一个 matplotlib 图,我希望能够在 2D 和 3D 投影之间切换。我可以从 2D 转到 3D,但我似乎不知道如何转到另一条路。示例...
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
# Create a 3D scatter plot...
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# Now I want a 2D plot...
ax.cla()
ax = fig.add_subplot(111)
ax.plot(xs, ys)
plt.show()
绘图保留在 3D 投影中,并且投影=“2D”不是有效的 kwarg...
我想也许 ax.clf() 会做我想要的事情并让我定义一个新图形。但它只是给了我以下错误: ValueError:未知元素 o
任何人都可以给我一个有关解决方案的提示吗? ValueError 与问题有关还是暗示我的设置存在其他问题?是否有 kwarg 将投影从 3D 切换到 2D?
I have a matplotlib figure that I want to be able to switch between 2D and 3D projections. I can go from 2D to 3D but I can't seem to work out how to go the other way. Example...
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def randrange(n, vmin, vmax):
return (vmax-vmin)*np.random.rand(n) + vmin
fig = plt.figure()
# Create a 3D scatter plot...
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zl, zh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')
# Now I want a 2D plot...
ax.cla()
ax = fig.add_subplot(111)
ax.plot(xs, ys)
plt.show()
The plot stays in the 3D projection and projection="2D" isn't a valid kwarg...
I thought perhaps ax.clf() would do what I wanted and let me define a new figure. But it just gives me the following error:
ValueError: Unknown element o
Can anyone give me a hint as to the solution to this? Is the ValueError related to the problem or a hint to something else wrong with my setup? Is there a kwarg to switch the projection from 3D to 2D?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先我要说的是,如果你通过从你之前实际使用过的问题中划分答案来提高你的接受率(目前为 0%),那么也许会有更多的人愿意帮助你。
现在,回答你的问题,没有“2d”投影 kwarg。但是,如果您想根据关键字“q”浏览并快速决定所需的投影类型,以下内容应该会对您有所帮助。我还更改了基本设置,以避免不同类型的绘图之间的混淆,因为您在循环之外有一些绘图调用,并且通常会清理您的组织。
希望这有帮助。
Let me start out by saying that if you boosted your acceptance rate (which is currently 0%) by going and demarcating the answers from your previous questions that you actually used, then perhaps more people would be willing to help you.
Now, to answer your question, there is no '2d' projection kwarg. However, if you want to go through and quickly decide which type of projection you would like, depending on a keyword 'q', the following should help you. I also altered the basic setup to avoid confusion between your different kinds of plots, since you had some plotting calls outside of the loop, and generally clean up your organization.
Hope this helps.
我相信我已经找到了一个可能的解决方案,尽管它似乎会导致一些内存问题。我怀疑它实际上并没有删除初始绘图数据,只是将其从图中删除,因此每次更改投影时内存使用量都会上升。
I believe I have found one possible solution, although it seems to result in a bit of a memory issue. I suspect that it isn't actually deleting the initial plot data, just removing it from the figure so memory usage does climb every time the projection is changed.
我遇到了同样的问题,并尝试了公认的解决方案(由丹发布)。它有效,但给了我以下警告:
但是,如果我使用:
那么它就可以正常工作而不会发出任何警告。
I was having the same problem and tried the accepted solution (posted by Dan). It worked, but gave me the following warning:
However if I use:
then it works without any warnings.