[Python] Plotly's legend2 not allowing graphs full width

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I'm making a plotly chart with many hight lenth legends. Because of the number of legends and their lenth, i'm having issues with the legend automatic position property of plotly and i'm not having success on doing it by myself.

    Code:

    fig4 = go.Figure()
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['A'], name='Combustíveis e lubrificantes'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['B'], name='Hipermercados, supermercados, alimentos, bebidas e fumo'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['C'], name='Tecidos, vestuário e calçados'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['D'], name='Móveis e eletrodomésticos'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['E'], name='Artigos farmacêuticos, médicos, ortopédicos, de perfumaria e cosméticos'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['F'], name='Livros, jornais, revistas e papelaria'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['G'], name='Equipamentos e materiais para escritório, informática e comunicação'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['H'], name='Outros artigos de uso pessoal e doméstico'))

    fig4.update_layout(title={'text':'PMC: Pesquisa Mensal de Comércio - Nível SA', 'x':0.5, 'xanchor': 'center'},
    template='plotly_white',width=975,height=525,
    xaxis=dict(showticklabels=True, showgrid=True),
    yaxis=dict(range=[-100,100], ticksuffix="%"),
    legend=dict(y=-0.2, orientation='h'),
    )


    Output: [​IMG]

    What i want: [​IMG]

    I did some research and found out about "legend2" wich could separete the legends in 2 blocks, so then i could put them on the bottom sseparately. It didn't work. The code below is an edit of the code above where i tried to separate the legends.

    Code edited:

    fig4 = go.Figure()
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['A'], name='Combustíveis e lubrificantes'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['B'], name='Hipermercados, supermercados, alimentos, bebidas e fumo'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['C'], name='Tecidos, vestuário e calçados'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['D'], name='Móveis e eletrodomésticos'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['E'], name='Artigos farmacêuticos, médicos, ortopédicos, de perfumaria e cosméticos', legend='legend2'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['F'], name='Livros, jornais, revistas e papelaria', legend='legend2'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['G'], name='Equipamentos e materiais para escritório, informática e comunicação', legend='legend2'))
    fig4.add_trace(go.Scatter(x=df_mm3m_sa.index, y=df_mm3m_sa['H'], name='Outros artigos de uso pessoal e doméstico', legend='legend2'))
    fig4.update_layout(title={'text':'PMC: Pesquisa Mensal de Comércio - Nível SA', 'x':0.5, 'xanchor': 'center'},
    template='plotly_white',width=975,height=525,
    xaxis=dict(showticklabels=True, showgrid=True),
    yaxis=dict(range=[-100,100], ticksuffix="%"),
    legend=dict(yanchor='bottom', xanchor='left', y=-1, x=-0.5, orientation='h'),
    legend2=dict(yanchor='bottom', xanchor='right', y=-1, x=1.5, orientation='h'),
    margin=dict(pad=100),
    paper_bgcolor="LightSteelBlue" # Used for better visualization on the stackoverflow post
    )
    )


    Output edit: [​IMG]

    Continue reading...

Compartilhe esta Página