gpt4 book ai didi

python - 如何使用 Pandas 在 x 轴上绘制列并使用索引作为 y 轴?

转载 作者:行者123 更新时间:2023-12-03 20:22:47 26 4
gpt4 key购买 nike

我有一个包含 60 列的数据框。我希望在 X 轴上绘制列,在 y 轴上绘制索引。

    df.plot() 

上面的代码默认将索引放在 x 轴上。我无法弄清楚如何切换轴。
任何帮助表示赞赏。

最佳答案

也许您可以绘制转置的 DataFrame 。

创建一个具有 10 列和 5 行的 DataFrame。

在 [1] 中:将 Pandas 导入为 pd
在 [2] 中:将 numpy 导入为 np
在 [3] 中:df = pd.DataFrame(np.random.rand(5,10))
在 [4] 中:df
出[4]:
0 1 2 3 4 5 6\
0 0.221466 0.375541 0.318416 0.695447 0.117332 0.836268 0.326673
1 0.439128 0.546087 0.380608 0.599192 0.553365 0.739253 0.984701
2 0.189311 0.799067 0.322041 0.171035 0.595848 0.106069 0.940712
3 0.971887 0.416420 0.945873 0.828611 0.055950 0.690107 0.526104
4 0.643041 0.800376 0.010879 0.677169 0.268208 0.151503 0.062268

7 8 9
0 0.690805 0.741308 0.487934
1 0.827711 0.284417 0.878259
2 0.734911 0.597626 0.256107
3 0.583156 0.398342 0.406590
4 0.247892 0.990469 0.611637

在 [8] 中:df.plot()

enter image description here

在 [9]: df.T.plot()

enter image description here

以下是DataFrame转置后的内容。

在 [10]:df.T
出[10]:
0 1 2 3 4
0 0.221466 0.439128 0.189311 0.971887 0.643041
1 0.375541 0.546087 0.799067 0.416420 0.800376
2 0.318416 0.380608 0.322041 0.945873 0.010879
3 0.695447 0.599192 0.171035 0.828611 0.677169
4 0.117332 0.553365 0.595848 0.055950 0.268208
5 0.836268 0.739253 0.106069 0.690107 0.151503
6 0.326673 0.984701 0.940712 0.526104 0.062268
7 0.690805 0.827711 0.734911 0.583156 0.247892
8 0.741308 0.284417 0.597626 0.398342 0.990469
9 0.487934 0.878259 0.256107 0.406590 0.611637

关于python - 如何使用 Pandas 在 x 轴上绘制列并使用索引作为 y 轴?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21473570/

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