How to make an axes occupy multiple subplots when using pandas datetime plot?

感情迁移 提交于 2021-02-10 16:01:19

问题


I would like to create a (sub)plot with two rows and two columns where the plot in the lower row occupies both axes.

Since I use the plot from within pandas datetime (I think) I was not able to use 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 ??? ) 

How do I need to assign the axes (if at all possible) in order to get something like this:


回答1:


You can do in this way for example:

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()




回答2:


For completeness only I am posting the code-snippet which resulted from the help of stackoverflow. The datetime of the two sensor readings is not synchronized, so I could not plot as suggested.

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()



来源:https://stackoverflow.com/questions/51606524/how-to-make-an-axes-occupy-multiple-subplots-when-using-pandas-datetime-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!