如何绘制按子索引对齐的Pandas数据框列? [英] How to barplot Pandas dataframe columns aligning by sub-index?

查看:54
本文介绍了如何绘制按子索引对齐的Pandas数据框列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据框,df包含两只股票的财务比率数据:

I have a pandas dataframe df contains two stocks' financial ratio data :

>>> df
                  ROIC    ROE
STK_ID RPT_Date              
600141 20110331  0.012  0.022
       20110630  0.031  0.063
       20110930  0.048  0.103
       20111231  0.063  0.122
       20120331  0.017  0.033
       20120630  0.032  0.077
       20120930  0.050  0.120
600809 20110331  0.536  0.218
       20110630  0.734  0.278
       20110930  0.806  0.293
       20111231  1.679  0.313
       20120331  0.666  0.165
       20120630  1.039  0.257
       20120930  1.287  0.359

然后我尝试绘制比率'ROIC'&股票'600141'和'股票的'ROE'将"600809"放在同一个"RPT_Date"上以对它们的性能进行基准测试.

And I try to plot the ratio 'ROIC' & 'ROE' of stock '600141' & '600809' together on the same 'RPT_Date' to benchmark their performance.

df.plot(kind='bar')给出以下内容

该图表在左侧绘制"600141",在右侧绘制"600809".比较"ROIC"和相同报告日期'RPT_Date'的两只股票的"ROE".

The chart draws '600141' on the left side , '600809' on the right side. It is somewhat inconvenience to compare the 'ROIC' & 'ROE' of the two stocks on same report date 'RPT_Date' .

我想要的是放入'ROIC'&在同一组中并排的相同"RPT_Date"索引的"ROE"条(每组4条),x轴仅标记"RPT_Date",这将清楚地表明两只股票的区别.

What I want is to put the 'ROIC' & 'ROE' bar indexed by same 'RPT_Date' in same group side by side ( 4 bar per group), and x-axis only labels the 'RPT_Date', that will clearly tell the difference of two stocks.

该怎么做?

如果我df.plot(kind='line'),它只显示两行,但应该是四行(2股* 2比率):

And if I df.plot(kind='line') , it only shows two lines, but it should be four lines (2 stocks * 2 ratios) :

这是一个错误,还是我可以做些什么来纠正它?谢谢.

Is it a bug, or what I can do to correct it ? Thanks.

我正在使用Pandas 0.8.1.

I am using Pandas 0.8.1.

推荐答案

如果取消STK_ID的堆叠,则可以按照RPT_Date创建并排图.

If you unstack STK_ID, you can create side by side plots per RPT_Date.

 In [55]: dfu = df.unstack("STK_ID")

 In [56]: fig, axes = subplots(2,1)

 In [57]: dfu.plot(ax=axes[0], kind="bar")
 Out[57]: <matplotlib.axes.AxesSubplot at 0xb53070c>

 In [58]: dfu.plot(ax=axes[1])
 Out[58]: <matplotlib.axes.AxesSubplot at 0xb60e8cc>

这篇关于如何绘制按子索引对齐的Pandas数据框列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆