Python,改变3D p的轴

2024-04-24 13:49:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

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

问题是轴心是错的。但需要交换 当我试图做到:

^{pr2}$

我得到一个错误,说“参数xs和ys的大小必须相同”。有没有什么方法可以让坐标轴切换,因为直观地说,在这种配置下,绘图是没有意义的。在

这些天被迭代的原因是为了让我可以在图中的每一天都有一个单独的颜色。在

我尝试过每天迭代t和p列表,只绘制相应的t,d,p点的方法,但是这要慢得多,而且如果你试图移动matplotlib绘图,它就没有响应了。在


Tags: 方法代码in绘图列表forlennp
1条回答
网友
1楼 · 发布于 2024-04-24 13:49:59

我不知道你为什么会收到一条错误信息,但是你能给我一个你的数据样本吗?下面的代码运行良好,并生成您所要求的绘图类型。在

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

{a1}

编辑:

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

^{pr2}$

我们得到: enter image description here

相关问题 更多 >