From b388c51a6ef675b67b064bde0e5c490548958951 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 12:05:42 +0200 Subject: [PATCH 1/7] REFACTOR: Clean up plot.py file --- pyaedt/generic/plot.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pyaedt/generic/plot.py b/pyaedt/generic/plot.py index 839ac8aea27..bdec675eb1f 100644 --- a/pyaedt/generic/plot.py +++ b/pyaedt/generic/plot.py @@ -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 @@ -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] @@ -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) From 7f7c9e30bf5e92bd43ce0cde4cafb0d5462dd3a4 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 12:11:06 +0200 Subject: [PATCH 2/7] REFACTOR: make plot methods consistent Following #4626 and #4708, theses changes aim at making our code base consistent. The changes consist in returning Matplotlib figure objects on methods that were returning a list of value. In the case of polar_plot_3d_pyvista, a Pyvista plotter object is returned when, before, a boolean was returned if one wanted to show the figure or save it in a file. --- pyaedt/modules/solutions.py | 81 ++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 46 deletions(-) diff --git a/pyaedt/modules/solutions.py b/pyaedt/modules/solutions.py index 8e798b5c0b0..c33f502246b 100644 --- a/pyaedt/modules/solutions.py +++ b/pyaedt/modules/solutions.py @@ -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 @@ -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 @@ -1640,6 +1640,7 @@ def plot_farfield_contour( Returns ------- :class:`matplotlib.pyplot.Figure` + Matplotlib figure object. Examples -------- @@ -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 -------- @@ -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", @@ -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 -------- @@ -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") @@ -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 -------- @@ -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() From 1cdd02edc2c8f645fb8f5969a6994e104c11ceaf Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 14:28:58 +0200 Subject: [PATCH 3/7] FIX: Add missing show argument in plot_2d_chart --- pyaedt/generic/plot.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyaedt/generic/plot.py b/pyaedt/generic/plot.py index bdec675eb1f..0f592e0b4cf 100644 --- a/pyaedt/generic/plot.py +++ b/pyaedt/generic/plot.py @@ -439,8 +439,10 @@ def plot_3d_chart(plot_data, size=(2000, 1000), xlabel="", ylabel="", title="", @pyaedt_function_handler() @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. +def plot_2d_chart( + plot_data, size=(2000, 1000), show_legend=True, xlabel="", ylabel="", title="", snapshot_path=None, show=True +): + """Create a Matplotlib figure based on a list of data. Parameters ---------- From f84ed77c1234bba33187393f0efa97d5df2bfb1b Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 14:29:56 +0200 Subject: [PATCH 4/7] FIX: Add missing show argument in plot methods --- pyaedt/modules/solutions.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pyaedt/modules/solutions.py b/pyaedt/modules/solutions.py index c33f502246b..7660f89162f 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 figure based on a list of data. Parameters ---------- @@ -893,9 +894,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,8 +910,9 @@ def plot_3d( formula=None, size=(2000, 1000), snapshot_path=None, + show=True, ): - """Create a matplotlib 3d plot based on a list of data. + """Create a matplotlib 3D figure based on a list of data. Parameters ---------- @@ -985,7 +987,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): From 627199e5ac55133e28d17b7fd69161e0db97c6a1 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 15:57:14 +0200 Subject: [PATCH 5/7] FIX: Add missing show argument --- pyaedt/generic/plot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyaedt/generic/plot.py b/pyaedt/generic/plot.py index 0f592e0b4cf..a2ed87de43e 100644 --- a/pyaedt/generic/plot.py +++ b/pyaedt/generic/plot.py @@ -385,7 +385,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 From 354704f8f799d5241a12efc31f27128bcc6a1156 Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 15:57:42 +0200 Subject: [PATCH 6/7] FIX: Handle show_legend as argument --- pyaedt/modules/solutions.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pyaedt/modules/solutions.py b/pyaedt/modules/solutions.py index 7660f89162f..9d7f190012e 100644 --- a/pyaedt/modules/solutions.py +++ b/pyaedt/modules/solutions.py @@ -1739,6 +1739,7 @@ def plot_2d_cut( image_path=None, show=True, is_polar=False, + show_legend=True, **kwargs ): # fmt: on @@ -1772,6 +1773,8 @@ def plot_2d_cut( If ``False``, the Matplotlib instance of the plot is shown. is_polar : bool, optional Whether this plot is a polar plot. The default is ``True``. + show_legend : bool, optional + Whether to display the legend or not. The default is ``True``. Returns ------- @@ -1849,9 +1852,9 @@ def plot_2d_cut( return False curves.append([x, y, "{}={}".format(y_key, data[y_key][theta_idx])]) - show_legend = show - if len(curves) > 15: - show_legend = False + # FIXME: See if we need to keep this check on the curves length + # if len(curves) > 15: + # show_legend = False if is_polar: return plot_polar_chart( curves, From 126b11b5613fd487044be3ecbf1d300ee35bfeee Mon Sep 17 00:00:00 2001 From: Sebastien Morais Date: Fri, 24 May 2024 15:58:44 +0200 Subject: [PATCH 7/7] TESTS: Rework test check with Figure and Plotter --- _unittest/test_12_PostProcessing.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/_unittest/test_12_PostProcessing.py b/_unittest/test_12_PostProcessing.py index 2375a7e7acb..03f2ba5bd0b 100644 --- a/_unittest/test_12_PostProcessing.py +++ b/_unittest/test_12_PostProcessing.py @@ -2,7 +2,9 @@ import sys from _unittest.conftest import config +from matplotlib.figure import Figure import pytest +from pyvista.plotting.plotter import Plotter from pyaedt import Circuit from pyaedt import Icepak @@ -597,18 +599,18 @@ def test_70_far_field_data(self): assert ffdata.origin == [0, 0, 1] img1 = os.path.join(self.local_scratch.path, "ff_2d1.jpg") - ffdata.plot_2d_cut(primary_sweep="Theta", secondary_sweep_value="all", image_path=img1) + ffdata.plot_2d_cut(primary_sweep="Theta", secondary_sweep_value="all", image_path=img1, show=False) assert os.path.exists(img1) img2 = os.path.join(self.local_scratch.path, "ff_2d2.jpg") - ffdata.plot_2d_cut(secondary_sweep_value=[0, 1], image_path=img2) + ffdata.plot_2d_cut(secondary_sweep_value=[0, 1], image_path=img2, show=False) assert os.path.exists(img2) img3 = os.path.join(self.local_scratch.path, "ff_2d2.jpg") - ffdata.plot_2d_cut(image_path=img3) + ffdata.plot_2d_cut(image_path=img3, show=False) assert os.path.exists(img3) curve_2d = ffdata.plot_2d_cut(show=False) - assert len(curve_2d[0]) == 3 + assert isinstance(curve_2d, Figure) data = ffdata.polar_plot_3d(show=False) - assert len(data) == 3 + assert isinstance(data, Figure) img4 = os.path.join(self.local_scratch.path, "ff_3d1.jpg") ffdata.polar_plot_3d_pyvista( @@ -623,7 +625,7 @@ def test_70_far_field_data(self): data_pyvista = ffdata.polar_plot_3d_pyvista( quantity="RealizedGain", show=False, background=[255, 0, 0], show_geometry=False, convert_to_db=True ) - assert data_pyvista + assert isinstance(data_pyvista, Plotter) @pytest.mark.skipif(is_linux or sys.version_info < (3, 8), reason="FarFieldSolution not supported by IronPython") def test_71_antenna_plot(self, field_test):