I have this simple dataframe.
import requests
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
# Intitialise data of lists
data = [{'Month': '2020-01-01', 'Expense':1000, 'ID':'123'},
{'Month': '2020-02-01', 'Expense':3000, 'ID':'123'},
{'Month': '2020-03-01', 'Expense':2000, 'ID':'123'},
{'Month': '2020-01-01', 'Expense':3000, 'ID':'456'},
{'Month': '2020-02-01', 'Expense':5000, 'ID':'456'},
{'Month': '2020-03-01', 'Expense':10000, 'ID':'456'},
{'Month': '2020-03-01', 'Expense':5000, 'ID':'789'},
{'Month': '2020-04-01', 'Expense':2000, 'ID':'789'},
{'Month': '2020-05-01', 'Expense':3000, 'ID':'789'}]
df = pd.DataFrame(data)
df
Based on unique IDs in the ID column, I am trying to crate a dropdown control.
# uniques = df['ID'].unique()
# for i in uniques:
# print(i)
I'm testing this code, and it looks pretty close, but it's not actually generating a chart for me.
uniques = df['ID'].unique()
# plotly
fig = go.Figure()
# set up ONE trace
fig.add_trace(go.Scatter(x=df.index,
y=df[df.columns[0]],
visible=True)
)
updatemenu = []
buttons = []
# button with one option for each dataframe
for i in uniques:
#print(i)
#df_single = df[df['ID']==i]
#data=df_single
#print(data)
buttons.append(dict(method='restyle',
label=i,
visible=True,
args=[{'y':[df['ID']==i],
'x':[df.index],
'type':'scatter'}]
)
)
# some adjustments to the updatemenus
updatemenu = []
your_menu = dict()
updatemenu.append(your_menu)
updatemenu[0]['buttons'] = buttons
updatemenu[0]['direction'] = 'down'
updatemenu[0]['showactive'] = True
# add dropdown menus to the figure
fig.update_layout(showlegend=False, updatemenus=updatemenu)
fig.show()
I am leveraging two resources for this.
Plotly: How to filter a pandas dataframe using a dropdown menu?
https://plotly.com/python/dropdowns/
Any idea what I'm doing wrong?