Skip to content

Commit

Permalink
CHORE: expose show for plotting (#4708)
Browse files Browse the repository at this point in the history
Co-authored-by: Kathy Pippert <[email protected]>
Co-authored-by: Sébastien Morais <[email protected]>
Co-authored-by: Sebastien Morais <[email protected]>
  • Loading branch information
4 people authored May 22, 2024
1 parent fabce85 commit f174240
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
38 changes: 24 additions & 14 deletions pyaedt/generic/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ def is_notebook():
bool
"""
try:
from IPython import get_ipython

shell = get_ipython().__class__.__name__
if shell == "ZMQInteractiveShell":
return True # Jupyter notebook or qtconsole
else:
return False
# Check if shell is Jupyter notebook or QTconsole
return shell == "ZMQInteractiveShell"
# Probably standard Python interpreter
except NameError:
return False # Probably standard Python interpreter
return False


def is_float(istring):
Expand Down Expand Up @@ -385,7 +386,7 @@ def plot_polar_chart(

@pyaedt_function_handler()
@update_plot_settings
def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="", snapshot_path=None):
def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="", snapshot_path=None, show=True):
"""Create a Matplotlib 3D plot based on a list of data.
Parameters
Expand All @@ -403,6 +404,9 @@ def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="",
Plot Title label.
snapshot_path : str
Full path to image file if a snapshot is needed.
show : bool, optional
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
-------
Expand Down Expand Up @@ -432,14 +436,16 @@ def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="",
fig.set_size_inches(size[0] / dpi, size[1] / dpi)
if snapshot_path:
fig.savefig(snapshot_path)
else:
if show:
fig.show()
return fig


@pyaedt_function_handler()
@update_plot_settings
def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", ylabel="", title="", snapshot_path=None):
def plot_2d_chart(
plot_data, size=(2000, 1000), show_legend=True, xlabel="", ylabel="", title="", snapshot_path=None, show=True
):
"""Create a Matplotlib plot based on a list of data.
Parameters
----------
Expand All @@ -459,6 +465,9 @@ def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", yla
snapshot_path : str, optional
Full path to image file if a snapshot is needed.
The default value is ``None``.
show : bool, optional
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
-------
Expand Down Expand Up @@ -489,7 +498,7 @@ def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", yla

if snapshot_path:
fig.savefig(snapshot_path)
elif not is_notebook():
if show:
fig.show()
return fig

Expand Down Expand Up @@ -540,9 +549,10 @@ def plot_matplotlib(
Default is `False`.
annotations : list, optional
List of annotations to add to the plot. The format is [x, y, string, dictionary of font options].
Default is `None`.
The default is ``None``.
show : bool, optional
Whether to show the plot or return the matplotlib object. Default is `True`.
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
Expand Down Expand Up @@ -636,10 +646,10 @@ def plot_contour(
levels : int, optional
Color map levels. The default is ``64``.
snapshot_path : str, optional
Full path to save the image save. The default is ``None``.
Full path to save the image to. The default is ``None``.
show : bool, optional
Whether to render the figure. The default is ``True``. If
``False``, the image is not drawn.
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
-------
Expand Down
17 changes: 13 additions & 4 deletions pyaedt/modules/AdvancedPostProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,13 @@ def animate_fields_from_aedtplt(

@pyaedt_function_handler()
def create_3d_plot(
self, solution_data, nominal_sweep=None, nominal_value=None, primary_sweep="Theta", secondary_sweep="Phi"
self,
solution_data,
nominal_sweep=None,
nominal_value=None,
primary_sweep="Theta",
secondary_sweep="Phi",
show=True,
):
"""Create a 3D plot using Matplotlib.
Expand All @@ -827,17 +833,20 @@ def create_3d_plot(
Primary sweep. The default is ``"Theta"``.
secondary_sweep : str, optional
Secondary sweep. The default is ``"Phi"``.
show : bool, optional
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
-------
bool
``True`` when successful, ``False`` when failed.
:class:`matplotlib.plt`
Matplotlib fig object.
"""
if nominal_value:
solution_data.intrinsics[nominal_sweep] = nominal_value
if nominal_value:
solution_data.primary_sweep = primary_sweep
return solution_data.plot_3d(x_axis=primary_sweep, y_axis=secondary_sweep)
return solution_data.plot_3d(x_axis=primary_sweep, y_axis=secondary_sweep, show=show)

@pyaedt_function_handler(frames_list="frames", output_gif_path="gif_path")
def plot_scene(
Expand Down
20 changes: 14 additions & 6 deletions pyaedt/modules/solutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,9 @@ def plot(
title="",
snapshot_path=None,
is_polar=False,
show=True,
):
"""Create a matplotlib plot based on a list of data.
"""Create a Matplotlib plot based on a list of data.
Parameters
----------
Expand All @@ -846,9 +847,12 @@ def plot(
title : str
Plot title label.
snapshot_path : str
Full path to image file if a snapshot is needed.
Full path to the image file if a snapshot is needed.
is_polar : bool, optional
Set to `True` if this is a polar plot.
Whether this is a polar plot. The default is ``False``.
show : bool, optional
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
-------
Expand Down Expand Up @@ -893,9 +897,9 @@ def plot(
if len(data_plot) > 15:
show_legend = False
if is_polar:
return plot_polar_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path)
return plot_polar_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path, show=show)
else:
return plot_2d_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path)
return plot_2d_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path, show=show)

@pyaedt_function_handler(xlabel="x_label", ylabel="y_label", math_formula="formula")
def plot_3d(
Expand All @@ -909,6 +913,7 @@ def plot_3d(
formula=None,
size=(2000, 1000),
snapshot_path=None,
show=True,
):
"""Create a matplotlib 3d plot based on a list of data.
Expand All @@ -935,6 +940,9 @@ def plot_3d(
snapshot_path : str, optional
Full path to image file if a snapshot is needed.
The default is ``None``.
show : bool, optional
Whether to render the figure. The default is ``True``. If ``False``, the
figure is not drawn.
Returns
-------
Expand Down Expand Up @@ -985,7 +993,7 @@ def plot_3d(
y_label = y_axis
if not title:
title = "Simulation Results Plot"
return plot_3d_chart(data_plot, size, x_label, y_label, title, snapshot_path)
return plot_3d_chart(data_plot, size, x_label, y_label, title, snapshot_path, show=show)

@pyaedt_function_handler()
def ifft(self, curve_header="NearE", u_axis="_u", v_axis="_v", window=False):
Expand Down

0 comments on commit f174240

Please sign in to comment.