Skip to content

Commit 4dd12f2

Browse files
render_shapes now has a scale parameter (#152)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f78e0c7 commit 4dd12f2

File tree

7 files changed

+25
-12
lines changed

7 files changed

+25
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning][].
1313
### Added
1414

1515
- Multipolygons are now handled correctly (#93)
16+
- Can now scale shapes (#152)
1617
- Can now plot columns from GeoDataFrame (#149)
1718

1819
### Fixed

src/spatialdata_plot/pl/basic.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def render_shapes(
145145
elements: str | list[str] | None = None,
146146
color: str | None = None,
147147
groups: str | Sequence[str] | None = None,
148-
size: float = 1.0,
148+
scale: float = 1.0,
149149
outline: bool = False,
150150
outline_width: float = 1.5,
151151
outline_color: str | list[float] = "#000000ff",
@@ -171,7 +171,7 @@ def render_shapes(
171171
groups
172172
For discrete annotation in ``color``, select which values
173173
to plot (other values are set to NAs).
174-
size
174+
scale
175175
Value to scale circles, if present.
176176
outline
177177
If `True`, a thin border around points/shapes is plotted.
@@ -212,11 +212,12 @@ def render_shapes(
212212
na_color=na_color, # type: ignore[arg-type]
213213
**kwargs,
214214
)
215-
outline_params = _set_outline(size, outline, outline_width, outline_color)
215+
outline_params = _set_outline(outline, outline_width, outline_color)
216216
sdata.plotting_tree[f"{n_steps+1}_render_shapes"] = ShapesRenderParams(
217217
elements=elements,
218218
color=color,
219219
groups=groups,
220+
scale=scale,
220221
outline_params=outline_params,
221222
layer=layer,
222223
cmap_params=cmap_params,

src/spatialdata_plot/pl/render.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def _render_shapes(
102102
shapes = gpd.GeoDataFrame(shapes, geometry="geometry")
103103
_cax = _get_collection_shape(
104104
shapes=shapes,
105-
s=render_params.size,
105+
s=render_params.scale,
106106
c=color_vector,
107107
render_params=render_params,
108108
rasterized=sc_settings._vector_friendly,

src/spatialdata_plot/pl/render_params.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ShapesRenderParams:
7777
palette: ListedColormap | str | None = None
7878
outline_alpha: float = 1.0
7979
fill_alpha: float = 0.3
80-
size: float = 1.0
80+
scale: float = 1.0
8181
transfunc: Callable[[float], float] | None = None
8282

8383

src/spatialdata_plot/pl/utils.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def _get_collection_shape(
185185
Args:
186186
- shapes (list[GeoDataFrame]): List of geometrical shapes.
187187
- c: Color parameter.
188-
- s (float): Size of the shape.
188+
- s (float): Scale of the shape.
189189
- norm: Normalization for the color map.
190190
- fill_alpha (float, optional): Opacity for the fill color.
191191
- outline_alpha (float, optional): Opacity for the outline.
@@ -241,21 +241,30 @@ def assign_fill_and_outline_to_row(
241241
geom = row["geometry"]
242242
if geom.geom_type == "Polygon":
243243
row = row.to_dict()
244-
row["geometry"] = mplp.Polygon(geom.exterior.coords, closed=True)
244+
coords = np.array(geom.exterior.coords)
245+
centroid = np.mean(coords, axis=0)
246+
scaled_coords = [(centroid + (np.array(coord) - centroid) * s).tolist() for coord in geom.exterior.coords]
247+
row["geometry"] = mplp.Polygon(scaled_coords, closed=True)
245248
assign_fill_and_outline_to_row(shapes, fill_c, outline_c, row, idx)
246249
rows.append(row)
247250

248251
elif geom.geom_type == "MultiPolygon":
249-
mp = _make_patch_from_multipolygon(geom)
250-
for _, m in enumerate(mp):
252+
# mp = _make_patch_from_multipolygon(geom)
253+
for polygon in geom.geoms:
251254
mp_copy = row.to_dict()
252-
mp_copy["geometry"] = m
255+
coords = np.array(polygon.exterior.coords)
256+
centroid = np.mean(coords, axis=0)
257+
scaled_coords = [(centroid + (coord - centroid) * s).tolist() for coord in coords]
258+
mp_copy["geometry"] = mplp.Polygon(scaled_coords, closed=True)
253259
assign_fill_and_outline_to_row(shapes, fill_c, outline_c, mp_copy, idx)
254260
rows.append(mp_copy)
255261

256262
elif geom.geom_type == "Point":
257263
row = row.to_dict()
258-
row["geometry"] = mplp.Circle((geom.x, geom.y), radius=row["radius"])
264+
scaled_radius = row["radius"] * s
265+
row["geometry"] = mplp.Circle(
266+
(geom.x, geom.y), radius=scaled_radius
267+
) # Circle is always scaled from its center
259268
assign_fill_and_outline_to_row(shapes, fill_c, outline_c, row, idx)
260269
rows.append(row)
261270

@@ -576,7 +585,6 @@ def _prepare_cmap_norm(
576585

577586

578587
def _set_outline(
579-
size: float,
580588
outline: bool = False,
581589
outline_width: float = 1.5,
582590
outline_color: str | list[float] = "#0000000ff", # black, white
4.77 KB
Loading

tests/pl/test_render_shapes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ def test_plot_can_color_from_geodataframe(self, sdata_blobs: SpatialData):
9797
elements="blobs_polygons",
9898
color="value",
9999
).pl.show()
100+
101+
def test_plot_can_scale_shapes(self, sdata_blobs: SpatialData):
102+
sdata_blobs.pl.render_shapes(elements="blobs_circles", scale=0.5).pl.show()

0 commit comments

Comments
 (0)