How to resize legend items

Hi there,

Is there anything that can be done to resize the items (specifically the ID color boxes, not the font) displayed in my legend when creating a figure? When I try to export images at high resolutions, the legend winds up being much too small.

Realize I posted a similar question pertaining to choropleth figures the other day, but Iโ€™d be happy with even a generalist solution.

Thanks!

1 Like

You could use the layout attributes itemsizing and itemwidth:

import plotly.graph_objects as go

dict_of_fig = dict({
    "data": [{"type": "scatter",
              "x": [1, 2, 3],
              "y": [1, 3, 2]}],
    "layout": {"legend": {"itemsizing": "constant", "itemwidth": 100}}
})

fig = go.Figure(dict_of_fig)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[2, 3, 4]))
fig.show()

Hmmm, it seems the issue is that when the legend items are squares (rather than lines), width simply adds a bunch of white space on either side. Is there anything to enlarge?

Unfortunately there is no attribute to change the item height independently of the trace. Also see this related thread:

Interesting! Iโ€™m hoping that because Iโ€™m working with choropleths, marker size may not affect the actual chart but still affect the legend. Thanks for your help!

+1 Would be great if legends could be increased/decreased in size independently of the rest of the figure. I am currently exporting a PDF and then change the legend size with Adobe Illustrator.

I have not tried though > r - Change legend size in plotly chart - Stack Overflow

No, only font size, e.g.

import plotly.express as px

df = px.data.iris()
fig = px.scatter(df, x="sepal_length", y="sepal_width", color="species",
                title="Playing with Fonts")

fig.update_layout(legend = dict(font = dict(family = "Courier", size = 50, color = "black")),
                  legend_title = dict(font = dict(family = "Courier", size = 40, color = "blue")))
fig.show()

Yeah, I find it very frustrating that there is no way to adjust legend size without changing the trace size (which Iโ€™ve found canโ€™t be done with choropleths angways). I suppose itโ€™s not a huge problem when working with svgs, but Mapbox choropleths in particular donโ€™t appear to vectorize properly so scaling creates a blurry mess. Hopefully that functionality comes at some point.

Is there a feature request regarding this on GitHub already?

Frustrating indeed, in my case I have a bubble charts where the bubble area is defined by a column of my dataframe.
And I get the same kind of result as @windrose.

My audience is >50 y.o, they keep asking for larger symbols and legendsโ€ฆ

I dont know if it will help, I have a similar problem and it didnโ€™t help either.

  • overwrite (bool) โ€“ If True, existing properties will be overwritten. If False, recursively apply updates to existing properties, retaining properties that are not specified in the update operation.

Here a related GitHub issue can be found.