2

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.

Somya Mohanty
  • 75
  • 2
  • 6
  • This is not available at the moment, you can follow this issue to receive updated https://github.com/plotly/plotly.js/issues/5099 – joelostblom May 26 '21 at 14:59

1 Answers1

0

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.

r-beginners
  • 31,170
  • 3
  • 14
  • 32
  • In the above plot, i would like to have a sub legend (apart from the categorial legend) which shows the size of the marker in relationship to a variable. The `itemsizing` attribute is operating globally, is there a way to control it for grouped legends. – Somya Mohanty Feb 14 '21 at 02:50
  • Something similar to this. https://stackoverflow.com/questions/34582584/how-to-simultaneously-apply-color-shape-size-in-a-scatter-plot-using-plotly – Somya Mohanty Feb 14 '21 at 02:56
  • plotly does not seem to support multiple legends at this time. I am referring to [this answer](https://stackoverflow.com/questions/62780182/displaying-multiple-legends-in-a-single-trace-in-plotly) and from the [development community](https://github.com/plotly/plotly.js/issues/5099). – r-beginners Feb 14 '21 at 03:03