For the initial data:
from your posted code, your px.line draws a single line that can have a unique color, set by fig.update_traces(line_color="blue")
.
Each button updates x, y data for that line. To change the line color, you must add to the first button arg dict, "line.color": ["blue"]
, respectively "line.color": ["red"]
, to the second one.
Note, that in the button definition we do not write line_color="blue"
, because plotly.js does not understand that line_color
, representing line=dict(color)
, is a nested dict. For javascript we must pass it as "line.color": ["blue"]
.
Code for initial data, with updating line color by each button:
import plotly.express as px
import pandas as pd
import plotly.graph_objects as go
df1 = pd.DataFrame({'Mes': ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
'Vendas': [10000, 20000, 15000, 17000, 16000, 19000, 21000, 22000, 23000, 25000, 26000, 27000]
})
df2 = pd.DataFrame({
'Mes': ['Jan', 'Fev', 'Mar', 'Abr', 'Mai', 'Jun', 'Jul', 'Ago', 'Set', 'Out', 'Nov', 'Dez'],
'Vendas': [12000, 22000, 17000, 19000, 18000, 21000, 23000, 24000, 25000, 27000, 28000, 29000]
})
fig = px.line(df1, x='Mes', y='Vendas', title='Vendas por Mes')
fig.update_traces(line_color="magenta")
buttons = [
dict(label='Vendas - DF1',
method='restyle',
args=[{'x':[df1['Mes']], 'y':[df1['Vendas']], 'line.color':['magenta']},
{'title':'Vendas por Mes - DF1'}
]),
dict(label='Vendas - DF2',
method='update',
args=[{'x':[df2['Mes']], 'y': [df2['Vendas']], 'line.color':['red']},
{'title':'Vendas por Mes - DF1'}
]),
]
fig.update_layout(updatemenus=[dict(active=0, buttons=buttons),
],
yaxis_range=[9800, 29800])
When you define a new column, Categoria
, then fig=px_line()
contains as many traces as categories.
VERY IMPORTANT!!! In button definition you have to use exclusively attributes of go.TraceName. updatemenus is not aware of arguments in px.function(s). That’s why you got something unexpected when in button definition included ‘color’: df1['Categoria`].
In this case you have to groupby your dataframes by categories:
df1 = pd.DataFrame({
'Mes': ['Jan', 'Jan', 'Fev','Fev', 'Mar','Mar', 'Abr', 'Abr','Mai','Mai', 'Jun', 'Jun'],
'Categoria': ['A', 'B', 'A', 'B', 'A','B','A','B', 'A','B', 'A','B'],
'Vendas': [10000, 20000, 15000, 17000, 16000, 19000, 21000, 22000, 23000, 25000, 26000, 27000]
})
df2 = pd.DataFrame({
'Mes': ['Jan', 'Jan', 'Fev','Fev', 'Mar','Mar', 'Abr', 'Abr','Mai','Mai', 'Jun', 'Jun'],
'Categoria': ['A', 'B', 'A', 'B', 'A','B','A','B', 'A','B', 'A','B'],
'Vendas': [12000, 22000, 17000, 19000, 18000, 21000, 23000, 24000, 25000, 27000, 28000, 29000]
})
fig = px.line(df1, x='Mes', y='Vendas', title='Vendas por Mes', color='Categoria',text= 'Vendas')
dg1=df1.groupby(['Categoria']).get_group('A')
dg2=df1.groupby(['Categoria']).get_group('B')
dh1=df2.groupby(['Categoria']).get_group('A')
dh2=df2.groupby(['Categoria']).get_group('B')
buttons = [
dict(label='Vendas - DF1',
method='restyle',
args=[{'x': [dg1['Mes'], dg2['Mes']], 'y': [dg1['Vendas'], dg2['Vendas']], 'line.color': ['#636efa', 'c0c0c0']},
{'title':' Vendas por Mes - DF1'}
]),
dict(label='Vendas - DF2',
method='update',
args=[{'x':[dh1['Mes'], dh2['Mes']], 'y': [dh1['Vendas'], dh2['Vendas']], 'line.color': ['#EF553B', '#ffd700']},
{'title':'Vendas por Mes - DF1'}
]),
]
fig.update_layout(updatemenus=[dict(active=0, buttons=buttons)],
yaxis_range=[9800, 29800]
)