Change names in legend using plotly.express

Is it possible to change the names in the legend without changing the columnames of the dataframe ?

import pandas as pd
import numpy as np
import plotly.express as px
df = pd.DataFrame({"DateTime": pd.date_range(start="2021-12-08", end="2021-12-12")})
df["A"] = np.random.randint(0, 10, size=len(df))
df["B"] = np.random.randint(0, 5, size=len(df))
fig = px.bar(df, x='DateTime', y=['A','B'],title="Title",barmode='relative')
fig.show()

image

HI @marvy, you could change the names manually like so:

fig.update_traces({'name': 'trace 1, aka A'}, selector={'name': 'A'})
fig.update_traces({'name': 'trace 2, aka B'}, selector={'name': 'B'})

creates:
newplot (39)

1 Like

Is there a way to do this using lists ? [β€˜A’,β€˜B’]

Hi @marvy, I don’t understand your question.