Plotly scatter (bubble plot) marker size in legend

Viewed 1475

Is there a way to show marker size (bubble size) within the legend of a graph in plotly express (plotly python). For example using the code below, it shows the population using the marker size.

df = px.data.gapminder().query("year==2007")
fig = px.scatter(df, x="gdpPercap", y="lifeExp", color="continent",
    size="pop", size_max=45, log_x=True)
fig.show()

Output: enter image description here

Matplotlib has something similar using the .legend_elements("sizes") (see here, creating a matplotlib scatter legend size related), was wondering if there is something in Plotly as well.

1 Answers

If you have multiple scatter plots, the size of the legend is basically linked to the size of each marker. As a means of aligning them, you can use the raft setting.API here Offical Reference

fig.update_layout(legend= {'itemsizing': 'trace'})
# Constant: same, Trace: individual marker size

The marker size of your question is a categorical variable, so there is no effect of changing the size.

Related