Doubt Column Label - Seaborn Python

Asked

Viewed 142 times

0

I have a Doubt when Plotting a chart in Seaborn, where the label of the Axis of Column is very large Como está agora

However, I would like to "break" this line in 2, or 3 Lines, to get smaller the label box and give greater emphasis to the graphic

How can I do that

pais = base_depara['Dono'].unique()
print(pais)

for pai in pais:
    img_email = base_depara.loc[base_depara['Dono'] == pai, ['grupo','Tt_casos', 'criados_d3', 'criados_d1']]
    
    altura = []
    for i in img_email['Tt_casos']:
       altura.append(i)
    posicao = []
    for i in range(len(img_email['grupo'])): 
       posicao.append(i)
   #criando uma figure, axes, alterando tamanho
    fig, ax = plt.subplots(figsize=(8,6))
    #criando o gráfico de barras
    sns.barplot(x=img_email['grupo'], y=img_email['Tt_casos'], ax=ax, data=img_email,  palette='RdPu_r')
    #adicionando título
    ax.set_title("quantidade x ano", fontdict={'fontsize':15})
    #mudando e nome e tamanho do label x
    ax.set_xlabel('Anos', fontdict={'fontsize':14})
    #mudando tamanho do label eixo y
    ax.set_ylabel('')
    #mudando tamanho dos labels dos ticks
    ax.tick_params(labelsize=14)
    #aumentando espessura linha inferior
    ax.spines['bottom'].set_linewidth(2.5)
    #remoção dos outros três axis
    for axis in ['top', 'right', 'left']:
       ax.spines[axis].set_color(None)
    #remoção dos ticks
    ax.tick_params(axis='y', labelleft=False, left=None)                  
    #Colocando a quantidade em cada barra
    for i in range(len(img_email)):
       ax.text(x=posicao[i]-0.2, y=altura[i]+0.2, s=str(altura[i]),   
               fontsize=15)
    #otimizar espaço da figure
    plt.xticks(rotation=90)
    fig.tight_layout();
  • can make the data available or create an MWE?

1 answer

0

I believe there are several ways, but unfortunately it seems that Seaborn does not implement this.

Replacing space with \n would be an option

ml_labels = [label.replace(' ', '\n') for label in labels]

It seems that the textwrap also works

from textwrap import wrap

ml_labels = ['\n'.join(wrap(label, 15)) for label in labels]

Note: this will cut the label into the 15th character

It seems that the dexplot also does this, but I have not tested.

Browser other questions tagged

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