Repository navigation
fix(graphics): axes_logic uses an empty current figure instead of creating a second one #265
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
Open
petercorke
wants to merge
1
commit into
rai-opensource:master
Choose a base branch
from
petercorke:fix/axes-logic-reuse-empty-figure
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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,102 @@ | ||
| """Tests of ``axes_logic``, which decides which axes a plot_xxx() function draws into.""" | ||
|
|
||
| import matplotlib | ||
|
|
||
| matplotlib.use("Agg") | ||
| import matplotlib.pyplot as plt | ||
| import pytest | ||
|
|
||
| from spatialmath.base.graphics import axes_logic | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def close_figures(): | ||
| plt.close("all") | ||
| yield | ||
| plt.close("all") | ||
|
|
||
|
|
||
| def nfigs(): | ||
| return len(plt.get_fignums()) | ||
|
|
||
|
|
||
| # --- behaviour that must not change --------------------------------------------- | ||
|
|
||
|
|
||
| def test_no_figure_creates_one(): | ||
| ax = axes_logic(None, 2) | ||
| assert nfigs() == 1 | ||
| assert ax is plt.gca() | ||
|
|
||
|
|
||
| def test_current_axes_are_reused(): | ||
| fig, ax0 = plt.subplots() | ||
| ax = axes_logic(None, 2) | ||
| assert ax is ax0 | ||
| assert nfigs() == 1 | ||
|
|
||
|
|
||
| def test_new_forces_a_new_figure(): | ||
| fig, ax0 = plt.subplots() | ||
| ax = axes_logic(None, 2, new=True) | ||
| assert ax is not ax0 | ||
| assert nfigs() == 2 | ||
| assert ax is plt.gca() | ||
|
|
||
|
|
||
| def test_dimension_mismatch_makes_a_new_figure(): | ||
| fig = plt.figure() | ||
| ax3 = fig.add_subplot(projection="3d") | ||
| ax = axes_logic(None, 2) | ||
| assert ax is not ax3 | ||
| assert nfigs() == 2 | ||
|
|
||
|
|
||
| def test_explicit_axes_of_the_right_dimension_are_used(): | ||
| fig, ax0 = plt.subplots() | ||
| fig2, ax1 = plt.subplots() | ||
| assert axes_logic(ax0, 2) is ax0 | ||
| assert nfigs() == 2 | ||
|
|
||
|
|
||
| def test_explicit_axes_of_the_wrong_dimension_make_a_new_figure(): | ||
| fig, ax0 = plt.subplots() | ||
| ax = axes_logic(ax0, 3) | ||
| assert ax is not ax0 | ||
| assert nfigs() == 2 | ||
|
|
||
|
|
||
| # --- an empty current figure, eg. from a bare plt.figure() ----------------------- | ||
|
|
||
|
|
||
| def test_empty_current_figure_is_used_2d(): | ||
| fig = plt.figure() | ||
| ax = axes_logic(None, 2) | ||
| assert nfigs() == 1 | ||
| assert ax.figure is fig | ||
| assert ax is plt.gca() | ||
|
|
||
|
|
||
| def test_empty_current_figure_is_used_3d(): | ||
| fig = plt.figure() | ||
| ax = axes_logic(None, 3) | ||
| assert nfigs() == 1 | ||
| assert ax.figure is fig | ||
| assert ax.name == "3d" | ||
|
|
||
|
|
||
| def test_only_the_current_figure_matters(): | ||
| # an earlier figure has axes, but the current one is empty: use the current one | ||
| plt.subplots() | ||
| fig = plt.figure() | ||
| ax = axes_logic(None, 2) | ||
| assert nfigs() == 2 | ||
| assert ax.figure is fig | ||
|
|
||
|
|
||
| def test_new_with_an_empty_current_figure_still_makes_another(): | ||
| # new=True is an explicit request for a new figure | ||
| fig = plt.figure() | ||
| ax = axes_logic(None, 2, new=True) | ||
| assert nfigs() == 2 | ||
| assert ax.figure is not fig | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find that banner comments like these are very likely to be ignored or go stale. I would recommend putting comments on each test. Or use docstrings.