I’ve played with an example at https://plot.ly/python/custom-buttons/ for plotting a Scatter plot with custom buttons to plot multiple traces and select which to show. The pattern used is to load everything and then show/hide using custom buttons to set a list with visibility set to True or False for each trace. I’m using offline mode.
I would like to instead have a button filter my dataset to a single trace and update the plot.
What arguments can I set to facilitate this?
For sake of discussion, I’ve included an example here. I’m working my way to control charts for a lab process, but for now I’m just trying to select which trace to display.
import plotly
import plotly.graph_objs as go
import pandas as pd
df = pd.read_csv('updated.csv')
filter_name = 'NameIsFoo'
filtered = df[df.name == filter_name]
metric = 'intensity'
trace = go.Scatter(
x=filtered['date'], y=filtered[metric], # Data
mode='lines', name=filter_name # Additional options
)
layout = go.Layout(
title=filter_name,
showlegend=True,
width=1200,
height=600,
font=dict(
family='Calibri, sans-serif',
size=18,
color='rgb(0,0,0)'),
xaxis = dict(
type='date',
linewidth=1,
title='Date',
tickangle=-45,
tickformat='%Y-%m-%d',
tickfont=dict(
size=12
)
),
yaxis = dict(
linewidth=1,
tickfont=dict(
size=12
)
)
)
fig = go.Figure(data=[trace], layout=layout)
plotly.offline.plot(fig, filename='line_chart')
I would like to use a custom button to re-define ‘data’ and replot.
I’ve tried something like the following (getTrace() returns a scatter object) but it doesn’t update the graph:
updatemenus=list([
dict(
buttons=list([
dict(label = 'Type1',
method = 'update',
args=[{'data' : [getTrace('Type1')]}]
),
dict(label = 'Type2',
method = 'update',
args=[{'data' : [getTrace('Type2')]}]
),
]),
direction = 'left',
pad = {'r': 10, 't': 10},
showactive = True,
type = 'buttons',
x = 0.1,
xanchor = 'left',
y = 1.1,
yanchor = 'top'
),
])
layout['updatemenus'] = updatemenus
fig = go.Figure(data=[getTrace('Type1')], layout=layout)
plotly.offline.plot(fig, filename='line_chart.html')