matplotlib 3d 回到 2d [英] matplotlib 3d back to 2d

查看:80
本文介绍了matplotlib 3d 回到 2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 matplotlib 图,我希望能够在 2D 和 3D 投影之间切换.我可以从2D到3D,但似乎无法解决其他问题.示例...

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

情节停留在 3D 投影中,而 projection="2D" 不是有效的 kwarg...

The plot stays in the 3D projection and projection="2D" isn't a valid kwarg...

我认为ax.clf()可能会做我想要的事情,然后让我定义一个新的图形.但这只是给我以下错误:ValueError:未知元素o

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

有人可以给我提示解决方案吗?ValueError与问题有关还是与我的设置有其他问题的暗示?是否有将投影从3D切换到2D的怪癖?

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?

在此先感谢您提供的任何指针.丹

Many thanks in advance for any pointer you can provide. Dan

推荐答案

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

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

这篇关于matplotlib 3d 回到 2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆