[英]pandas dataframe : subplot how to adjust legend for each figures?
I have 2 dataframes df1 and df2 with column date as index:我有 2 个数据框 df1 和 df2 以列日期作为索引:
df1 = {'Confirmed':[3,0,0,9],
'Date' : ['2020-03-20','2020-03-21','2020-03-22','2020-03-23'],
'Recovered' : [0,0,1,2]
}
# ratio Recovered / Confirmed
df2 = { 'Ratio': [0.00,2.77,2.43,7.95],
'Date' : ['2020-03-20','2020-03-21','2020-03-22','2020-03-23']
}
when I plot them separately:当我 plot 将它们分开时:
# plot df1 bar
ax = df1.plot(
kind='bar' , figsize=(20,7), grid=True , legend= False,
color=('r','g'),fontsize=15,
secondary_y=False,mark_right=False,
title = 'Covid-19 Madagascar',
yticks = range (0,30,2)
)
ax.xaxis.set_major_formatter(plt.FixedFormatter(df1.index.to_series().dt.strftime("%Y-%m-%d")))
ax.set_ylabel('Isan\'ny olona',fontsize=15)
ax.set_yticklabels("" , minor =True)
# ax.yaxis.set_major_locator(plt.MinNLocator(1))
plt.legend(loc='upper center')
plt.show()
the result:结果:
now for df2:现在为df2:
# plot second Ratio line
ax = df2.plot(
kind='line' , figsize=(20,7), grid=True , legend= False,
color=('b'),fontsize=15,
mark_right=False,
title = 'Covid-19 Madagascar'
# yticks = range (0,1)
)
# ax.xaxis.set_major_formatter(plt.FixedFormatter(df2.index.to_series().dt.strftime("%Y-%m-%d")))
ax.set_ylabel('Isan-jato %',fontsize=15)
ax.set_yticklabels("" , minor =True)
# ax.yaxis.set_major_locator(plt.MinNLocator(1))
plt.legend(loc='upper center')
plt.show()
the result is:结果是:
as you can see when i plotted separately, the result is ok.正如您在我单独绘制时看到的那样,结果还可以。 But when I would like to add those figures in one by using subplot, I have a problem with the legend position, it hides the title.但是当我想通过使用子图将这些数字添加到一个中时,我对图例 position 有疑问,它隐藏了标题。
fig , axes = plt.subplots(ncols=1,nrows=2,constrained_layout=True)
# -------------------------- plot first bar -------------------------------------------
ax = df1.plot(
ax = axes[0],
kind='bar' , figsize=(20,15) , grid=True , legend= False,
color=('r','g'),fontsize=15,
secondary_y=False,mark_right=False,
title = 'Covid-19 Madagascar',
yticks = range (0,30,2)
)
ax.xaxis.set_major_formatter(plt.FixedFormatter(df1.index.to_series().dt.strftime("%Y-%m-%d")))
ax.set_ylabel('Isan\'ny olona',fontsize=17)
ax.set_yticklabels("" , minor =True)
# plt.legend(loc='upper left')
# handles, labels = ax.get_legend_handles_labels()
fig.legend(loc='upper center',prop={'size':15})
# --------------------- plot second line -----------------------------
ax = df2.plot(
ax = axes[1],
kind='line' , figsize=(20,15) ,grid=True , legend= False,
color=('b'),fontsize=15,
mark_right=False,
)
# ax.xaxis.set_major_formatter(plt.FixedFormatter(df2.index.to_series().dt.strftime("%Y-%m-%d")))
ax.set_ylabel('Isan-jato %',fontsize=17)
ax.set_yticklabels("" , minor =True)
plt.legend(loc='upper center',prop={'size':15})
plt.show()
how can we do to duplicate the same figures as separately with subplot我们如何使用 subplot 单独复制相同的数字
You can move the legend with;您可以移动图例;
ax.legend(bbox_to_anchor=(1.0, 0.75))
Play with the numbers until it's where you want.玩数字,直到你想要的地方。
Or you could pass the relative coordinates as the loc
parameter, instead of 'upper center'
:或者您可以将相对坐标作为loc
参数传递,而不是'upper center'
:
fig.legend(loc=(0.47, 0.92), prop={'size':15})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.