Greetings!
I have a simple dashboard that displays costs in a go.bar plot, and updates the plot based on selecting/deselecting legend items.
Is it possible to display the total cost, based on which legend items are selected?
I’m envisioning a function that returns which traces/legends are selected, sums up numbers from the cost column in those traces, and displays the value somewhere on the plot.
An example of the behavior can be seen here, where the number of cellphone towers updates based on the selected radio type.
Thanks very much for your help!!!
Example code (from Jupyter Lab) is below and input data is here.
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import pandas as pd
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import plotly
# get data
df = pd.read_csv('dummy_model.csv')
# instantiate figure with subplots
fig = make_subplots(rows=4, cols=2,
subplot_titles=('title', 'cost', 'title', 'title'),
horizontal_spacing = .20,
specs=[[{"rowspan": 4}, {}],
[None, {"rowspan": 2}],
[None,None],
[None,{}]])
# populate traces automatically
hide = [] # instantiate a list to store existing legend items
# All Modalities plot
for index, row in df.iterrows(): # grab all of the proposals
show = row['proposal'] not in hide # Check if legend item has already been used
# Hide all legend items in subsequent plots
fig.add_trace(go.Scatter(name=row['proposal'], x=[row['PDA_modality']], y=[row['modality']],
mode='markers',
marker=dict(size=16,
color = 'darkgray', opacity = 0.5),
legendgroup=row['proposal'],
showlegend=show),
row=1, col=1)
hide.append(row['proposal']) # add legend item to list of existing legend items
# Cost plot, THIS IS THE PLOT WHERE I WOULD LIKE TO DISPLAY TOTAL COST
for index, row in df.iterrows():
fig.add_trace(go.Bar(name=row['proposal'], x=[row['PDA_cost']], y=[row['cost2']],
marker = dict(color = 'darkgray'),
legendgroup=row['proposal'],
showlegend=False),
row=1, col=2)
# Grouped modalities plot
for index, row in df.iterrows():
fig.add_trace(go.Scatter(name=row['proposal'], x=[row['PDA_modality']], y=[row['modality_groups']],
mode='markers',
marker = dict(size=16, color = 'darkgray', opacity = 0.5),
legendgroup=row['proposal'],
showlegend=False),
row=2, col=2)
# Cohorts plot
for index, row in df.iterrows():
fig.add_trace(go.Bar(name=row['proposal'], x=[row['PDA_modality']], y=[row['Cohorts_prosp']],
marker = dict(color = 'darkgray'),
legendgroup=row['proposal'],
showlegend=False,
#text = [row['Cohorts_prosp']],
textposition='outside',),
row=4, col=2)
# configure appearance of the plots
fig.update_layout(
barmode='stack',
height = 1300,
width = 2200,
title='Dashboard',
xaxis_title='x-axis',
yaxis_title='y-axis',
legend_title='title',
font=dict(
family="Courier New, monospace",
size=14,
color="RebeccaPurple"),
legend_title_font_size = 16)
# pass categorical axis labels to a list to hardcode the axis labels
# unfortunately, this setting changes the order, but only if there are actual values
y_categories = df['modality_list'].tolist()
fig.update_xaxes(overwrite = True, categoryorder='array', categoryarray= ['X', 'Y', 'Z'], row = 1, col = 1)
fig.update_xaxes(overwrite = True, categoryorder='array', categoryarray= ['X', 'Y', 'Z', 'Q', 'R'], row = 1, col = 2)
fig.update_xaxes(overwrite = True, categoryorder='array', categoryarray= ['X', 'Y', 'Z'], row = 2, col = 2)
fig.update_xaxes(overwrite = True, categoryorder='array', categoryarray= ['X', 'Y', 'Z'], row = 3, col = 2)
fig.update_yaxes(overwrite = True, range = [0,50000000], row = 1, col = 2)
fig.update_yaxes(overwrite = True, range = [0,3000], row = 3, col = 2)
# save plot to html
plotly.offline.plot(fig, filename='Dummy_dashboard.html')
fig.show()