Skip to content

Commit

Permalink
automatically interpret color/colormap for the categorical plot
Browse files Browse the repository at this point in the history
  • Loading branch information
hanjinliu committed Sep 8, 2024
1 parent 77aec8f commit 3336fbf
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
8 changes: 5 additions & 3 deletions whitecanvas/canvas/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1864,7 +1864,7 @@ def add_image(
image: ArrayLike,
*,
name: str | None = None,
cmap: ColormapType = "gray",
cmap: ColormapType | None = None,
clim: tuple[float | None, float | None] | None = None,
flip_canvas: bool = True,
lock_aspect: bool = True,
Expand All @@ -1880,8 +1880,9 @@ def add_image(
image : ArrayLike
Image data. Must be 2D or 3D array. If 3D, the last dimension must be
RGB(A). Note that the first dimension is the vertical axis.
cmap : ColormapType, default "gray"
Colormap used for the image.
cmap : ColormapType, optional
Colormap used for the image. If None, the theme default for image colormap
will be used.
clim : (float or None, float or None) or None
Contrast limits. If None, the limits are automatically determined by min and
max of the data. You can also pass None separately to either limit to use
Expand All @@ -1896,6 +1897,7 @@ def add_image(
Image
The image layer.
"""
cmap = theme._default("colormap_image", cmap)
layer = _l.Image(
image, name=name, cmap=cmap, clim=clim, backend=self._get_backend()
)
Expand Down
4 changes: 3 additions & 1 deletion whitecanvas/canvas/_dims.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from psygnal import Signal, SignalGroup

from whitecanvas import layers as _l
from whitecanvas import theme
from whitecanvas._axis import DimAxis, RangeAxis
from whitecanvas._exceptions import ReferenceDeletedError
from whitecanvas.layers import _ndim as _ndl
Expand Down Expand Up @@ -529,7 +530,7 @@ def add_image(
image: Any,
*,
name: str | None = None,
cmap: ColormapType = "gray",
cmap: ColormapType | None = None,
clim: tuple[float | None, float | None] | None = None,
rgb: bool = False,
flip_canvas: bool = True,
Expand All @@ -553,6 +554,7 @@ def add_image(
"""
canvas = self._get_canvas()
name = canvas._coerce_name(name)
cmap = theme._default("colormap_image", cmap)
stack = _ndl.ImageLayerStack.from_layer_class(
_l.Image, image, name=name, cmap=cmap, clim=clim, rgb=rgb,
backend=canvas._get_backend(),
Expand Down
8 changes: 6 additions & 2 deletions whitecanvas/canvas/dataframe/_feature_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from whitecanvas.canvas.dataframe._base import BaseCatPlotter
from whitecanvas.layers import tabular as _lt
from whitecanvas.layers.tabular import _jitter
from whitecanvas.layers.tabular import _plans as _p
from whitecanvas.types import HistBinType, KdeBandWidthType, Orientation

if TYPE_CHECKING:
Expand Down Expand Up @@ -205,7 +206,11 @@ def add_markers(
self._df, xj, yj, name=name, color=color, hatch=hatch,
size=size, symbol=symbol, backend=canvas._get_backend(),
) # fmt: skip
if color is not None and not layer._color_by.is_const():
if (
color is not None
and not layer._color_by.is_const()
and isinstance(layer._color_by, _p.ColorPlan)
):
layer.update_color(layer._color_by.by, palette=canvas._color_palette)
elif color is None:
layer.update_color(canvas._color_palette.next())
Expand All @@ -219,7 +224,6 @@ def add_hist2d(
bins: HistBinType | tuple[HistBinType, HistBinType] = "auto",
rangex: tuple[float, float] | None = None,
rangey: tuple[float, float] | None = None,
cmap=None, # deprecated
) -> _lt.DFMultiHeatmap[_DF]:
"""
Add 2-D histogram of given x/y columns.
Expand Down
2 changes: 1 addition & 1 deletion whitecanvas/layers/tabular/_marker_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def _update_color_or_colormap(self, by: str | Iterable[str]) -> Self:
and by in self._source
and self._source[by].dtype.kind in "fiu"
):
return self.update_colormap(by)
return self.update_colormap(by, theme._default("colormap_categorical"))

Check warning on line 125 in whitecanvas/layers/tabular/_marker_like.py

View check run for this annotation

Codecov / codecov/patch

whitecanvas/layers/tabular/_marker_like.py#L125

Added line #L125 was not covered by tests
self.update_color(by)
return self

Expand Down
2 changes: 1 addition & 1 deletion whitecanvas/theme/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_theme(name: str | Theme | None = None) -> Theme:
return _EXISTING_THEMES[name]


def _default(attr: str, value) -> Any:
def _default(attr: str, value=None) -> Any:
if value is not None:
return value
out = _DEFAULT_THEME
Expand Down
2 changes: 2 additions & 0 deletions whitecanvas/theme/_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ class Theme(_BaseModel):
background_color: Color = _field(Color("white"))
canvas_size: tuple[float, float] = _field((800, 600), _validate_canvas_size)
palette: Colormap = _field(Colormap("tab10"), Colormap)
colormap_categorical: Colormap = _field(Colormap("viridis"), Colormap)
colormap_image: Colormap = _field(Colormap("gray"), Colormap)


LIGHT_THEME = Theme()
Expand Down

0 comments on commit 3336fbf

Please sign in to comment.