从多个数据帧绘制在matplotlib副区和规范轴系

问题描述 投票:0回答:1

我有蟒蛇,通过一个DateTime年有相同的列,但分离行11个的数据帧。这里是我的数据帧的两个例子:

_2008 = data[(data['fulldate'] > '2007-12-31') & (data['fulldate'] <= '2008-12-31')]
_2009 = data[(data['fulldate'] > '2008-12-31') & (data['fulldate'] <= '2009-12-31')]

从这些数据帧,我创建matplotlib图表看起来像这样:enter image description here

enter image description here

使用以下代码:

plt.plot('fulldate','num_recvd', data=_2009, color='black')
plt.title('Number of Requests in 2009')
plt.xlabel('Month', fontsize=10)
plt.ylabel('Number of Requests', fontsize=10)

与“数据= _2009”,因为从每一年的数据被包含在单独的数据帧取决于年份改变。

我想创建一个图形类似于此的次要情节:

enter image description here

但无法弄清楚如何(答案我已经寻找其他资源)这样做。

我也想给轴只在x轴和y轴相同的单位几个月规范(有些年份数可达〜47,而其他人只计数到1或2)。

随着第一个答案的代码我得到这样的情节:

enter image description here

python pandas matplotlib axis-labels subplot
1个回答
1
投票

您应该使用pyplot.subplots。下面是一些演示代码。这是干净多了,首先创建一个在您数据的字段year,然后由year和区组。

data.fulldate = pd.to_datetime(data.fulldate)
data['year'] = data.fulldate.dt.year

fig, axarr = plt.subplots(nrows=4, ncols=3, sharey=True)
axarr = axarr.flatten()

for i, (name, data_group) in enumerate(data.groupby('year')):
    ax = axarr[i]
    ax.plot('fulldate','num_recvd', data=data_group, color='black')
    ax.set_title('Number of Requests in ' + str(name))
    ax.set_xlabel('Month', fontsize=10)
    ax.set_ylabel('Number of Requests', fontsize=10)

注意在第一行的sharey = True:这意味着所有的y轴将共享相同的规模。

© www.soinside.com 2019 - 2024. All rights reserved.