gpt4 book ai didi

Python,改变 3D 图的轴

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

我有以下代码:

xx = np.arange(len(days[0]))
ys = [i+xx+(i*xx)**2 for i in range(len(days[0]))]
colors = cm.rainbow(np.linspace(0, 1, len(ys)))

for d,cc in zip(days[0],colors):

ax.scatter(t,p,d,color=cc)

t 和 p 是列表(时间和价格),d 是整数(天)。当我运行代码时,得到的结果如下:

enter image description here

问题是轴错误。 p 和 d 需要交换但是当我尝试这样做时:

ax.scatter(t,d,p)

我收到一条错误消息“参数 xs 和 ys 的大小必须相同”。有什么方法可以让轴切换,因为直观上该图在这种配置中没有意义。

迭代这些日子的原因是这样我可以为绘图上的每一天使用单独的颜色。

我尝试了每天迭代 t 和 p 列表并仅绘制各个相应的 t、d、p 点的解决方案,但是速度要慢得多,并且如果您尝试移动它,之后 matplotlib 绘图将无响应。

最佳答案

我不确定您为什么会收到错误消息,但您能否提供数据示例?以下代码工作正常,并生成您要求的绘图类型。

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np

# Generate some dummy data
time = np.random.rand(100)
price = 120+10*np.random.rand(100)
day = np.random.randint(0,10,100)


# Plot data
fig = plt.figure(figsize=(12,4))

ax = fig.add_subplot(121, projection='3d')
ax.scatter(time, price, day)
ax.set_xlabel('time')
ax.set_ylabel('price')
ax.set_zlabel('day')

ax = fig.add_subplot(122, projection='3d')
ax.scatter(time, day, price)
ax.set_xlabel('time')
ax.set_ylabel('day')
ax.set_zlabel('price')

fig.show()

enter image description here

编辑:

您可以通过传递列表/数组来设置散点图中点的颜色。如果我们使用以下方法绘制第二个散点图:

ax.scatter(time, day, price, c=day)

我们得到: enter image description here

关于Python,改变 3D 图的轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43382583/

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