legendgram
Create a histogram legend for a specified column in a GeoDataFrame.
Note
"Legendgrams are map legends that visualize the distribution of observations by color in a given map."
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gdf
|
GeoDataFrame
|
The GeoDataFrame containing segmented objects. |
required |
column
|
str
|
The column/feature name to create the legend for. This needs to be numeric. |
required |
n_bins
|
int
|
The number of bins to use for the histogram. |
100
|
cmap
|
str
|
The name of the matplotlib colormap to use for the legend. |
'viridis'
|
breaks
|
ndarray
|
Custom breaks for the histogram. If None, breaks will be calculated based on the data in the specified column. If provided, should be a 1D array of numeric values that define the bin edges. |
None
|
frame_on
|
bool
|
Whether to draw a frame around the legend. |
False
|
add_mean
|
bool
|
Whether to add a vertical line for the mean of the specified column. |
True
|
add_median
|
bool
|
Whether to add a vertical line for the median of the specified column. |
False
|
lw
|
float
|
Line width for the mean/median line. Ignored if both |
2
|
lc
|
str
|
Line color for the mean/median line. Ignored if both |
'black'
|
ticks
|
int | List[float]
|
Number of x-ticks or an array of explicit tick locations for the x-axis. |
None
|
tick_params
|
dict
|
Extra parameters for the tick labels. |
{'labelsize': 10}
|
ax
|
Axes
|
The axes to draw the legend on. If None, a new axes will be created and the legend will be returned as standalone plt.Axes. |
None
|
loc
|
str
|
The location of the legend. One of: "upper left", "upper center", "upper right",
"center left", "center", "center right", "lower left", "lower center", "lower right".
Ignored if |
'lower left'
|
legend_size
|
Tuple[str, str] | Tuple[float, float]
|
The size (width, height) of the legend. If the values are floats, the size is
given in inches, e.g. (1.3, 1.0). If the values are strings, the size is in relative
units to the given input axes, e.g. ("40%", "25%") means 40% of the width and 25%
of the height of the input axes. Ignored if |
('40%', '25%')
|
Returns:
Type | Description |
---|---|
Axes
|
plt.Axes:
The axes containing the histogram legend. If |
Examples:
>>> from histolytics.data import cervix_tissue, cervix_nuclei
>>> from histolytics.spatial_ops.ops import get_objs
>>> from histolytics.spatial_geom.shape_metrics import shape_metric
>>>
>>> # Get the cervix nuclei and tissue data
>>> nuc = cervix_nuclei()
>>> tis = cervix_tissue()
>>> # Filter the tissue data for CIN lesions and get the neoplastic nuclei
>>> lesion = tis[tis["class_name"] == "cin"]
>>> neo = get_objs(lesion, nuc)
>>> neo = neo[neo["class_name"] == "neoplastic"]
>>> # Calculate the eccentricity for the neoplastic nuclei
>>> neo = shape_metric(neo, ["eccentricity"])
>>>
>>> # Plot the neoplastic nuclei with eccentricity as a color scale
>>> col = "eccentricity"
>>> ax = nuc.plot(
... column="class_name",
... figsize=(6, 6),
... aspect=1,
... alpha=0.5,
... )
>>> ax = neo.plot(
... ax=ax,
... column=col,
... legend=False,
... cmap="turbo",
... )
>>> ax.set_axis_off()
>>>
>>> # Add a legendgram to the plot
>>> legendgram(
... neo,
... column=col,
... ax=ax,
... n_bins=50,
... cmap="turbo",
... frame_on=False,
... lw=2,
... lc="black",
... ticks=3,
... legend_size=("30%", "20%"),
... )
Source code in src/histolytics/utils/plot.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|