Subplot Matplotlib Как настроить оси X временных рядов? - PullRequest
1 голос
/ 26 мая 2020

Я хотел бы объединить 2 графика на одном рисунке (2 строки). когда я показываю это отдельно, нет проблем с осями x (временными рядами), но когда я складываю их вместе, следуя приведенному ниже коду, оси x, которых я не вижу, теперь, в чем ошибка: 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()

результат

enter image description here

Решение: я добавил код ниже, поместив предыдущий код в комментарий:

# 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")))

1 Ответ

1 голос
/ 26 мая 2020

Команда plt.xticks() работает с текущей осью.

Вы можете выполнять итерацию по осям:

for ax in (ax1,ax2): 
    ax.tick_params(axis='x',labelrotation=90) 
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...