在大熊猫

时间:2016-10-28 03:08:25

标签: python pandas matplotlib

我想要在子图中绘制5个时间序列。基本上我一直在使用子图:

fig, axes = plt.subplots(nrows=5, ncols=1, figsize=(16,10), sharex=True)
xlim = (start, end)
ax1=df.hr.plot(ax=axes[0], color='green', xlim=xlim)
ax2=df.act.plot(ax=axes[1], color='orange', xlim=xlim)
ax3=df.rr.plot(ax=axes[2], color='blue', xlim=xlim)
ax4=df2.set_index('timestamp').rmssd.plot(color='purple', ax=axes[3], xlim=xlim)
ax5=ma_df.tz_convert('US/Eastern')['any_act'].resample('10Min', how='count').plot(kind='line',ax=axes[4])

哪个产生

enter image description here

由于数据的性质,我想将最后一个子图可视化为条形图。很自然地,我把最后一行改为:

ax5=ma_df.tz_convert('US/Eastern')['any_act'].resample('10Min', how='count').plot(kind='bar',ax=axes[4])

然后创建下图:

enter image description here

其中,产生了我在最后一个子情节中的期望,但是使其他情节无用。不用说,这不是我想要的。

如何将4行时间序列与同一图表中的一个条形图相结合,但不同的子图表共享相同的x轴? 意思是我想要第一个图像中的前4个子图,以及第二个图像中的最后一个子图。

更新

我做了一个简单的例子,遗憾的是它按预期工作,并没有复制我的问题,这更令人费解。代码如下

import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline
df =  pd.read_csv('https://s3.amazonaws.com/temp-leonsas-qsaeamu0sl5v4b/df.csv')
bar_df = pd.read_csv('https://s3.amazonaws.com/temp-leonsas-qsaeamu0sl5v4b/bar_df.csv')
fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(16,10), sharex=True)
ax1=df.hr.plot(ax=axes[0], color='green', kind='line')
ax2=df.act.plot(ax=axes[1], color='orange', kind='line')
ax3=df.rr.plot(ax=axes[2], color='blue', kind='line')
ax4=bar_df.occ_count.plot(ax=axes[3], kind='bar')

而我的代码库中复制问题的代码是

fig, axes = plt.subplots(nrows=4, ncols=1, figsize=(16,10), sharex=True)
ax1=df.hr.plot(ax=axes[0], color='green', kind='line')
ax2=df.act.plot(ax=axes[1], color='orange', kind='line')
ax3=df.rr.plot(ax=axes[2], color='blue', kind='line')
ax4=bar_df.occ_count.plot(ax=axes[3], kind='bar')

主要区别在于我的代码库中正在生成DataFrame,而不仅仅是从s3加载。在DataFrame中是否有一个隐式配置可以以某种方式实现这一点?我只是使用df.to_csv将这2个数据帧转储到S3中。

1 个答案:

答案 0 :(得分:0)

我认为你只需要明确地将kind='line'传递给前三个图,这里有一个更简单的例子:

import matplotlib.pyplot as plt
import pandas as pd
%matplotlib inline
s = pd.Series([1,2,3,2,1])
fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(16,10), sharex=True)
s.plot(ax=axes[0], color='green', kind='line')
s.plot(ax=axes[1], color='red', kind='bar')

enter image description here