对于在同一图表上绘制多个数据存在一些疑问,该图表是由参数明确指定的函数创建的。
def plot_freq(f):
p=my_data.set_index('Date').Items.str.count(f).sum(level=0).to_frame('Count').reset_index()
s2=p.sort_values('Count', ascending=False)
ax=p.plot(x="Date", y=["Count"], kind="line", figsize=(30,20), legend=False)
p.plot(ax=ax)
ax.set_xticklabels(ax.get_xticklabels(), ha='right')
return(p)
这通过指定关键搜索词f
来生成图。
这意味着如果我有一个数据集样本
Date Items
24/05/2020 Item_1
24/05/2020 Item_3
25/05/2020 Item_1
25/05/2020 Item_1
25/05/2020 Item_3
以此类推
它将按时间绘制项目,如下所示:
plot_freq('Item_1') for Item_1
plot_freq('Item_3') for Item_3
到目前为止。
我想在同一张图表上比较上面的图,但是不幸的是,我有不同的图。 为了做到这一点,我应该与项目数一样多。
能否请您告诉我如何达到预期的输出?
答案 0 :(得分:0)
要处理同一图形(图表),应创建图形。我看不到plt.figure()之后,您可以获取当前图形并对其执行操作。我想它将使您满意。
plt.gcf()方法(gcf对应于getcurrentfigure)
答案 1 :(得分:0)
您可以预先创建轴对象,然后重写plot_freq()
以将给定的轴作为要传递给p.plot()
的关键字
def plot_freq(f,ax=None):
p=my_data.set_index('Date').Items.str.count(f).sum(level=0).to_frame('Count').reset_index()
s2=p.sort_values('Count', ascending=False)
ax=p.plot(x="Date", y=["Count"], kind="line", figsize=(30,20), legend=False, ax=ax)
ax.set_xticklabels(ax.get_xticklabels(), ha='right')
return(p)
然后创建轴并将其作为参数传递给两个调用。
fig,ax = plt.subplots()
plot_freq('Item_1', ax=ax)
plot_freq('Item_3', ax=ax)