Skip to content

Commit

Permalink
Updated Legends to appear on ax instead of fig (#1272)
Browse files Browse the repository at this point in the history
Resolves #1271 "Legends
should appear on the ax object when passed to plotting functions"
Resolves #1280 "Plotting
Unit Test Regression"
- Changed default legend positioning from fig to ax, to better support
using DESC functions in subplots
- Passing an array of phi values to plot_boundaries plots cross-sections
for each phi value, instead of skipping the last one.
- Reduced tolerances for plotting unit tests
- Updated plotting baseline
  • Loading branch information
YigitElma authored Oct 1, 2024
2 parents 056fcc4 + 3e1bd18 commit 9096947
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 12 deletions.
14 changes: 6 additions & 8 deletions desc/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2016,7 +2016,7 @@ def plot_boundary(eq, phi=None, plot_axis=True, ax=None, return_data=False, **kw
ax.set_ylabel(_AXIS_LABELS_RPZ[2], fontsize=ylabel_fontsize)
ax.tick_params(labelbottom=True, labelleft=True)

fig.legend(**legend_kw)
ax.legend(**legend_kw)
_set_tight_layout(fig)

plot_data = {}
Expand Down Expand Up @@ -2120,9 +2120,7 @@ def plot_boundaries(

phi = (1 if eqs[-1].N == 0 else 4) if phi is None else phi
if isinstance(phi, numbers.Integral):
phi = np.linspace(
0, 2 * np.pi / eqs[-1].NFP, phi + 1
) # +1 to include pi and 2pi
phi = np.linspace(0, 2 * np.pi / eqs[-1].NFP, phi, endpoint=False)
phi = np.atleast_1d(phi)

neq = len(eqs)
Expand Down Expand Up @@ -2173,7 +2171,7 @@ def plot_boundaries(
plot_data["R"].append(R)
plot_data["Z"].append(Z)

for j in range(nz - 1):
for j in range(nz):
(line,) = ax.plot(
R[:, -1, j], Z[:, -1, j], color=colors[i], linestyle=ls[i], lw=lw[i]
)
Expand All @@ -2190,7 +2188,7 @@ def plot_boundaries(
ax.tick_params(labelbottom=True, labelleft=True)

if any(labels) and kwargs.pop("legend", True):
fig.legend(**kwargs.pop("legend_kw", {}))
ax.legend(**kwargs.pop("legend_kw", {}))
_set_tight_layout(fig)

assert (
Expand Down Expand Up @@ -2706,7 +2704,7 @@ def plot_boozer_modes( # noqa: C901
ax.set_ylabel(ylabel, fontsize=ylabel_fontsize)

if kwargs.pop("legend", True):
fig.legend(**kwargs.pop("legend_kw", {"loc": "lower right"}))
ax.legend(**kwargs.pop("legend_kw", {"loc": "lower right"}))

assert (
len(kwargs) == 0
Expand Down Expand Up @@ -3119,7 +3117,7 @@ def plot_qs_error( # noqa: 16 fxn too complex
ax.set_ylabel(ylabel, fontsize=ylabel_fontsize)

if kwargs.pop("legend", True):
fig.legend(**kwargs.pop("legend_kw", {"loc": "center right"}))
ax.legend(**kwargs.pop("legend_kw", {"loc": "center right"}))

assert (
len(kwargs) == 0
Expand Down
Binary file modified tests/baseline/test_bounce1d_checks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_b_mag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boozer_modes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boozer_modes_breaking_only.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boozer_modes_max.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boozer_modes_no_norm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boundaries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boundary.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_boundary_surface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_plot_qs_error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 15 additions & 4 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
from desc.utils import isalmostequal
from desc.vmec import VMECIO

tol_1d = 7.8
tol_2d = 15
tol_3d = 15
tol_1d = 4.5
tol_2d = 10
tol_3d = 10


@pytest.mark.unit
Expand Down Expand Up @@ -507,7 +507,7 @@ def test_plot_boundary_surface(self):
return fig

@pytest.mark.unit
@pytest.mark.mpl_image_compare(remove_text=True, tolerance=tol_2d)
@pytest.mark.mpl_image_compare(remove_text=True, tolerance=tol_1d)
def test_plot_boundaries(self):
"""Test plotting boundaries."""
eq1 = get("SOLOVEV")
Expand All @@ -516,6 +516,11 @@ def test_plot_boundaries(self):
eq4 = get("ESTELL")
with pytest.raises(ValueError, match="differing field periods"):
fig, ax = plot_boundaries([eq3, eq4], theta=0)
_, _, data1 = plot_boundaries(
(eq1, eq2, eq3),
phi=4,
return_data=True,
)
fig, ax, data = plot_boundaries(
(eq1, eq2, eq3),
phi=np.linspace(0, 2 * np.pi / eq3.NFP, 4, endpoint=False),
Expand All @@ -525,6 +530,12 @@ def test_plot_boundaries(self):
assert "Z" in data.keys()
assert len(data["R"]) == 3
assert len(data["Z"]) == 3
assert (
data["R"][-1].shape == data1["R"][-1].shape
), "Passing phi as an integer or array results in different behavior"
assert (
data["Z"][-1].shape == data1["Z"][-1].shape
), "Passing phi as an integer or array results in different behavior"

return fig

Expand Down

0 comments on commit 9096947

Please sign in to comment.