Pandas 将子地块组合成一个图形

Pandas 将子地块组合成一个图形,pandas,dataframe,matplotlib,plot,visualization,Pandas,Dataframe,Matplotlib,Plot,Visualization,我在理解Pandas子图以及如何创建轴以显示所有子图(而不是被后续绘图覆盖)时遇到困难 对于每个“站点”,我想对数据帧中的所有列绘制一个时间序列图 # the grouper function is from itertools' cookbook from itertools import zip_longest def grouper(iterable, n, fillvalue=None): "Collect data into fixed-length chunks or blo

我在理解Pandas子图以及如何创建轴以显示所有子图(而不是被后续绘图覆盖)时遇到困难

对于每个“站点”,我想对数据帧中的所有列绘制一个时间序列图

# the grouper function is from itertools' cookbook
from itertools import zip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)


fig, axs = plt.subplots(len(df.Site.unique())*(len(df.columns)-1),1, sharex=True)
for (site,sitedat),axList in zip(df.groupby('Site'),grouper(axs,len(df.columns)-1)):
    sitedat.plot(subplots=True, ax=axList)
    axList[0].set_title(site)
plt.tight_layout()
这里的“站点”是“鲨鱼”和“独角兽”,都有两个变量。输出应为4条绘制线-每个站点上Var 1和Var 2的时间索引图

使用NAN生成时间索引数据:

df = pd.DataFrame({ 

    # some ways to create random data
    'Var1':pd.np.random.randn(100),
    'Var2':pd.np.random.randn(100),
    'Site':pd.np.random.choice( ['unicorn','shark'], 100),

    # a date range and set of random dates
    'Date':pd.date_range('1/1/2011', periods=100, freq='D'),
#     'f':pd.np.random.choice( pd.date_range('1/1/2011', periods=365, 
#                           freq='D'), 100, replace=False) 
    })
df.set_index('Date', inplace=True)
df['Var2']=df.Var2.cumsum()
df.loc['2011-01-31' :'2011-04-01', 'Var1']=pd.np.nan
fig, ax = plt.subplots(len(df.Site.unique()), 1)
counter=0
for site in df.Site.unique():
    print(site)
    sitedat=df[df.Site==site]
    sitedat.plot(subplots=True, ax=ax[counter], sharex=True)
    ax[0].title=site #Set title of the plot to the name of the site
    counter=counter+1
plt.show()
为每个站点绘制一个带有子图的图形:

df = pd.DataFrame({ 

    # some ways to create random data
    'Var1':pd.np.random.randn(100),
    'Var2':pd.np.random.randn(100),
    'Site':pd.np.random.choice( ['unicorn','shark'], 100),

    # a date range and set of random dates
    'Date':pd.date_range('1/1/2011', periods=100, freq='D'),
#     'f':pd.np.random.choice( pd.date_range('1/1/2011', periods=365, 
#                           freq='D'), 100, replace=False) 
    })
df.set_index('Date', inplace=True)
df['Var2']=df.Var2.cumsum()
df.loc['2011-01-31' :'2011-04-01', 'Var1']=pd.np.nan
fig, ax = plt.subplots(len(df.Site.unique()), 1)
counter=0
for site in df.Site.unique():
    print(site)
    sitedat=df[df.Site==site]
    sitedat.plot(subplots=True, ax=ax[counter], sharex=True)
    ax[0].title=site #Set title of the plot to the name of the site
    counter=counter+1
plt.show()
然而,这并不是书面的。第二个子图最终覆盖第一个子图。在我的实际用例中,每个数据帧中有14个可变数量的站点,以及可变数量的“Var1,2,…”。因此,我需要一个不需要手动创建每个轴(ax0、ax1等)的解决方案

作为奖励,我希望在这组情节上面有一个每个“站点”的标题

当前代码将第一个“站点”绘图覆盖到第二个。我用斧头少了什么?!

使用时,需要为每列提供正确数量的轴(如果使用
布局=
,则需要使用正确的几何图形)。在您的示例中,您有两列,因此
plot()
需要两个轴,但在
ax=
中只传递一个轴,因此pandas别无选择,只能删除所有轴并创建适当数量的轴

因此,需要传递一个长度与数据帧中的列数相对应的轴数组

# the grouper function is from itertools' cookbook
from itertools import zip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)


fig, axs = plt.subplots(len(df.Site.unique())*(len(df.columns)-1),1, sharex=True)
for (site,sitedat),axList in zip(df.groupby('Site'),grouper(axs,len(df.columns)-1)):
    sitedat.plot(subplots=True, ax=axList)
    axList[0].set_title(site)
plt.tight_layout()