В верхнем субплоте не будет отображаться x меток в matplotlib - PullRequest
0 голосов
/ 29 апреля 2020

Я строю данные из фрейма в 3-х субплотах. Я хочу, чтобы метки х отображались на каждом из графиков. Однако, с моим кодом только два нижних графика показывают метки x, и я не понимаю, почему.

Код:

fig, axes = plt.subplots(3, 1, figsize=(11, 10))

ticks_to_use = df3.index[::5]
labels = [ i.strftime("%m/%d") for i in ticks_to_use ]

df3['Temp [Degrees_C]'].plot(ax=axes[0])
ax2 = axes[0].twinx()

color = 'tab:red'
ax2.set_ylabel(r'$\sigma_{\theta}$', color=color)  
df3['Cond [mS/cm]'].plot(ax=ax2, color=color)
ax2.tick_params(axis='y', labelcolor=color)

for i in range(3):
    axes[i].set_xticks(ticks_to_use)
    axes[i].set_xticklabels(labels)

axes[0].set_xlabel('time')
axes[0].set_ylabel('s1 and s2')
axes[0].grid(True)

df3['Salin [PSU]'].plot(ax=axes[1])
df3['Pres [deciBars]'].plot(ax=axes[2])

fig.tight_layout(pad=3.0)

И результат:

enter image description here

Что не так? Я думаю, что-то с twinx, но я не уверен.

РЕДАКТИРОВАТЬ:

df3 выглядит так:

    Temp [Degrees_C]    Cond [mS/cm]    Salin [PSU] Pres [deciBars]
yyyy-mm-ddThh:mm:ss.sss             
2020-01-27 22:00:59 14.470891   19.066957   14.510464   12.198908
2020-01-28 03:00:59 14.553947   19.301285   14.673590   12.481595
2020-01-28 08:00:59 14.501740   19.310037   14.700473   12.593718
2020-01-28 13:00:59 14.425415   18.531609   14.083557   12.626744
2020-01-28 18:00:59 14.414717   16.155998   12.134919   12.469164

Спасибо !!!

1 Ответ

0 голосов
/ 29 апреля 2020
Data
df3=pd.DataFrame({'time':['2020-01-27 22:00:59','2020-01-28 03:00:59','2020-01-28 08:00:59','2020-01-28 13:00:59','2020-01-28 18:00:59'], 'Temp [Degrees_C]':[14.470891,14.553947,14.501740,14.425415,14.414717],'Cond [mS/cm]':[19.066957,19.301285,19.310037,18.531609,16.155998],'Salin [PSU]':[14.510464,14.673590,14.700473,14.083557,12.134919],'Pres [deciBars]':[12.198908,12.481595,12.593718,12.626744,12.469164]})
df3

Пожалуйста, попробуйте

    fig, axes = plt.subplots(3, 1, figsize=(11, 10))
    ticks_to_use = df3.index[::5]
    labels = [ i.strftime("%m/%d") for i in ticks_to_use ]
    df3['Temp [Degrees_C]'].plot(ax=axes[0])
    ax2 = axes[2].twinx()
    color = 'tab:red'
    ax2.set_ylabel(r'$\sigma_{\theta}$', color=color)  
    df3['Cond [mS/cm]'].plot(ax=ax2, color=color)
    ax2.tick_params(axis='y', labelcolor=color)
    for i in range(3):
        axes[i].set_xticks(ticks_to_use)
        axes[i].set_xticklabels(labels)
    axes[0].set_xlabel('time')
    axes[0].set_ylabel('s1 and s2')
    axes[0].grid(True)

    df3['Salin [PSU]'].plot(ax=axes[1])
    df3['Pres [deciBars]'].plot(ax=axes[2])

    fig.tight_layout(pad=3.0)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...