Skip to content
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

REFACTOR: Extend consistency on plot methods #4723

Merged
merged 8 commits into from
May 27, 2024
8 changes: 2 additions & 6 deletions pyaedt/generic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="",
@update_plot_settings
def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", ylabel="", title="", snapshot_path=None):
"""Create a Matplotlib plot based on a list of data.

Parameters
----------
plot_data : list of list
Expand Down Expand Up @@ -470,10 +471,6 @@ def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", yla
fig, ax = plt.subplots(figsize=figsize)
label_id = 1
for plo_obj in plot_data:
if len(plo_obj) == 3:
label = plo_obj[2]
else:
label = "Trace " + str(label_id)
if isinstance(plo_obj[0], np.ndarray):
x = plo_obj[0]
y = plo_obj[1]
Expand Down Expand Up @@ -544,11 +541,10 @@ def plot_matplotlib(
show : bool, optional
Whether to show the plot or return the matplotlib object. Default is `True`.


Returns
-------
:class:`matplotlib.pyplot.Figure`
Matplotlib Figure object.
Matplotlib figure object.
"""
dpi = 100.0
figsize = (size[0] / dpi, size[1] / dpi)
Expand Down
81 changes: 35 additions & 46 deletions pyaedt/modules/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,8 @@ def plot(

Returns
-------
:class:`matplotlib.plt`
Matplotlib fig object.
:class:`matplotlib.pyplot.Figure`
Matplotlib figure object.
"""
if is_ironpython: # pragma: no cover
return False
Expand Down Expand Up @@ -938,8 +938,8 @@ def plot_3d(

Returns
-------
:class:`matplotlib.plt`
Matplotlib fig object.
:class:`matplotlib.figure.Figure`
Matplotlib figure object.
"""
if is_ironpython:
return False # pragma: no cover
Expand Down Expand Up @@ -1640,6 +1640,7 @@ def plot_farfield_contour(
Returns
-------
:class:`matplotlib.pyplot.Figure`
Matplotlib figure object.

Examples
--------
Expand Down Expand Up @@ -1772,11 +1773,8 @@ def plot_2d_cut(

Returns
-------
:class:`matplotlib.plt`
Whether to show the plotted curve.
If ``show=True``, a Matplotlib figure instance of the plot is returned.
If ``show=False``, the plotted curve is returned.

:class:`matplotlib.pyplot.Figure`
Matplotlib figure object.

Examples
--------
Expand Down Expand Up @@ -1849,30 +1847,29 @@ def plot_2d_cut(
return False
curves.append([x, y, "{}={}".format(y_key, data[y_key][theta_idx])])

if show:
show_legend = True
if len(curves) > 15:
show_legend = False
if is_polar:
return plot_polar_chart(
curves,
xlabel=x_key,
ylabel=quantity,
title=title,
snapshot_path=image_path,
show_legend=show_legend,
)
else:
return plot_2d_chart(
curves,
xlabel=x_key,
ylabel=quantity,
title=title,
snapshot_path=image_path,
show_legend=show_legend,
)
show_legend = show
if len(curves) > 15:
show_legend = False
if is_polar:
return plot_polar_chart(
curves,
xlabel=x_key,
ylabel=quantity,
title=title,
snapshot_path=image_path,
show_legend=show_legend,
show=show,
)
else:
return curves
return plot_2d_chart(
curves,
xlabel=x_key,
ylabel=quantity,
title=title,
snapshot_path=image_path,
show_legend=show_legend,
show=show,
)

# fmt: off
@pyaedt_function_handler(farfield_quantity="quantity",
Expand Down Expand Up @@ -1913,14 +1910,12 @@ def polar_plot_3d(
Full path for the image file. The default is ``None``, in which case a file is not exported.
show : bool, optional
Whether to show the plot. The default is ``True``.
If ``False``, the Matplotlib instance of the plot is shown.
If ``False``, the Matplotlib instance of the plot is not shown.

Returns
-------
:class:`matplotlib.plt`
Whether to show the plotted curve.
If ``show=True``, a Matplotlib figure instance of the plot is returned.
If ``show=False``, the plotted curve is returned.
:class:`matplotlib.pyplot.Figure`
Matplotlib figure object.

Examples
--------
Expand Down Expand Up @@ -1968,10 +1963,7 @@ def polar_plot_3d(
x = r * np.sin(theta_grid) * np.cos(phi_grid)
y = r * np.sin(theta_grid) * np.sin(phi_grid)
z = r * np.cos(theta_grid)
if show: # pragma: no cover
plot_3d_chart([x, y, z], xlabel="Theta", ylabel="Phi", title=title, snapshot_path=image_path)
else:
return x, y, z
return plot_3d_chart([x, y, z], xlabel="Theta", ylabel="Phi", title=title, snapshot_path=image_path, show=show)

# fmt: off
@pyaedt_function_handler(farfield_quantity="quantity", export_image_path="image_path")
Expand Down Expand Up @@ -2028,8 +2020,7 @@ def polar_plot_3d_pyvista(
Returns
-------
bool or :class:`Pyvista.Plotter`
``True`` when successful. The :class:`Pyvista.Plotter` is returned when ``show`` and
``export_image_path`` are ``False``.
``False`` when the method fails, Pyvista plotter object otherwise.

Examples
--------
Expand Down Expand Up @@ -2208,10 +2199,8 @@ def scale(value=1):

if image_path:
p.show(screenshot=image_path)
return True
elif show: # pragma: no cover
if show: # pragma: no cover
p.show()
return True
return p

@pyaedt_function_handler()
Expand Down
Loading