Announcing Dash Bio 1.0.0 🎉 : a one-stop-shop for bioinformatics and drug development visualizations.

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?