使用 pandas 日期时间图时,如何使轴占据多个子图? [英] How to make an axes occupy multiple subplots when using pandas datetime plot?
本文介绍了使用 pandas 日期时间图时,如何使轴占据多个子图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个包含两行两列的(子)图,其中下一行的图同时占据两个轴.
由于我使用的是熊猫日期时间内的图(我认为),因此我无法使用
解决方案
您可以通过这种方式进行操作,例如:
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'])导入matplotlib.pyplot作为plt无花果= 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()
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:
解决方案
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()
这篇关于使用 pandas 日期时间图时,如何使轴占据多个子图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文