-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Use native legends when converting from matplotlib #5312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+137
−81
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
plotly/matplotlylib/tests/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import matplotlib | ||
|
|
||
| matplotlib.use("Agg") | ||
| import matplotlib.pyplot as plt |
87 changes: 87 additions & 0 deletions
plotly/matplotlylib/tests/test_renderer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import plotly.tools as tls | ||
|
|
||
| from . import plt | ||
|
|
||
|
|
||
| def test_native_legend_enabled_when_matplotlib_legend_present(): | ||
| """Test that when matplotlib legend is present, Plotly uses native legend.""" | ||
| fig, ax = plt.subplots() | ||
| ax.plot([0, 1], [0, 1], label="Line 1") | ||
| ax.plot([0, 1], [1, 0], label="Line 2") | ||
| ax.legend() | ||
|
|
||
| plotly_fig = tls.mpl_to_plotly(fig) | ||
|
|
||
| # Should enable native legend | ||
| assert plotly_fig.layout.showlegend == True | ||
| # Should have 2 traces with names | ||
| assert len(plotly_fig.data) == 2 | ||
| assert plotly_fig.data[0].name == "Line 1" | ||
| assert plotly_fig.data[1].name == "Line 2" | ||
|
|
||
|
|
||
| def test_no_fake_legend_shapes_with_native_legend(): | ||
| """Test that fake legend shapes are not created when using native legend.""" | ||
| fig, ax = plt.subplots() | ||
| ax.plot([0, 1], [0, 1], "o-", label="Data with markers") | ||
| ax.legend() | ||
|
|
||
| plotly_fig = tls.mpl_to_plotly(fig) | ||
|
|
||
| # Should use native legend | ||
| assert plotly_fig.layout.showlegend == True | ||
| # Should not create fake legend elements | ||
| assert len(plotly_fig.layout.shapes) == 0 | ||
| assert len(plotly_fig.layout.annotations) == 0 | ||
|
|
||
|
|
||
| def test_legend_disabled_when_no_matplotlib_legend(): | ||
| """Test that legend is not enabled when no matplotlib legend is present.""" | ||
| fig, ax = plt.subplots() | ||
| ax.plot([0, 1], [0, 1], label="Line 1") # Has label but no legend() call | ||
|
|
||
| plotly_fig = tls.mpl_to_plotly(fig) | ||
|
|
||
| # Should not have showlegend explicitly set to True | ||
| # (Plotly's default behavior when no legend elements exist) | ||
| assert ( | ||
| not hasattr(plotly_fig.layout, "showlegend") | ||
| or plotly_fig.layout.showlegend != True | ||
| ) | ||
|
|
||
|
|
||
| def test_legend_disabled_when_matplotlib_legend_not_visible(): | ||
| """Test that legend is not enabled when no matplotlib legend is not visible.""" | ||
| fig, ax = plt.subplots() | ||
| ax.plot([0, 1], [0, 1], label="Line 1") | ||
| legend = ax.legend() | ||
| legend.set_visible(False) # Hide the legend | ||
|
|
||
| plotly_fig = tls.mpl_to_plotly(fig) | ||
|
|
||
| # Should not enable legend when matplotlib legend is hidden | ||
| assert ( | ||
| not hasattr(plotly_fig.layout, "showlegend") | ||
| or plotly_fig.layout.showlegend != True | ||
| ) | ||
|
|
||
|
|
||
| def test_multiple_traces_native_legend(): | ||
| """Test native legend works with multiple traces of different types.""" | ||
| fig, ax = plt.subplots() | ||
| ax.plot([0, 1, 2], [0, 1, 0], "-", label="Line") | ||
| ax.plot([0, 1, 2], [1, 0, 1], "o", label="Markers") | ||
| ax.plot([0, 1, 2], [0.5, 0.5, 0.5], "s-", label="Line+Markers") | ||
| ax.legend() | ||
|
|
||
| plotly_fig = tls.mpl_to_plotly(fig) | ||
|
|
||
| assert plotly_fig.layout.showlegend == True | ||
| assert len(plotly_fig.data) == 3 | ||
| assert plotly_fig.data[0].name == "Line" | ||
| assert plotly_fig.data[1].name == "Markers" | ||
| assert plotly_fig.data[2].name == "Line+Markers" | ||
| # Verify modes are correct | ||
| assert plotly_fig.data[0].mode == "lines" | ||
| assert plotly_fig.data[1].mode == "markers" | ||
| assert plotly_fig.data[2].mode == "lines+markers" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.