I am trying to make a plotly scatter plot with a OLS trendline and provide a dropdown menu what let's the user pick from a different of X's and Y's. The plot almost works. The code below updates the data but does not update the X and Y axes titles or the labels for the data. So when you hover over a given point, it always show the X variables as CRTTOTAL and Y variable as AVG_MISPERCEPTION.
The code:
import plotly.graph_objects as go
import plotly.express as px
#making a figure
fig = go.Figure()
x1 = df['crttotal']
x2 = df['nfcc_mean']
x3 = df['bficonmean']
x4 = df['bfiopmean']
y1 = df['avg_misperception_score']
y2 = df['avg_ambiguous_score']
# make figure
fig = px.scatter(df, x=x1, y=y1, trendline="ols", trendline_scope="overall", labels = {"crttotal":"crttotal", "avg_misperception_score":"avg_misperception_score"})
#making the dropdown
fig.update_layout(
updatemenus=[
go.layout.Updatemenu(
type="dropdown",
buttons=list([
dict(label="CRT vs Misperception",
method="update",
args=[{"x": [x1, px.scatter(x=x1, y=y1, trendline='ols').data[1].x],
"y": [y1, px.scatter(x=x1, y=y1, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "CRT vs Misperception"}]),
dict(label="NFCC vs Misperception",
method="update",
args=[{"x": [x2, px.scatter(x=x2, y=y1, trendline='ols').data[1].x],
"y": [y1, px.scatter(x=x2, y=y1, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "NFCC vs Misperception"}]),
dict(label="bficonmean vs Misperception",
method="update",
args=[{"x": [x3, px.scatter(x=x3, y=y1, trendline='ols').data[1].x],
"y": [y1, px.scatter(x=x3, y=y1, trendline='ols').data[1].y],
"trendline":["ols"], "trendline_scope":["overall"]},
{"title": "bficonmean vs Misperception"}]),
dict(label="bfiopmean vs Misperception",
method="update",
args=[{"x": [x4, px.scatter(x=x4, y=y1, trendline='ols').data[1].x],
"y": [y1, px.scatter(x=x4, y=y1, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "bfiopmean vs Misperception"}]),
### changing the y variable also
dict(label="CRT vs Ambiguity",
method="update",
args=[{"x": [x1, px.scatter(x=x1, y=y2, trendline='ols').data[1].x],
"y": [y2, px.scatter(x=x1, y=y2, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "CRT vs Ambiguity"}]),
dict(label="NFCC vs Ambiguity",
method="update",
args=[{"x": [x2, px.scatter(x=x2, y=y2, trendline='ols').data[1].x],
"y": [y2, px.scatter(x=x2, y=y2, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "NFCC vs Ambiguity"}]),
dict(label="bficonmean vs Ambiguity",
method="update",
args=[{"x": [x3, px.scatter(x=x3, y=y2, trendline='ols').data[1].x],
"y": [y2, px.scatter(x=x3, y=y2, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "bficonmean vs Ambiguity"}]),
dict(label="bfiopmean vs Ambiguity",
method="update",
args=[{"x": [x4, px.scatter(x=x4, y=y2, trendline='ols').data[1].x],
"y": [y2, px.scatter(x=x4, y=y2, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"]},
{"title": "bfiopmean vs Ambiguity"}])
])
)
]
)
#set the title
fig.update_layout(title="Dropdown")
fig.show()
The Data
crttotal nfcc_mean bficonmean bfiopmean avg_misperception_score \
0 3 2.87 3.875 3.000 -0.062
1 0 3.53 3.625 3.125 -0.235
2 0 3.80 4.000 3.000 0.077
3 0 3.73 3.750 3.500 0.067
4 2 3.87 3.125 3.000 0.368
5 0 3.47 2.750 3.500 -0.200
6 0 4.33 3.625 3.625 -0.200
7 0 4.13 3.250 3.125 -0.500
8 0 4.73 3.250 3.250 -0.643
9 3 5.20 3.750 2.750 0.000
avg_ambiguous_score
0 2.60
1 2.10
2 3.35
3 2.55
4 2.90
5 2.80
6 2.85
7 3.30
8 3.15
9 2.70
** What I've tried** Adding arguments to the buttons so that it updates the px.scatter funtion:
fig.update_layout(
updatemenus=[
go.layout.Updatemenu(
type="dropdown",
buttons=list([
dict(label="CRT vs Misperception", # button label
method="update",
args=[{"x": [x1, px.scatter(x=x1, y=y1, trendline='ols').data[1].x],
"y": [y1, px.scatter(x=x1, y=y1, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"],
** "labels":{"crttotal":"CRT",
** "avg_misperception_score":"Misperception"}},
{"title": "CRT vs Misperception"}]),
dict(label="NFCC vs Misperception",
method="update",
args=[{"x": [x2, px.scatter(x=x2, y=y1, trendline='ols').data[1].x],
"y": [y1, px.scatter(x=x2, y=y1, trendline='ols').data[1].y],
"trendline":["ols"],
"trendline_scope":["overall"],
},
{"title": "NFCC vs Misperception",
... ETC.