2
votes

I am having a really tough time to make legends bigger in plotly. I've exhausted the docs, forums, github issues and have found nothing. Starting to think plotly isn't that great of software.

I created this graph:

enter image description here

And I want to make the lines bigger in the legend for Trial 1, Trial 2, and Trial 3. I can make the font bigger using their api but I see no reference to the legend lines and now wondering whether it is possibe.

Here's some Code:


fig.update_layout(
    title_text="Dipole Moment X (0 V/nm)",
    title_font=dict(size=44, family='Arial'),
    template='simple_white',
    xaxis_tickformat = 'i',
    bargap=0.2, # gap between bars of adjacent location coordinates,
    legend=dict(
        orientation="h",
        yanchor="bottom",
        y=1.02,
        xanchor="right",
        x=1,  
        font = dict(family = "Arial", size = 60),
        bordercolor="LightSteelBlue",
        borderwidth=2,
        itemsizing='trace'
    ),
    legend_title = dict(font = dict(family = "Arial", size = 60)),
)

Played around with the itemsizing and got nothing either. Anyone have any idea on how to accomplish this?

UPDATE:

Based on the answer below I was able to get the line thicker but there is a limit. And attached is the limit of thickness I believe it to be (don't know the exact size)

enter image description here

1
Thank you for marking my suggestion as the accepted answer. I see you've received a lot of useful suggestions to other questions as well, but a lot of them have not been accepted. Please review your questions and see if some of the numerous answers deserve to be accepted there too (disclaimer: on of them comes from me).vestland
Ya definitely, I just joined a PhD program (1-month in) and it's been really intense so far so I have a backlog of community software things list. The subplot one I'm actually going to check out really soon.Suliman Sharif
OK, thanks for the feedback. And congrats on starting your PhD!vestland

1 Answers

2
votes

The answer:

Depending on how you've set up your figure, you can use:

fig.update_layout(legend=dict(itemsizing='constant'))

Or:

fig.update_layout(legend=dict(itemsizing='trace'))
fig.update_traces(line=dict(width=12))

The details:

You seem to have opted for very thin lines when setting up the figure. You can "detach" the width of the lines in the legend from the width of the trace lines using:

fig.update_layout(legend=dict(itemsizing='constant'))

Since you haven't produced a runnable code snippet, I'll show the effect using data from px.data.gapminder().

Plot 1 - Without fig.update_layout(legend=dict(itemsizing='constant'))

enter image description here

Plot 2 - With fig.update_layout(legend=dict(itemsizing='constant'))

enter image description here

Your third option is to set fig.update_layout(legend=dict(itemsizing='trace')) and then increase the line width for the traces and the legend using, for example, fig.update_traces(line=dict(width=12)):

Plot 3 - With fig.update_layout(legend=dict(itemsizing='trace'))

enter image description here

Complete code with all options available

import plotly.express as px

df = px.data.gapminder().query("continent=='Oceania'")
fig = px.line(df, x="year", y="lifeExp", color='country')

fig.update_layout(showlegend=True)
fig.update_layout(legend = dict(bgcolor = 'yellow'))

#fig.update_layout(legend=dict(itemsizing='constant'))
fig.update_layout(legend=dict(itemsizing='trace'))
fig.update_traces(line=dict(width=12))

fig.show()