Plotly: Change space between legend entries?

Viewed 2283

How can I reduce the separation/spacing between legend entries in Plotly? Making the legend font smaller only has so much impact. I want to do this because my real case has a lot of entries and I need to compact them.

Here is a Python Plotly Express example, but doubt the frontend language matters. Note the huge amount of space between "Female, No", "Female, Yes", etc. in the legend:

import plotly.express as px

df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="sex", symbol="smoker", facet_col="time",
          labels={"sex": "Gender", "smoker": "Smokes"})

fig.update_layout(
    legend=dict(
        font=dict(
            size=6,
        ),
    )
)

fig.show()

enter image description here

1 Answers

You can change the space between legend entries using, for example:

fig.layout.legend.tracegroupgap = 10

But the default value is 0 so you can't decrease it any furher like you're trying to do here. The only accepted interval is [0, inf]. If you should try a negative number you'd get:

ValueError: Invalid value of type 'builtins.int' received for the 'tracegroupgap' property of layout.legend Received value: -1

So unfortunately there does not seem to be a direct way to do what you're aiming for at the time being.

Related