简体   繁体   English

熊猫用多个索引标签绘图

[英]Pandas plotting with multiple index labels

I have a pandas DataFrame that looks something like this: 我有一个像这样的pandas DataFrame:

n=pd.DataFrame({1:[1,2,np.NAN,4],2:[5,6,5,8],'info':[5,6,8,7],'moreinfo':[1,2,5,8]}).set_index(['info','moreinfo'])

Which looks like this: 看起来像这样:

                       1    2
info    moreinfo        
5       1            1.0    5
6       2            2.0    6
8       5            NaN    5
7       8            4.0    8

How can I line plot this such that: 我该如何绘制这样的图:

The column labels are used as the xlabels 列标签用作xlabels
The data is grouped and colored by index 数据按索引分组和着色
The y-axis is the numerical data found within each column. y轴是在每列中找到的数值数据。

Below is an example of what I'm thinking the plot should look like: 下面是我认为情节应该是这样的例子:

在此输入图像描述

Let's try: 我们试试吧:

ax = n.T.plot(marker='o')
ax.set_xticks(n.T.index.astype(int))

Output: 输出:

在此输入图像描述

You might want to fill that NaN with zero. 您可能希望用零填充NaN。

ax = n.fillna(0).T.plot(marker='o')
ax.set_xticks(n.T.index.astype(int))

Output: 输出: 在此输入图像描述

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

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