简体   繁体   English

如何使用熊猫在x轴上绘制列并将索引用作y轴?

[英]How to plot columns on x axis and use index as y axis using pandas?

I have a data frame with 60 columns. 我有一个60列的数据框。 I want the columns to be plotted on the X axis, and the index to be plotted on the y-axis. 我希望将列绘制在X轴上,将索引绘制在y轴上。

    df.plot() 

The above code puts the index on the x-axis by default. 上面的代码默认情况下将索引放在x轴上。 I am unable to work out how to switch the axes. 我无法解决如何切换轴的问题。 Any help appreciated. 任何帮助表示赞赏。

Maybe you can plot the DataFrame transposed. 也许您可以绘制转置的DataFrame。

Creating a DataFrame with 10 columns and 5 rows. 创建一个具有10列5行的DataFrame。

In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: df = pd.DataFrame(np.random.rand(5,10))
In [4]: df
Out[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 

In [8]: df.plot()

在此处输入图片说明

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

在此处输入图片说明

The following are the contents of the DataFrame transposed. 以下是转置的DataFrame的内容。

In [10]: df.T
Out[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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM