Skip to content

Only show a legend when the matplotlib figure has one - #5699

Merged
camdecoster merged 4 commits into
plotly:mainfrom
robertoffmoura:rm/hide-internal-mpl-label-legends
Oct 7, 2026
Merged

camdecoster merged 4 commits into
plotly:mainfrom
robertoffmoura:rm/hide-internal-mpl-label-legends

Conversation

@robertoffmoura

@robertoffmoura robertoffmoura commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Converted matplotlib figures without legends unexpectedly show legends in plotly. mpl_to_plotly left layout.showlegend unset, and plotly.js defaults it to True, auto-naming unnamed traces, so every conversion without an explicit mpl legend (e.g. plt.plot(x, y) with no labels, boxplots) rendered with a legend of junk entries.

Fix:

  • open_figure now sets showlegend=False by default; the legend is only enabled by open_legend when the matplotlib figure actually has one (existing behavior, unchanged)

Snippet to reproduce:

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import plotly.tools as tls

fig, ax = plt.subplots()
ax.plot([0, 1, 2, 3], [0, 1, 0, 1], "b", [0, 1, 2, 3], [1, 0, 1, 0], "r--")
fig.savefig("legend_mpl.png")

p = tls.mpl_to_plotly(fig)
p.write_image("legend_plotly.png")
matplotlib plotly before plotly after
legend_mpl legend_plotly_before legend_plotly_after

@robertoffmoura
robertoffmoura force-pushed the rm/hide-internal-mpl-label-legends branch from b0a951c to e405fe0 Compare August 21, 2026 17:18
@robertoffmoura
robertoffmoura force-pushed the rm/hide-internal-mpl-label-legends branch 2 times, most recently from 6ab05fc to 7388a2b Compare September 3, 2026 11:43

@camdecoster camdecoster left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but does it handle the case when a plot has a legend enabled?

Could you please add a changelog entry?

@robertoffmoura
robertoffmoura force-pushed the rm/hide-internal-mpl-label-legends branch from 7388a2b to d167f80 Compare October 4, 2026 03:02
@robertoffmoura

Copy link
Copy Markdown
Contributor Author

Thank you. I've added a changelog entry.

Yes, it handles the case when a plot has a legend enabled. This behaviour is covered by tests in plotly/matplotlylib/tests/test_renderer.py:

  • test_native_legend_enabled_when_matplotlib_legend_present
  • test_multiple_traces_native_legend

@camdecoster

Copy link
Copy Markdown
Contributor

What about the case of a figure with a legend, but where a line doesn't have a label?

@robertoffmoura
robertoffmoura force-pushed the rm/hide-internal-mpl-label-legends branch from d167f80 to e3ad1e7 Compare October 6, 2026 06:28
@robertoffmoura

Copy link
Copy Markdown
Contributor Author

Sorry, I misunderstood the previous comment. I just pushed a change to hide legend entries of unlabelled lines but keep those of labelled ones.

snippet to verify:

import matplotlib.pyplot as plt
import plotly.tools as tls

fig, ax = plt.subplots()
ax.plot([0, 1, 2], [0, 1, 2], label="Labeled line", color="blue")
ax.plot([0, 1, 2], [2, 1, 0], color="red")  # Unlabeled line
ax.legend()

# Save matplotlib reference
fig.savefig("legend_unlabeled_line_mpl.png", bbox_inches="tight")

# Convert and save plotly version
plotly_fig = tls.mpl_to_plotly(fig)
plotly_fig.write_image("legend_unlabeled_line_plotly.png")

@camdecoster
camdecoster merged commit d3d7e4d into plotly:main Oct 7, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants