gpt4 book ai didi

Python - 将 pcolor 与 panda 数据框一起使用

转载 作者:太空宇宙 更新时间:2023-11-03 17:20:05 24 4
gpt4 key购买 nike

我有一个 4397L、39L 的数据框,如下所示:

timestamp             0    1    2    3    4    5    6    7    8    9  ...   \                                                            
2013-04-17 05:00:00 46 46 46 45 45 45 45 45 45 45 ...
2013-04-17 05:10:00 46 46 45 45 45 45 45 45 45 45 ...
2013-04-17 05:20:00 45 46 45 45 45 45 45 45 45 45 ...
2013-04-17 05:30:00 45 46 45 45 45 45 45 45 45 45 ...

当我使用以下方法绘制 pcolor 时:

ax.pcolor(df) 

绘制数据图。

但是,我希望沿 x 轴标记时间 (df.index),并使用新向量 x 标记 y 轴:

[-13.73038435 -12.73038435 -12.73038435 -11.73038435 -11.73038435
-10.73038435 -10.73038435 -9.73038435 -9.73038435 -8.73038435
-8.73038435 -7.73038435 -7.73038435 -6.73038435 -6.73038435
-5.73038435 -5.73038435 -4.73038435 -4.73038435 -3.73038435
-3.73038435 -2.73038435 -2.73038435 -1.73038435 -1.73038435
-0.73038435 -0.73038435 0.26961565 0.26961565 1.26961565
1.26961565 2.26961565 2.26961565 3.26961565 3.26961565
4.26961565 4.26961565 5.26961565 5.26961565]

在 matlab 中,情况很简单:

ax.pcolor(df.index, x, df.transpose())

尺寸为:

df = (4397, 39)
x = (, 39L)
df.index = (4397L,)

然而,同样的逻辑在 Python 中不起作用。给出的错误是:

AttributeError: 'DatetimeIndex' object has no attribute 'reshape'

有什么想法吗?

最佳答案

您需要根据 x 和时间生成网格。这是一个例子,我首先生成 df (函数 x * time 是任意选择的)。而且似乎您还必须在绘图之前将 DataFrame 转换为数组。

time = sp.linspace(0, 10, 5)
x = sp.linspace(0, 1, 10)
TIME, X = sp.meshgrid(time, x)
df = pd.DataFrame(X * TIME).transpose()
df.index.name = 'time'
df.columns.name = 'x'

X, TIME = sp.meshgrid(x, time)
plt.pcolor(X, TIME, sp.array(df))

enter image description here

关于Python - 将 pcolor 与 panda 数据框一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33260045/

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