Skip to content

Commit

Permalink
Fix dotplot legend color norm. Legend color weren't being normalized …
Browse files Browse the repository at this point in the history
…the same way as the scatter plot colors.
  • Loading branch information
maximz committed Oct 23, 2024
1 parent 420cf49 commit a6d7006
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.7.3
current_version = 0.7.4
commit = False
tag = False

Expand Down
2 changes: 1 addition & 1 deletion genetools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions genetools/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/baseline/test_dotplot_single_key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


####


Expand Down

0 comments on commit a6d7006

Please sign in to comment.