使用熊猫日期时间图时,如何使轴占据多个子图?

时间:2018-07-31 06:15:02

标签: python pandas datetime matplotlib subplot

我想创建一个包含两行两列的(子)图,其中下一行的图占据两个轴。

由于我使用的是熊猫日期时间内的图(我认为),因此我无法使用this solution

fig, axes = plt.subplots(nrows=2, ncols=2)

df1.set_index('Date').plot(ax=axes[0,0])
df2.set_index('Date').plot(ax=axes[0,1])
df3.set_index('Date').plot(ax=axes ??? ) 

如何获得轴(如果可能的话)以便获得如下信息:

enter image description here

2 个答案:

答案 0 :(得分:2)

您可以通过这种方式进行操作,例如:

df = pd.DataFrame( {'date':['2010-01-01','2010-01-02','2010-01-03'],'a1':[5,4,3],'a2':[6,2,9]})
df['date'] = pd.to_datetime(df['date'])
import matplotlib.pyplot as plt
fig = plt.figure()

ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(212)

df.plot(x="date", y='a1',ax=ax1)
df.plot(x="date", y='a2',ax=ax2)
df.plot(x="date", y=['a1','a2'], ax=ax3)

plt.tight_layout()
plt.show()

enter image description here

答案 1 :(得分:0)

仅出于完整性考虑,我将发布由stackoverflow帮助产生的代码段。两个传感器读数的日期时间不同步,因此无法按照建议进行绘制。

fig = plt.figure()

ax1 = plt.subplot(221)
ax2 = plt.subplot(222)
ax3 = plt.subplot(212)

df1.plot(x='Date', y='series1',ax=ax1)
df2.plot(x='Date', y='series2',ax=ax2)
df1.plot(x='Date', y='series1',ax=ax3)
df2.plot(x='Date', y='series2',ax=ax3)

plt.tight_layout()
plt.show()

enter image description here