I am trying to have my plotly show different variables with a dropdown menu. The dropdown should show the “deathsper100k” for both “DEM” & “REPS” while the other option should show the same but for “casesper100k”.
However, I am unable to add the 2nd dataset using the add_trace function.
How do I add the plotly.express boxplot into the figure and show them in the different dropdown menus?
# Joint Boxplot
fig = px.box(uscovidinfo, y = "deathsper100k", x = "party", points = "all", hover_name="County Name_x",
color = "party", color_discrete_map={'DEM':'blue', 'REP':'red'})
data2 = px.box(uscovidinfo, y = "casesper100k", x = "party", points = "all", hover_name="County Name_x",
color = "party", color_discrete_map={'DEM':'blue', 'REP':'red'})
fig.add_trace(data2)
fig.update_layout(
updatemenus=[
dict(
active=0,
buttons=list([
dict(label="Both",
method="update",
args=[{"visible": [True, True]},
{"title": "Both"}]),
dict(label="Deaths",
method="update",
args=[{"visible": [True, False]},
{"title": "Deaths",
}]),
dict(label="Cases",
method="update",
args=[{"visible": [False, True]},
{"title": "Cases",
}]),
]),
)
])
fig.show()
Below is the error I got
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-101-e7ac1d928623> in <module>
6 color = "party", color_discrete_map={'DEM':'blue', 'REP':'red'})
7
----> 8 fig.add_trace(data2)
9
10 fig.update_layout(
~/opt/anaconda3/lib/python3.8/site-packages/plotly/basedatatypes.py in add_trace(self, trace, row, col, secondary_y, exclude_empty_subplots)
2097 return self
2098
-> 2099 return self.add_traces(
2100 data=[trace],
2101 rows=[row] if row is not None else None,
~/opt/anaconda3/lib/python3.8/site-packages/plotly/basedatatypes.py in add_traces(self, data, rows, cols, secondary_ys, exclude_empty_subplots)
2181
2182 # Validate traces
-> 2183 data = self._data_validator.validate_coerce(data)
2184
2185 # Set trace indexes
~/opt/anaconda3/lib/python3.8/site-packages/_plotly_utils/basevalidators.py in validate_coerce(self, v, skip_invalid, _validate)
2675
2676 if invalid_els:
-> 2677 self.raise_invalid_elements(invalid_els)
2678
2679 v = to_scalar_or_list(res)
~/opt/anaconda3/lib/python3.8/site-packages/_plotly_utils/basevalidators.py in raise_invalid_elements(self, invalid_els)
291 def raise_invalid_elements(self, invalid_els):
292 if invalid_els:
--> 293 raise ValueError(
294 """
295 Invalid element(s) received for the '{name}' property of {pname}
ValueError:
Invalid element(s) received for the 'data' property of
Invalid elements include: [Figure({
'data': [{'alignmentgroup': 'True',
'boxpoints': 'all',
'hovertemplate': '<b>%{hovertext}</b><br><br>party=%{x}<br>casesper100k=%{y}<extra></extra>',
'hovertext': array(['Autauga County', 'Baldwin County', 'Barbour County', ...,
'Uinta County', 'Washakie County', 'Weston County'], dtype=object),
'legendgroup': 'REP',
'marker': {'color': 'red'},
'name': 'REP',
'notched': False,
'offsetgroup': 'REP',
'orientation': 'v',
'showlegend': True,
'type': 'box',
'x': array(['REP', 'REP', 'REP', ..., 'REP', 'REP', 'REP'], dtype=object),
'x0': ' ',
'xaxis': 'x',
'y': array([ 8537.82956559, 6742.70048469, 6619.13635259, ..., 8370.4143182 ,
10211.40294683, 7001.58798903]),
'y0': ' ',
'yaxis': 'y'},
{'alignmentgroup': 'True',
'boxpoints': 'all',
'hovertemplate': '<b>%{hovertext}</b><br><br>party=%{x}<br>casesper100k=%{y}<extra></extra>',
'hovertext': array(['Bullock County', 'Dallas County', 'Greene County', ..., 'Sauk County',
'Albany County', 'Teton County'], dtype=object),
'legendgroup': 'DEM',
'marker': {'color': 'blue'},
'name': 'DEM',
'notched': False,
'offsetgroup': 'DEM',
'orientation': 'v',
'showlegend': True,
'type': 'box',
'x': array(['DEM', 'DEM', 'DEM', ..., 'DEM', 'DEM', 'DEM'], dtype=object),
'x0': ' ',
'xaxis': 'x',
'y': array([9395.10939511, 7818.04495107, 8913.82073727, ..., 7592.87421247,
9225.82304527, 9998.29526083]),
'y0': ' ',
'yaxis': 'y'}],
'layout': {'boxmode': 'overlay',
'legend': {'title': {'text': 'party'}, 'tracegroupgap': 0},
'margin': {'t': 60},
'template': '...',
'xaxis': {'anchor': 'y',
'categoryarray': [REP, DEM],
'categoryorder': 'array',
'domain': [0.0, 1.0],
'title': {'text': 'party'}},
'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'casesper100k'}}}
})]
The 'data' property is a tuple of trace instances
that may be specified as:
- A list or tuple of trace instances
(e.g. [Scatter(...), Bar(...)])
- A single trace instance
(e.g. Scatter(...), Bar(...), etc.)
- A list or tuple of dicts of string/value properties where:
- The 'type' property specifies the trace type
One of: ['area', 'bar', 'barpolar', 'box',
'candlestick', 'carpet', 'choropleth',
'choroplethmapbox', 'cone', 'contour',
'contourcarpet', 'densitymapbox', 'funnel',
'funnelarea', 'heatmap', 'heatmapgl',
'histogram', 'histogram2d',
'histogram2dcontour', 'image', 'indicator',
'isosurface', 'mesh3d', 'ohlc', 'parcats',
'parcoords', 'pie', 'pointcloud', 'sankey',
'scatter', 'scatter3d', 'scattercarpet',
'scattergeo', 'scattergl', 'scattermapbox',
'scatterpolar', 'scatterpolargl',
'scatterternary', 'splom', 'streamtube',
'sunburst', 'surface', 'table', 'treemap',
'violin', 'volume', 'waterfall']
- All remaining properties are passed to the constructor of
the specified trace type
(e.g. [{'type': 'scatter', ...}, {'type': 'bar, ...}])