Skip to content

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

Open
robertoffmoura wants to merge 1 commit into
plotly:mainfrom
robertoffmoura:rm/hide-internal-mpl-label-legends
Open

Only show a legend when the matplotlib figure has one#5699
robertoffmoura wants to merge 1 commit into
plotly:mainfrom
robertoffmoura:rm/hide-internal-mpl-label-legends

Conversation

@robertoffmoura

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

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.

1 participant