From e044296e1614d4ebf6ceefaf171f8be513f9d510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bel=C3=A1k?= Date: Fri, 28 Jul 2023 13:55:37 +0200 Subject: [PATCH 1/3] feat: Add `equal_scale_axes` parameter to `scatter_plot_2d`. --- edvart/plots.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/edvart/plots.py b/edvart/plots.py index f8e2839..603a56c 100644 --- a/edvart/plots.py +++ b/edvart/plots.py @@ -29,6 +29,7 @@ def scatter_plot_2d( show_xticks: bool = False, show_yticks: bool = False, show_zerolines: bool = False, + equal_scale_axes: bool = False, ) -> None: """Display a 2D scatter plot of x and y, with optional coloring of points by values in a column. @@ -60,6 +61,8 @@ def scatter_plot_2d( Whether to display ticks on the y axis. show_zerolines : bool (default = False) Whether to display zero lines. + equal_scale_axes : bool (default = False) + Whether to make the x and y axes have the same scale. """ if isinstance(x, str): x = df[x] @@ -78,6 +81,7 @@ def scatter_plot_2d( show_xticks=show_xticks, show_yticks=show_yticks, show_zerolines=show_zerolines, + equal_scale_axes=equal_scale_axes, ) @@ -93,6 +97,7 @@ def _scatter_plot_2d_noninteractive( show_xticks: bool = False, show_yticks: bool = False, show_zerolines: bool = False, + equal_scale_axes: bool = False, ) -> None: _fig, ax = plt.subplots(figsize=figsize) if color_col is not None: @@ -123,6 +128,8 @@ def _scatter_plot_2d_noninteractive( ax.set_yticks([]) if not show_zerolines: ax.grid(False) + if equal_scale_axes: + ax.set_aspect("equal") plt.show() @@ -138,6 +145,7 @@ def _scatter_plot_2d_interactive( show_xticks: bool = False, show_yticks: bool = False, show_zerolines: bool = False, + equal_scale_axes: bool = False, ) -> None: layout = go.Layout( width=figsize[0] * _INCHES_TO_PIXELS, @@ -150,6 +158,9 @@ def _scatter_plot_2d_interactive( ), legend=go.layout.Legend(title=f"{color_col}"), ) + if equal_scale_axes: + layout.yaxis.scaleanchor = "x" + layout.yaxis.scaleratio = 1 fig = go.Figure(layout=layout) if color_col is not None: is_color_categorical = utils.is_categorical(df[color_col]) or not is_numeric(df[color_col]) From a26fe5d8bdb55cd25e591288d8197323f76b969a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bel=C3=A1k?= Date: Fri, 28 Jul 2023 13:56:52 +0200 Subject: [PATCH 2/3] feat: use equal scale axes for PCA scatter plot --- edvart/report_sections/multivariate_analysis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/edvart/report_sections/multivariate_analysis.py b/edvart/report_sections/multivariate_analysis.py index 63e458f..a286705 100644 --- a/edvart/report_sections/multivariate_analysis.py +++ b/edvart/report_sections/multivariate_analysis.py @@ -354,6 +354,7 @@ def pca_first_vs_second( show_xticks=True, show_yticks=True, show_zerolines=True, + equal_scale_axes=True, ) print(f"Explained variance ratio: {pca.explained_variance_ratio_[:2].sum() * 100 :.2f}%") From 117a64d5fcb72d03642a36c7c813b7017d0e1426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Bel=C3=A1k?= Date: Thu, 10 Aug 2023 11:48:11 +0200 Subject: [PATCH 3/3] fix(review): scale axes by limits, not box size --- edvart/plots.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/edvart/plots.py b/edvart/plots.py index 603a56c..30d2679 100644 --- a/edvart/plots.py +++ b/edvart/plots.py @@ -129,7 +129,7 @@ def _scatter_plot_2d_noninteractive( if not show_zerolines: ax.grid(False) if equal_scale_axes: - ax.set_aspect("equal") + ax.set_aspect("equal", "datalim") plt.show()