Regplot with year on X-axis (Python)

Asked

Viewed 54 times

0

I’m trying to plot a regression graph where the date interval is in year format, but I can’t centralize the data on the X axis. The chart range below is between 2016 and 2020.

plt.figure(figsize=(13,9))
ax = sns.regplot(data=dados_de_carga, x='data_ordinal', y='media_carga',scatter_kws={"color": "black"},line_kws={"color": "red"})

# Tighten up the axes for prettiness
ax.set_xlim(dados_de_carga['data_ordinal'].min() - 2, dados_de_carga['data_ordinal'].max() + 2)
ax.set_ylim(0, dados_de_carga['media_carga'].max() + 2)

ax.set_xlabel('')
new_labels = dados_de_carga['data_de_distribuicao'].dt.strftime('%Y')
ax.set_xticklabels(new_labels)
plt.ylabel('Média de carga')

inserir a descrição da imagem aqui

1 answer

0

The ax.set_xticklabels accepts as argument a vector with the Labels to be displayed on the x-axis, but if this vector is larger than the vector with the handles, then only the first names are used.

In your case, if you have the guarantee that the location of the points in the legend is correct, just do

newlabels = [str(ano) for ano in range(2016, 2021)]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.