Skip to content

Commit 6be1b41

Browse files
timtreisclaude
andauthored
Fix outline linewidth mismatch between matplotlib and datashader (#543)
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 264bc92 commit 6be1b41

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/spatialdata_plot/pl/render.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,20 @@ def _render_shapes(
342342
agg = cvs.polygons(transformed_element, geometry="geometry", agg=ds.count())
343343

344344
# render outlines if needed
345+
# outline_linewidth is in points (1pt = 1/72 inch); datashader line_width is in canvas pixels
346+
ds_lw_factor = fig_params.fig.dpi / 72
345347
assert len(render_params.outline_alpha) == 2 # shut up mypy
346348
if render_params.outline_alpha[0] > 0:
347349
agg_outlines = cvs.line(
348350
transformed_element,
349351
geometry="geometry",
350-
line_width=render_params.outline_params.outer_outline_linewidth,
352+
line_width=render_params.outline_params.outer_outline_linewidth * ds_lw_factor,
351353
)
352354
if render_params.outline_alpha[1] > 0:
353355
agg_inner_outlines = cvs.line(
354356
transformed_element,
355357
geometry="geometry",
356-
line_width=render_params.outline_params.inner_outline_linewidth,
358+
line_width=render_params.outline_params.inner_outline_linewidth * ds_lw_factor,
357359
)
358360

359361
ds_span = None
533 Bytes
Loading

tests/pl/test_render_shapes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,3 +999,17 @@ def test_plot_datashader_single_category(sdata_blobs: SpatialData):
999999
sdata_blobs["table"] = table
10001000

10011001
sdata_blobs.pl.render_shapes(element="blobs_polygons", color="category", method="datashader").pl.show()
1002+
1003+
1004+
def test_datashader_outline_width_uses_points_units(sdata_blobs: SpatialData):
1005+
"""Datashader outlines should scale with DPI like matplotlib (points, not raw pixels).
1006+
1007+
Regression test for https://github.com/scverse/spatialdata-plot/issues/493.
1008+
"""
1009+
sdata_blobs.pl.render_shapes(
1010+
element="blobs_polygons", method="datashader", outline_alpha=1.0, outline_width=10.0
1011+
).pl.show()
1012+
1013+
sdata_blobs.pl.render_shapes(
1014+
element="blobs_polygons", method="datashader", outline_alpha=1.0, outline_width=(8.0, 3.0)
1015+
).pl.show()

0 commit comments

Comments
 (0)