diff --git a/pyaedt/generic/plot.py b/pyaedt/generic/plot.py index 839ac8aea27..aaea4ac2378 100644 --- a/pyaedt/generic/plot.py +++ b/pyaedt/generic/plot.py @@ -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): @@ -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 @@ -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 ------- @@ -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 ---------- @@ -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 ------- @@ -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 @@ -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 @@ -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 ------- diff --git a/pyaedt/modules/AdvancedPostProcessing.py b/pyaedt/modules/AdvancedPostProcessing.py index 6b5650d6074..081ed166218 100644 --- a/pyaedt/modules/AdvancedPostProcessing.py +++ b/pyaedt/modules/AdvancedPostProcessing.py @@ -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. @@ -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( diff --git a/pyaedt/modules/solutions.py b/pyaedt/modules/solutions.py index 8e798b5c0b0..71c3a5961cf 100644 --- a/pyaedt/modules/solutions.py +++ b/pyaedt/modules/solutions.py @@ -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 ---------- @@ -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 ------- @@ -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( @@ -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. @@ -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 ------- @@ -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):