-
Couldn't load subscription status.
- Fork 4
Plots/add utils functions #214
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
hoyer-a
wants to merge
10
commits into
plot/ax_colorbar_refactor
Choose a base branch
from
plots/add_utils_functions
base: plot/ax_colorbar_refactor
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
Show all changes
10 commits
Select commit
Hold shift + click to select a range
52c59aa
add utils functions for plots
hoyer-a 6232cef
fix ruff and remove private function from init
hoyer-a 4280c4f
remove blank line
hoyer-a d547680
add docstring to test
hoyer-a f81f5c8
specify return type
hoyer-a a16c3b4
fix tests
hoyer-a 73e0798
fix ruff
hoyer-a 1e2f95f
revise tests
hoyer-a 8325add
extend assertion
hoyer-a a020f54
apply review
hoyer-a 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """Private utility functions for plot module.""" | ||
|
|
||
| import numpy as np | ||
| import matplotlib.pyplot as plt | ||
|
|
||
|
|
||
| def _prepare_plot(ax=None, projection=None): | ||
| """ | ||
| Returns a figure to plot on. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| ax : matplotlib.axes.Axes or list, tuple or ndarray of maplotlib.axes.Axes | ||
| Axes to plot on. The default is None in which case the axes are | ||
| obtained from the current figure. A new figure is created if it does | ||
| not exist. | ||
| projection : str, optional | ||
| Type of projection for the axes. This is only applied if new axes are | ||
| created. Default is ``None`` (2D projection). See | ||
| `matplotlib.projections <https://matplotlib.org/stable/api/projections_api.html>`_ | ||
| for more information on projections. | ||
|
|
||
| Returns | ||
| ------- | ||
| fig : matplotlib.figure.Figure | ||
| Returns the active figure. | ||
| ax : maptlotlib.axes.Axes | ||
| Returns the current axes. | ||
| """ # noqa: E501 | ||
| if ax is None: | ||
| # get current figure or create a new one | ||
| fig = plt.gcf() | ||
| if fig.axes: | ||
| ax = plt.gca() | ||
| else: | ||
| ax = plt.axes(projection=projection) | ||
|
|
||
| else: | ||
| # get figure from axis | ||
| # ax can be array or tuple of two ax objects | ||
| # first axis for the plot, second axis for colorbar placement | ||
| if isinstance(ax, np.ndarray): | ||
| fig = ax.flatten()[0].figure | ||
| elif isinstance(ax, (list, tuple)): | ||
| fig = ax[0].figure | ||
| else: | ||
| fig = ax.figure | ||
|
|
||
| return fig, ax | ||
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,32 @@ | ||
| import pytest | ||
| import matplotlib.pyplot as plt | ||
| from spharpy.plot._utils import _prepare_plot | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("ax_case", "output_type", "projection"), | ||
| [("none", plt.Axes, "3d"), ("single", plt.Axes, None), | ||
| ("two", list, None)], | ||
| ) | ||
| def test_prepare_plot(ax_case, output_type, projection): | ||
| """ | ||
| Test output of :py:func:`~spharpy.plot._utils._prepare_plot`. | ||
| """ | ||
| if ax_case == "none": | ||
| input_ax = None | ||
| elif ax_case == "single": | ||
| _, input_ax = plt.subplots() | ||
| else: | ||
| _, axs = plt.subplots(1, 2) | ||
| input_ax = [axs[0], axs[1]] | ||
|
|
||
| fig, ax = _prepare_plot(input_ax, projection) | ||
|
|
||
| assert isinstance(fig, plt.Figure) | ||
| assert isinstance(ax, output_type) | ||
|
|
||
| if isinstance(ax, list): | ||
| assert all(isinstance(ax_, plt.Axes) for ax_ in ax) | ||
|
|
||
| if ax is None and projection is not None: | ||
| assert ax.name == projection |
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.
Uh oh!
There was an error while loading. Please reload this page.