如何在 2D 和 3D 投影之间切换

发布于 2024-12-12 11:01:44 字数 943 浏览 9 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

烟花肆意 2024-12-19 11:01:45

首先我要说的是,如果你通过从你之前实际使用过的问题中划分答案来提高你的接受率(目前为 0%),那么也许会有更多的人愿意帮助你。

现在,回答你的问题,没有“2d”投影 kwarg。但是,如果您想根据关键字“q”浏览并快速决定所需的投影类型,以下内容应该会对您有所帮助。我还更改了基本设置,以避免不同类型的绘图之间的混淆,因为您在循环之外有一些绘图调用,并且通常会清理您的组织。

希望这有帮助。

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()
plt.clf()

q='2d'

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)

if q=='3d':

  # Create a 3D scatter plot...
  ax = fig.add_subplot(111, projection='3d')
  ax.scatter(xs, ys, zs, c=c, marker=m)
  ax.set_xlabel('X Label')
  ax.set_ylabel('Y Label')
  ax.set_zlabel('Z Label')
  plt.show()

else:
  plt.clf()
  ax = fig.add_subplot(111)
  ax.plot(xs, ys)
  ax.set_xlabel('X Label')
  ax.set_ylabel('Y Label')
  plt.show()

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.

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()
plt.clf()

q='2d'

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)

if q=='3d':

  # Create a 3D scatter plot...
  ax = fig.add_subplot(111, projection='3d')
  ax.scatter(xs, ys, zs, c=c, marker=m)
  ax.set_xlabel('X Label')
  ax.set_ylabel('Y Label')
  ax.set_zlabel('Z Label')
  plt.show()

else:
  plt.clf()
  ax = fig.add_subplot(111)
  ax.plot(xs, ys)
  ax.set_xlabel('X Label')
  ax.set_ylabel('Y Label')
  plt.show()
温柔女人霸气范 2024-12-19 11:01:45

我相信我已经找到了一个可能的解决方案,尽管它似乎会导致一些内存问题。我怀疑它实际上并没有删除初始绘图数据,只是将其从图中删除,因此每次更改投影时内存使用量都会上升。

# Delete the 3D subplot
self.fig.delaxes(self.axes)
# Create a new subplot that is 2D
self.axes = self.fig.add_subplot(111)
# 2D scatter
self.axes.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
# Update the figure
self.canvas.draw()

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.

# Delete the 3D subplot
self.fig.delaxes(self.axes)
# Create a new subplot that is 2D
self.axes = self.fig.add_subplot(111)
# 2D scatter
self.axes.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
# Update the figure
self.canvas.draw()
琉璃梦幻 2024-12-19 11:01:45

我遇到了同样的问题,并尝试了公认的解决方案(由丹发布)。它有效,但给了我以下警告:

“用户警告:该图包含与以下不兼容的轴
strict_layout,因此其结果可能不正确。”

但是,如果我使用:

self.figure.clf()
self.axes = self.figure.add_subplot(111)

那么它就可以正常工作而不会发出任何警告。

I was having the same problem and tried the accepted solution (posted by Dan). It worked, but gave me the following warning:

"UserWarning: This figure includes Axes that are not compatible with
tight_layout, so its results might be incorrect."

However if I use:

self.figure.clf()
self.axes = self.figure.add_subplot(111)

then it works without any warnings.

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