Hello. I am trying to plot a scatter graph offline that will show up in my browser. I would like to have a drop-down menu that will allow me to filter my data by a third column from my data frame that is associated with my two columns chosen for my x and y values in the scatter plot.
I am having 2 problems.
-
First, I am having trouble coming up with a way to build the list of values in the dropdown by unique values in the 3rd column that I would like to filter my data from. For example, I am trying build the names in the drop-down from a list assigned to a variable like this:
labelnames = list(df.name.unique())
-
Second, I am having trouble figuring out how to update my scatter based on what value would be selected from the drop-down menu.
Any help would be greatly appreciated!
Here is an example of the code I am working with so far:
import plotly.plotly as py
from plotly.graph_objs import *
import plotly.offline
import pandas as pd
my_dict = {
'name': ["a", "a", "c", "c", "c", "f", "g"],
'age': [20, 27, 35, 55, 18, 21, 35],
'score': [33, 11, 9, 12, 44, 15, 25]
}
df = pd.DataFrame(my_dict)
trace1 = Scatter(
y=df['age'],
x=df['score'],
name='Data Set 1',
mode='markers',
)
labelnames = list(df.name.unique())
data = Data([trace1])
layout = Layout(
updatemenus=list([
dict(
x=-0.05,
y=1,
buttons=list([
dict(
args=[],
options=[{'label': i, 'value': i} for i in labelnames],
value='',
label='Data Set 4',
method='restyle'
)
]),
yanchor='top'
)
]),
)
fig = Figure(data=data, layout=layout)
plotly.offline.plot(fig, filename='name.html')