I would like to combine 2 plots in the same figure (2 lines ). when I show it separately, there is no problem with x-axes (time series ) but when I put them together following the code below, the x-axes i don't now where is the mistake: code:
fig, (ax2, ax1) = plt.subplots(2)
fig.suptitle('covid-19 Madagasikara')
# first axe
# pour les axes
sns.set(rc={'figure.figsize':(30,25)},palette=['#F70A0A','#2A930C','#930C85'], font_scale=1.7)
ax1 = sns.barplot(x=df_final_seaborn.index,y='Isan\'ny olona',data=df_final_seaborn,hue='Covid-19 Madagascar' , ax = ax1)
sns.set_style("darkgrid" , {"ytick.major.size": 10 , "ytick.minor.size": 2 , 'grid.linestyle': '--'})
# X-axis for first figure
ax1.xaxis.set_major_formatter(plt.FixedFormatter(df_sea.index.to_series().dt.strftime("%Y-%m-%d")))
for patch in ax1.patches:
x , width , height = patch.get_x(),patch.get_width(),patch.get_height()
color = patch.get_facecolor()
#ignore . and nan values
if height is None or height ==0:continue
ax1.text(x+width/2,height+0.1,height.astype(int),ha='center',color=color)
# ------------------------------second figure--------------------------
# pour les axes
sns.set(rc={'figure.figsize':(30,25)},palette=['#0524C1','#F70A0A'], font_scale=1.7)
ax2 = sns.barplot(x=df_final_seaborn_with_test.index,y='Isan\'ny olona',data=df_final_seaborn_with_test,hue='Covid-19 Madagascar' , ax=ax2)
sns.set_style("darkgrid" , {"ytick.major.size": 10 , "ytick.minor.size": 2 , 'grid.linestyle': '--'})
# X-axis for second figure
ax2.xaxis.set_major_formatter(plt.FixedFormatter(df_test.index.to_series().dt.strftime("%Y-%m-%d")))
for patch in ax2.patches:
x , width , height = patch.get_x(),patch.get_width(),patch.get_height()
color = patch.get_facecolor()
#ignore . and nan values
if height is None or height ==0:continue
ax2.text(x+width/2,height+0.1,height.astype(int),ha='center',color=color)
plt.xticks(rotation=90)
plt.xlabel('Daty', fontsize = 20)
plt.ylabel('Isan\'ny olona', fontsize = 20)
plt.minorticks_on()
plt.legend(loc='upper left')
plt.grid(b=True, which='minor', color='#999999', linestyle='-', alpha=0.2 , axis='y')
plt.show()
the result
Solution: I added the code below by putting the previous code in comment:
# X-axis
for ax in (ax1 ,ax2):
ax.tick_params(axis='x',labelrotation=90)
ax.xaxis.set_major_formatter(plt.FixedFormatter(df_sea.index.to_series().dt.strftime("%Y-%m-%d")))
The command plt.xticks()
operates on the current axis.
You can iterate over the axes though:
for ax in (ax1,ax2):
ax.tick_params(axis='x',labelrotation=90)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.