I have a dataframe like this:
import pandas as pd
df = pd.DataFrame()
df['category'] = ['G1', 'G1', 'G1', 'G1','G1', 'G1','G1', 'G2', 'G2', 'G2', 'G2', 'G2', 'G2', 'G2']
df['date'] = ['2012-04-01', '2012-04-05', '2012-04-09', '2012-04-11', '2012-04-16', '2012-04-23', '2012-04-30',
'2012-04-01', '2012-04-05', '2012-04-09', '2012-04-11', '2012-04-16', '2012-04-23', '2012-04-30']
df['col1'] = [54, 34, 65, 67, 23, 34, 54, 23, 67, 24, 64, 24, 45, 89]
df['col2'] = round(df['col1'] * 0.85)
I want to create a plotly figure that has one x (date) and 2 ys (col1 and col2). like this one, where the category dropdown button let's you select the category and updates the figure by filtering the col1 and col2 data for the selected category.
But I cannot make the dropdown to work and update the lines.
This is the code I tried:
# import plotly
from plotly.offline import init_notebook_mode, iplot, plot
import plotly.graph_objs as go
init_notebook_mode(connected=True)
x = 'date'
y1 = 'col1'
y2 = 'col2'
trace1 = {
'x': df[x],
'y': df[y1],
'type': 'scatter',
'mode': 'lines',
'name':'col 1',
'marker': {'color': 'blue'}
}
trace2={
'x': df[x],
'y': df[y2],
'type': 'scatter',
'mode': 'lines',
'name':'col 2',
'marker': {'color': 'yellow'}
}
data = [trace1, trace2]
# Create layout for the plot
layout=dict(
title='my plot',
xaxis=dict(
title='Date',
type='date',
tickformat='%Y-%m-%d',
ticklen=5,
titlefont=dict(
family='Old Standard TT, serif',
size=20,
color='black'
)
),
yaxis=dict(
title='values',
ticklen=5,
titlefont=dict(
family='Old Standard TT, serif',
size=20,
color='black'
)
)
)
# create the empty dropdown menu
updatemenus = list([dict(buttons=list()),
dict(direction='down',
showactive=True)])
total_codes = len(df.category.unique()) + 1
for s, categ in enumerate(df.category.unique()):
visible_traces = [False] * total_codes
visible_traces[s + 1] = True
updatemenus[0]['buttons'].append(dict(args=[{'visible': visible_traces}],
label='category',
method='update'))
updatemenus[0]['buttons'].append(dict(args=[{'visible': [True] + [False] * (total_codes - 1)}],
label='category',
method='update'))
layout['updatemenus'] = updatemenus
fig = dict(data = data, layout = layout)
iplot(fig)
I want make the category dropdown button with unique groups from category column, and selecting the category (either G1 or G2) will filter that data and plot the x and ys for this selected category.
I already looked at dropdown page on plotly website but couldn't make the dropdown to work.
