gpt4 book ai didi

python - matplotlib 3d 回到 2d

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:22 25 4
gpt4 key购买 nike

我有一个 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?

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

最佳答案

首先让我说,如果您通过将您实际使用过的先前问题的答案划定界限来提高您的接受率(目前为 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()

关于python - matplotlib 3d 回到 2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7949384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com