Skip to content

Commit f174240

Browse files
Devin-CrawfordPipKatSMoraisAnsys
authored
CHORE: expose show for plotting (#4708)
Co-authored-by: Kathy Pippert <[email protected]> Co-authored-by: Sébastien Morais <[email protected]> Co-authored-by: Sebastien Morais <[email protected]>
1 parent fabce85 commit f174240

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

pyaedt/generic/plot.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,14 @@ def is_notebook():
9999
bool
100100
"""
101101
try:
102+
from IPython import get_ipython
103+
102104
shell = get_ipython().__class__.__name__
103-
if shell == "ZMQInteractiveShell":
104-
return True # Jupyter notebook or qtconsole
105-
else:
106-
return False
105+
# Check if shell is Jupyter notebook or QTconsole
106+
return shell == "ZMQInteractiveShell"
107+
# Probably standard Python interpreter
107108
except NameError:
108-
return False # Probably standard Python interpreter
109+
return False
109110

110111

111112
def is_float(istring):
@@ -385,7 +386,7 @@ def plot_polar_chart(
385386

386387
@pyaedt_function_handler()
387388
@update_plot_settings
388-
def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="", snapshot_path=None):
389+
def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="", snapshot_path=None, show=True):
389390
"""Create a Matplotlib 3D plot based on a list of data.
390391
391392
Parameters
@@ -403,6 +404,9 @@ def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="",
403404
Plot Title label.
404405
snapshot_path : str
405406
Full path to image file if a snapshot is needed.
407+
show : bool, optional
408+
Whether to render the figure. The default is ``True``. If ``False``, the
409+
figure is not drawn.
406410
407411
Returns
408412
-------
@@ -432,14 +436,16 @@ def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="",
432436
fig.set_size_inches(size[0] / dpi, size[1] / dpi)
433437
if snapshot_path:
434438
fig.savefig(snapshot_path)
435-
else:
439+
if show:
436440
fig.show()
437441
return fig
438442

439443

440444
@pyaedt_function_handler()
441445
@update_plot_settings
442-
def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", ylabel="", title="", snapshot_path=None):
446+
def plot_2d_chart(
447+
plot_data, size=(2000, 1000), show_legend=True, xlabel="", ylabel="", title="", snapshot_path=None, show=True
448+
):
443449
"""Create a Matplotlib plot based on a list of data.
444450
Parameters
445451
----------
@@ -459,6 +465,9 @@ def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", yla
459465
snapshot_path : str, optional
460466
Full path to image file if a snapshot is needed.
461467
The default value is ``None``.
468+
show : bool, optional
469+
Whether to render the figure. The default is ``True``. If ``False``, the
470+
figure is not drawn.
462471
463472
Returns
464473
-------
@@ -489,7 +498,7 @@ def plot_2d_chart(plot_data, size=(2000, 1000), show_legend=True, xlabel="", yla
489498

490499
if snapshot_path:
491500
fig.savefig(snapshot_path)
492-
elif not is_notebook():
501+
if show:
493502
fig.show()
494503
return fig
495504

@@ -540,9 +549,10 @@ def plot_matplotlib(
540549
Default is `False`.
541550
annotations : list, optional
542551
List of annotations to add to the plot. The format is [x, y, string, dictionary of font options].
543-
Default is `None`.
552+
The default is ``None``.
544553
show : bool, optional
545-
Whether to show the plot or return the matplotlib object. Default is `True`.
554+
Whether to render the figure. The default is ``True``. If ``False``, the
555+
figure is not drawn.
546556
547557
548558
Returns
@@ -636,10 +646,10 @@ def plot_contour(
636646
levels : int, optional
637647
Color map levels. The default is ``64``.
638648
snapshot_path : str, optional
639-
Full path to save the image save. The default is ``None``.
649+
Full path to save the image to. The default is ``None``.
640650
show : bool, optional
641-
Whether to render the figure. The default is ``True``. If
642-
``False``, the image is not drawn.
651+
Whether to render the figure. The default is ``True``. If ``False``, the
652+
figure is not drawn.
643653
644654
Returns
645655
-------

pyaedt/modules/AdvancedPostProcessing.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,13 @@ def animate_fields_from_aedtplt(
811811

812812
@pyaedt_function_handler()
813813
def create_3d_plot(
814-
self, solution_data, nominal_sweep=None, nominal_value=None, primary_sweep="Theta", secondary_sweep="Phi"
814+
self,
815+
solution_data,
816+
nominal_sweep=None,
817+
nominal_value=None,
818+
primary_sweep="Theta",
819+
secondary_sweep="Phi",
820+
show=True,
815821
):
816822
"""Create a 3D plot using Matplotlib.
817823
@@ -827,17 +833,20 @@ def create_3d_plot(
827833
Primary sweep. The default is ``"Theta"``.
828834
secondary_sweep : str, optional
829835
Secondary sweep. The default is ``"Phi"``.
836+
show : bool, optional
837+
Whether to render the figure. The default is ``True``. If ``False``, the
838+
figure is not drawn.
830839
831840
Returns
832841
-------
833-
bool
834-
``True`` when successful, ``False`` when failed.
842+
:class:`matplotlib.plt`
843+
Matplotlib fig object.
835844
"""
836845
if nominal_value:
837846
solution_data.intrinsics[nominal_sweep] = nominal_value
838847
if nominal_value:
839848
solution_data.primary_sweep = primary_sweep
840-
return solution_data.plot_3d(x_axis=primary_sweep, y_axis=secondary_sweep)
849+
return solution_data.plot_3d(x_axis=primary_sweep, y_axis=secondary_sweep, show=show)
841850

842851
@pyaedt_function_handler(frames_list="frames", output_gif_path="gif_path")
843852
def plot_scene(

pyaedt/modules/solutions.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,9 @@ def plot(
819819
title="",
820820
snapshot_path=None,
821821
is_polar=False,
822+
show=True,
822823
):
823-
"""Create a matplotlib plot based on a list of data.
824+
"""Create a Matplotlib plot based on a list of data.
824825
825826
Parameters
826827
----------
@@ -846,9 +847,12 @@ def plot(
846847
title : str
847848
Plot title label.
848849
snapshot_path : str
849-
Full path to image file if a snapshot is needed.
850+
Full path to the image file if a snapshot is needed.
850851
is_polar : bool, optional
851-
Set to `True` if this is a polar plot.
852+
Whether this is a polar plot. The default is ``False``.
853+
show : bool, optional
854+
Whether to render the figure. The default is ``True``. If ``False``, the
855+
figure is not drawn.
852856
853857
Returns
854858
-------
@@ -893,9 +897,9 @@ def plot(
893897
if len(data_plot) > 15:
894898
show_legend = False
895899
if is_polar:
896-
return plot_polar_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path)
900+
return plot_polar_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path, show=show)
897901
else:
898-
return plot_2d_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path)
902+
return plot_2d_chart(data_plot, size, show_legend, x_label, y_label, title, snapshot_path, show=show)
899903

900904
@pyaedt_function_handler(xlabel="x_label", ylabel="y_label", math_formula="formula")
901905
def plot_3d(
@@ -909,6 +913,7 @@ def plot_3d(
909913
formula=None,
910914
size=(2000, 1000),
911915
snapshot_path=None,
916+
show=True,
912917
):
913918
"""Create a matplotlib 3d plot based on a list of data.
914919
@@ -935,6 +940,9 @@ def plot_3d(
935940
snapshot_path : str, optional
936941
Full path to image file if a snapshot is needed.
937942
The default is ``None``.
943+
show : bool, optional
944+
Whether to render the figure. The default is ``True``. If ``False``, the
945+
figure is not drawn.
938946
939947
Returns
940948
-------
@@ -985,7 +993,7 @@ def plot_3d(
985993
y_label = y_axis
986994
if not title:
987995
title = "Simulation Results Plot"
988-
return plot_3d_chart(data_plot, size, x_label, y_label, title, snapshot_path)
996+
return plot_3d_chart(data_plot, size, x_label, y_label, title, snapshot_path, show=show)
989997

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

0 commit comments

Comments
 (0)