1

I'm new in using plotly and I'm trying to make a 2 different graph and show them individually through button; however, when I make it, the legends duplicated, resulting to a bad visualization of the data. Here's the code that I'm running right now:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import plotly as ply
import plotly.express as px
import plotly.graph_objects as go

url = "https://raw.githubusercontent.com/m23chaffee/DS100-Repository/main/Aluminum%20Alloy%20Data%20Set.csv"
alloy = pd.read_csv('https://raw.githubusercontent.com/m23chaffee/DS100-Repository/main/Aluminum%20Alloy%20Data%20Set.csv')
del alloy['temper']
alloy = alloy.rename(columns={'aluminum_alloy':'Alloy Number',
                              'modulus_elastic': 'Elastic Modulus',
                              'modulus_shear': 'Shear Modulus',
                              'strength_yield': 'Yield Strength',
                              'strength_tensile': 'Tensile Strength'
                             })

bar1 = px.bar(alloy,
            x = "Alloy Number",
            y = ["Elastic Modulus", "Shear Modulus","Yield Strength","Tensile Strength"],
            barmode = 'group',
            width = 1100,
            height =500,
            orientation = 'v',
            color_discrete_sequence = px.colors.qualitative.Pastel,
            labels={"value": "Data Values"},
            template = 'seaborn').update_traces(legendgroup="group").update_layout(showlegend=False)

line1 = px.line(alloy,
            x = "Alloy Number",
            y = ["Elastic Modulus", "Shear Modulus","Yield Strength","Tensile Strength"],
            width = 1100,
            height =500,
            orientation = 'v',
            color_discrete_sequence = px.colors.qualitative.Pastel,
            labels={"value": "Data Values"},
            template = 'seaborn').update_traces(legendgroup="group", visible = 'legendonly').update_layout(showlegend=False)

# Add buttom
fig.update_layout(
    updatemenus=[
        dict(
            type = "buttons",
            direction = "left",
            buttons=list([
                dict(
                    args=['type', 'bar'],
                    label="Bar Graph",
                    method="restyle",
                ),
                dict(
                    args=["type", "line"],
                    label="Line Graph",
                    method="restyle"
                )
            ]),
            pad={"r": 10, "t": 10},
            showactive=True,
            x=0.11,
            xanchor="left",
            y=1.1,
            yanchor="middle"
        ),
    ]
)

fig.show()

and the result of the image would look like this: Result of the code above

Attempted Solution

I tried to hide it using traces and in the documentation but it seems it didn't work out for me. I also found a similar stackoverflow post 8 years ago, tried it, and it didn't make any changes in my graph.

  • 1
    You havent defined fig in your code and gives error. Please update code. – Polatkan Polat Jan 11 '23 at 10:17
  • @PolatkanPolat what kind of fig should I put in? – Lynman Alpha Jan 11 '23 at 10:56
  • Please refer to the excellent [answer](https://stackoverflow.com/questions/26939121/how-to-avoid-duplicate-legend-labels-in-plotly-or-pass-custom-legend-labels) to this issue. – r-beginners Jan 11 '23 at 12:01
  • @r-beginners I did try to use it, as mention, but it didn't work on me, or I'm just hallucinating – Lynman Alpha Jan 11 '23 at 12:59
  • I'll run your code and add what's missing and share the legend with the duplicates removed in the comments. Colab's link expires when you view and comment on [this link](https://colab.research.google.com/drive/1GaEm-81hBEsUqCOUGIX0i_09-_V_Uvlf?usp=sharing). – r-beginners Jan 11 '23 at 13:36

0 Answers0