pandas 如何使用熊猫在 x 轴上绘制列并使用索引作为 y 轴?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/21473570/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to plot columns on x axis and use index as y axis using pandas?
提问by JAB
I have a data frame with 60 columns. I want the columns to be plotted on the X axis, and the index to be plotted on the y-axis.
我有一个包含 60 列的数据框。我希望将列绘制在 X 轴上,并将索引绘制在 y 轴上。
    df.plot() 
The above code puts the index on the x-axis by default. I am unable to work out how to switch the axes. Any help appreciated.
上面的代码默认将索引放在 x 轴上。我无法弄清楚如何切换轴。任何帮助表示赞赏。
回答by Nipun Batra
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

