I am wondering if someone would be able to help me out. I have searched loads of answers on multiple website to get me where I am and I believe I am close to an answer but am struggling with the last bit.
I am trying to make an interactive graph using plotly express
I have a dataframe that looks like this:
| num | label | color | value1 | value 2 | value3 | |
|---|---|---|---|---|---|---|
| 0 | 1 | A | red | 0.4 | 4 | 40 |
| 1 | 2 | A | blue | 0.2 | 2 | 20 |
| 2 | 3 | A | green | 0.3 | 3 | 30 |
| 3 | 4 | A | red | 0.6 | 6 | 60 |
| 4 | 5 | A | blue | 0.7 | 7 | 70 |
| 5 | 6 | A | green | 0.4 | 4 | 40 |
| 6 | 7 | B | blue | 0.2 | 2 | 20 |
| 7 | 8 | B | green | 0.4 | 4 | 40 |
| 8 | 9 | B | red | 0.4 | 4 | 40 |
| 9 | 10 | B | green | 0.2 | 2 | 20 |
| 10 | 11 | C | red | 0.1 | 1 | 10 |
| 11 | 12 | C | blue | 0.3 | 3 | 30 |
| 12 | 13 | D | red | 0.8 | 8 | 80 |
| 13 | 14 | D | blue | 0.4 | 4 | 40 |
| 14 | 15 | D | green | 0.6 | 6 | 60 |
| 15 | 16 | D | yellow | 0.5 | 5 | 50 |
I would like to plot a graph in plotly that has two dropdown menu's, one that controls which value is being plotted and the other that controls which color is being plotted so for example if the dropdowns said: red and value1, I would get a bar graph of only that data.
My current code is as follow and it sort of works but I am getting values appearing on the graph when I change the color settings.
#Import the dataframe
df_test = pd.read_csv('test.csv')
#Plot a figure using plotly express
fig = px.bar(df_test, y="value1", )
#Create a button list for the value columns
buttonlist = []
for col in df_test.columns[3:]:
buttonlist.append(
dict(
args=['y',[df_test[str(col)]] ],
label=str(col),
method='restyle'
)
)
#Create a button list for the colors
buttonlist2 = []
for p in df_test['color'].unique():
buttonlist2.append(
dict(
args=['y',[df_test[df_test['color']==p]]],
label= 'Color ' + str(p),
method='restyle'))
#Update the layout to include the dropdown menus, and to show titles etc
fig.update_layout(title="Test",
yaxis_title="value",
xaxis_title="Color",
updatemenus = [
dict(buttons = buttonlist, showactive = True),
dict(buttons = buttonlist2, showactive = True, y = 0.9)
])
#Show the figure
fig.show()
Any help would be appreciated
Thanks
print(df_test.to_markdown())and paste markdown into question