用matplotlib绘制 pandas period_range-设置轴的频率 [英] plot pandas period_range with matplotlib - set freq of axis

查看:190
本文介绍了用matplotlib绘制 pandas period_range-设置轴的频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个带有可变长度的时间跨度值的pandas DataFrame.我想用时间跨度的长度为每个时间跨度的值绘制一条水平线.因此,我通过以下方式将时间跨度转换为PeriodIndex:

I got a pandas DataFrame with values for time spans with variable lenths. I want to plot a horizontal line for the value of each timespan with the length of the timespan. Therfore, I converted the timespan to an PeriodIndex by

ax0=fig.add_axes([.16,.2,0.8,.7])
ax0.plot_date(data['periodIndex'], data['value'])

产生:

TypeError: Axis must have `freq` set to convert to Periods

所以我的问题是如何设置x轴的频率

so my Question is how to set the frequency of my xaxis

非常感谢您的帮助

更新

这是我要绘制的pandas dataFrame的一部分

this is part of the pandas dataFrame I want to plot

     start        stop          A           B          C
ID                                                                    
1    2015-08-17   2015-08-25    -5.684246  -34.759047  10.714920   
2    2015-08-25   2015-09-02   -10.850721  -78.443556   8.362215   
3    2015-09-02   2015-09-15    -7.139351  -47.814199   9.300612   
4    2015-09-15   2015-09-21    -7.822367  -56.895241   5.683696   
5    2015-09-21   2015-09-29    -6.491543  -42.947744   8.984596   
6    2015-09-29   2015-10-05    -9.897980  -67.985256  11.198583  

我解决了问题,现在看起来 通过以下代码

I solved the problem and now it looks like this by the following code

firstDate=df.ix[df.index[0]]['start']

fig=plt.figure(figsize=(9*1.5,6*1.5))
ax0=fig.add_axes([.15,.2,0.75,.7])

ax0.set_xticks(df['start_abs'])
ax0.set_xticklabels([i.date() for i in df['start']], rotation='vertical')

B=ax0.twinx()
B.set_ylim(-200,0)

for _index, _series in df.iterrows():
    start=_series['start']
    stop=_series['stop']   
    df.ix[_index, 'start_abs']=(start-firstDate).days
    df.ix[_index, 'width_abs']=(stop-start).days
    df.ix[_index, 'stop_abs']=(stop-firstDate).days

    line_A=ax0.hlines(y=df.ix[_index,'A'], xmin=df.ix[_index, 'start_abs'], xmax=df.ix[_index, 'stop_abs'], colors='r')
    line_A.set_linewidths(3)
    text_A=ax0.text(df.ix[_index, 'start_abs'], df.ix[_index,'A'], '{a:4.2f}'.format(a=df.ix[_index,'A']), fontsize=8, color='r')

    line_B=B.hlines(y=df.ix[_index,'B'], xmin=df.ix[_index,'start_abs'], xmax=df.ix[_index,'stop_abs'], colors='k')
    line_B.set_linewidths(3)
    text_B=B.text(df.ix[_index,'start_abs'], df.ix[_index,'B'], '{a:4.2f}'.format(a=df.ix[_index,'B']), fontsize=8, color='k')

plt.show()

推荐答案

我构建了数据来证明我认为是您的问题.

I constructed data to demonstrate what I believe was your question.

如果仅在特定范围的索引中用数据填充数据框,然后对其进行绘制,则只会在该数据不为空时生成一行.

If you fill in a dataframe with data for only a specific range of the index and then plot it, it will only generate a line for when that data was not null.

import pandas as pd
import numpy as np

ts = pd.date_range(end='2016-03-31', periods=250, freq='D')

np.random.seed([3,1415])
ranges = pd.DataFrame((np.random.choice(xrange(100), (2, 10)) +
                       np.array([[0], [150]])),
                      columns=list('ABCDEFGHIJ'))

df = pd.DataFrame(index=ts)
count = 1
for i, c in ranges.iteritems():
    s, e = c
    df.loc[ts[s]:ts[e], i] = count
    count += 1

df.plot()

这篇关于用matplotlib绘制 pandas period_range-设置轴的频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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