diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 6275033..2347cab 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.7.3 +current_version = 0.7.4 commit = False tag = False diff --git a/genetools/__init__.py b/genetools/__init__.py index 723a835..bfaf1e0 100644 --- a/genetools/__init__.py +++ b/genetools/__init__.py @@ -2,7 +2,7 @@ __author__ = """Maxim Zaslavsky""" __email__ = "maxim@maximz.com" -__version__ = "0.7.3" +__version__ = "0.7.4" # We want genetools.[submodule].[function] to be accessible simply by importing genetools, without having to import genetools.[submodule] from . import helpers, plots, stats, arrays diff --git a/genetools/plots.py b/genetools/plots.py index 0426a8d..e8dab8a 100644 --- a/genetools/plots.py +++ b/genetools/plots.py @@ -879,7 +879,8 @@ def size_norm(x: Union[np.ndarray, pd.Series, List[float]]) -> np.ndarray: ) else: plot_vmin, plot_vmax = color_vmin, color_vmax - color_norm = None + # Create a norm even when vcenter is None + color_norm = matplotlib.colors.Normalize(vmin=plot_vmin, vmax=plot_vmax) scatter = ax.scatter( data[x_axis_key].values, @@ -987,11 +988,11 @@ def size_norm(x: Union[np.ndarray, pd.Series, List[float]]) -> np.ndarray: representative_colors = np.clip( representative_colors, color_vmin, color_vmax ) - if color_norm is not None: - # Apply a color norm - representative_colors = [ - color_norm(value) for value in representative_colors - ] + + # Apply color norm + # Always normalize colors using the same norm as the scatter plot + representative_colors = color_norm(representative_colors) + # Apply color cmap # First, cast string cmaps to a callable function color_cmap_func = matplotlib.cm.get_cmap(color_cmap) diff --git a/setup.py b/setup.py index ed47e87..a35a887 100644 --- a/setup.py +++ b/setup.py @@ -49,7 +49,7 @@ test_suite="tests", tests_require=test_requirements, url="https://github.com/maximz/genetools", - version="0.7.3", + version="0.7.4", zip_safe=False, extras_require={ # This will also install anndata and UMAP packages diff --git a/tests/baseline/test_dotplot_confirm_legend_colors_match_dot_colors.png b/tests/baseline/test_dotplot_confirm_legend_colors_match_dot_colors.png new file mode 100644 index 0000000..713ba0f Binary files /dev/null and b/tests/baseline/test_dotplot_confirm_legend_colors_match_dot_colors.png differ diff --git a/tests/baseline/test_dotplot_single_key.png b/tests/baseline/test_dotplot_single_key.png index 5ffafaa..259c937 100644 Binary files a/tests/baseline/test_dotplot_single_key.png and b/tests/baseline/test_dotplot_single_key.png differ diff --git a/tests/test_plots.py b/tests/test_plots.py index f4d4355..76403c8 100644 --- a/tests/test_plots.py +++ b/tests/test_plots.py @@ -725,6 +725,34 @@ def test_dotplot_single_key(dotplot_data): return fig +@snapshot_image +def test_dotplot_confirm_legend_colors_match_dot_colors(): + # Regression test to confirm that the colors in the legend match the colors of the dots, for data where the max value is a small positive float. + df = pd.DataFrame( + [ + {"feature": "feature1", "value": 0.12}, + {"feature": "feature2", "value": 0.02}, + {"feature": "feature3", "value": 0.03}, + {"feature": "feature4", "value": 0.0}, + {"feature": "feature5", "value": 0.08}, + {"feature": "feature6", "value": 0.03}, + {"feature": "feature7", "value": 0.06}, + {"feature": "feature8", "value": 0.0}, + ] + ).assign(classname="class1") + with sns.plotting_context("paper"), sns.axes_style("white"): + fig, _ = genetools.plots.plot_color_and_size_dotplot( + data=df, + x_axis_key="classname", + y_axis_key="feature", + value_key="value", + color_cmap=sns.color_palette("magma_r", as_cmap=True), + figsize=(5, 6), + grid=False, + ) + return fig + + ####