【问题标题】:matplotlib 3d back to 2dmatplotlib 3d 回到 2d
【发布时间】:2011-10-31 02:14:32
【问题描述】:

我有一个 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 投影中,并且 projection="2D" 不是有效的 kwarg...

我想也许 ax.clf() 会做我想做的事,让我定义一个新的数字。但它只是给了我以下错误: ValueError:未知元素o

谁能给我一个关于解决这个问题的提示? ValueError 是与问题相关还是提示我的设置有其他问题?是否有将投影从 3D 切换到 2D 的 kwarg?

非常感谢您提供的任何指针。 丹

【问题讨论】:

    标签: python wxpython matplotlib


    【解决方案1】:

    首先我要说的是,如果您通过区分以前实际使用的问题的答案来提高接受率(目前为 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()
    

    【讨论】:

    • 感谢您输入宇宙。我错过了接受和投票有用不是一回事!现在纠正了我的错误。由于我的简化示例,我想我的问题一定有些模棱两可。我希望能够在 2D 和 3D 投影之间切换单个图形,而不是创建一个或另一个。该图位于 wxPython GUI 中的画布上。有什么想法可以实现吗?
    【解决方案2】:

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

    # 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()
    

    【讨论】:

    • 请注意这里的self 是什么?谢谢
    【解决方案3】:

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

    "UserWarning:此图包含不兼容的轴 紧密布局,所以它的结果可能不正确。”

    但是,如果我使用:

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

    然后它会在没有任何警告的情况下工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 2018-07-30
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多