utils.py
funzioni diverse
| FUNCTION | DESCRIPTION |
|---|---|
add_contour_HDR_50 |
Draw the 50% high-density region (HDR) contour on a 2D KDE grid. |
add_marginals |
Compute and plot 1D KDE marginal distributions along the x and y axes. |
build_categorical_palette |
Build a mapping from categories to colours for grouped plots. |
create_density_figure |
Create a Matplotlib figure and axes layout for 2D density plots, with |
draw_contours |
Draw 2D KDE contour lines and/or filled bands, with optional exclusion |
get_category_order |
Determine the plotting order for categorical groups. |
get_default_plot_params |
Return the default configuration dictionary for plots. |
hdr_threshold_from_grid |
Compute the density threshold for a high-density region (HDR) of mass |
kde1d |
Compute a 1D Gaussian kernel density estimate (KDE) on a given grid. |
kde_on_grid |
Compute a 2D Gaussian kernel density estimate (KDE) on a predefined grid. |
order_values_for_frames |
Restituisce l'ordine dei valori da usare per gli slider dei frame. |
set_fig_layout |
Configure axes layout, grid, central axes, diagonals and quadrant labels. |
update_params |
Update default parameters with ones provided by the user |
add_contour_HDR_50
add_contour_HDR_50(ax, XX, YY, ZZ, params, color=None)
Draw the 50% high-density region (HDR) contour on a 2D KDE grid.
This function computes the HDR threshold using hdr_threshold_from_grid
with probability mass params["hdr_p"] (typically 0.5), and plots:
- a filled region between the HDR threshold and the maximum density
- an outline contour at the HDR threshold
Colours for the filled and contour regions can be overridden via the
color argument; otherwise, params["fill_color"] and
params["contour_color"] are used.
| PARAMETER | DESCRIPTION |
|---|---|
ax
|
Axes on which to draw the HDR region.
TYPE:
|
XX
|
2D arrays defining the evaluation grid (as returned by
TYPE:
|
YY
|
2D arrays defining the evaluation grid (as returned by
TYPE:
|
ZZ
|
2D KDE values on the grid. If None, nothing is drawn.
TYPE:
|
params
|
Plot configuration dictionary. The following keys are used: - "hdr_p", "xlim", "ylim" - "fill_color", "fill_alpha" - "contour_color", "contour_width"
TYPE:
|
color
|
Override colour for both filling and outline. If None, defaults
from |
| RETURNS | DESCRIPTION |
|---|---|
ax
|
The same axes, with HDR region drawn (if applicable).
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
add_marginals
add_marginals(x, xi, y, yi, ax_top, ax_right, params, color=None)
Compute and plot 1D KDE marginal distributions along the x and y axes.
The function uses kde1d to estimate the marginals of x and y
on the grids xi and yi, respectively, and draws them on the
provided marginal axes.
| PARAMETER | DESCRIPTION |
|---|---|
x
|
Sample values for the x variable.
TYPE:
|
xi
|
Grid on which to evaluate the x marginal KDE.
TYPE:
|
y
|
Sample values for the y variable.
TYPE:
|
yi
|
Grid on which to evaluate the y marginal KDE.
TYPE:
|
ax_top
|
Axes for the x-direction marginal (top).
TYPE:
|
ax_right
|
Axes for the y-direction marginal (right).
TYPE:
|
params
|
Plot configuration dictionary. The following keys are used: - "marginal_bw" - "marginal_fill_alpha" - "marginal_linewidth" - "fill_color" - "contour_color"
TYPE:
|
color
|
Override colour for both filled area and line. If None, values
from |
| RETURNS | DESCRIPTION |
|---|---|
ax_top
|
The x marginal axes, updated with the marginal plot.
TYPE:
|
ax_right
|
The y marginal axes, updated with the marginal plot.
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | |
build_categorical_palette
build_categorical_palette(categories, palette_param)
Build a mapping from categories to colours for grouped plots.
The palette can be specified in several ways:
- dict: direct mapping
{category: color}. Missing categories default to"grey". - str: name of a Matplotlib colormap. Colours are sampled uniformly along the colormap range.
- list/tuple: sequence of colour specifications. If there are fewer colours than categories, the list is repeated cyclically.
- None or unsupported type: fall back to the "tab20" colormap.
| PARAMETER | DESCRIPTION |
|---|---|
categories
|
Sequence of category labels.
TYPE:
|
palette_param
|
Palette specification as described above. |
| RETURNS | DESCRIPTION |
|---|---|
color_map
|
Dictionary mapping each category in
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 | |
create_density_figure
create_density_figure(params)
Create a Matplotlib figure and axes layout for 2D density plots, with optional marginal axes.
If params["show_marginals"] is True, the function creates a 2x2
GridSpec layout with:
- top-left: x-axis marginal KDE (ax_top)
- bottom-left: main density plot (ax)
- bottom-right: y-axis marginal KDE (ax_right)
The size of the marginal axes relative to the main axes is controlled by
params["marginal_height_ratio"]. If show_marginals is False, only a
single main axes is created.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Plot configuration dictionary, typically from
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
fig
|
The created Figure.
TYPE:
|
ax
|
The main axes for the 2D density plot.
TYPE:
|
ax_top
|
Axes for the x-direction marginal KDE, or None if
TYPE:
|
ax_right
|
Axes for the y-direction marginal KDE, or None if
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | |
draw_contours
draw_contours(params, ax_, XX, YY, Z, color=None, cmap=None, levels=5, filled=False, lw=1, alpha=1)
Draw 2D KDE contour lines and/or filled bands, with optional exclusion of low-density regions.
The function supports two modes for levels:
- integer: number of density bands; levels are computed automatically
between Z.min() and Z.max(), applying skip_low_levels or
min_frac from params.
- array-like: explicit sequence of boundary levels, possibly filtered
by min_frac or skip_low_levels.
When filled=True, contour bands are drawn with contourf, optionally
using a colormap that fades from transparent to color. Contour lines
can be drawn on top.
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Plot configuration dictionary. The following keys are used: - "skip_low_levels" : int - "min_frac" : float or None - "extend" : {"neither", "min", "max", "both"}
TYPE:
|
ax_
|
Axes on which to draw the contours.
TYPE:
|
XX
|
2D arrays defining the evaluation grid.
TYPE:
|
YY
|
2D arrays defining the evaluation grid.
TYPE:
|
Z
|
2D array of density values on the grid. If None, nothing is drawn.
TYPE:
|
color
|
Base colour for lines and, when |
cmap
|
Colormap for filled contours. If None and
TYPE:
|
levels
|
Number of contour bands (if int) or array of boundary levels.
When |
filled
|
If True, draw filled contours (
TYPE:
|
lw
|
Line width for contour lines.
TYPE:
|
alpha
|
Opacity for filled contours (0–1).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
The function modifies |
Source code in src/smellscapy/plotting/utils.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 | |
get_category_order
get_category_order(params)
Determine the plotting order for categorical groups.
This helper is intended to build an ordered list of categories based on
a preferred order in params["category_order"], falling back to the
natural (sorted) order of the underlying categorical variable.
Notes
The implementation assumes the existence of a categorical Series
cats in the enclosing scope, with categories cats.cat.categories.
It then:
- If
params["category_order"]is not None, filters it to keep only categories present incats.cat.categoriesand keeps that order. - Appends any remaining categories from
cats.cat.categoriesthat are not already in the filtered list. - Reorders
catsusingcats.cat.reorder_categories(order, ordered=True).
| PARAMETER | DESCRIPTION |
|---|---|
params
|
Plot configuration dictionary. The following key is used: - "category_order" : list or None
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
order
|
Final list of category labels in plotting order.
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 | |
get_default_plot_params
get_default_plot_params()
Return the default configuration dictionary for plots.
The returned dictionary contains default values for axis limits, labels, figure size, 2D KDE contour settings, scatter plot appearance, grouping and colour handling, quadrant labels, marginal distributions, and saving options.
| PARAMETER | DESCRIPTION |
|---|---|
xlim
|
Default x-axis limits (pleasantness), initialised to (-1, 1). |
ylim
|
Default y-axis limits (presence), initialised to (-1, 1). |
figsize
|
Figure size in inches, default (8, 8). |
xlabel
|
Label for the x-axis, default "Pleasantness".
TYPE:
|
ylabel
|
Label for the y-axis, default "Presence".
TYPE:
|
levels
|
Number of contour bands or explicit boundary levels. By default, 10 levels are used. |
filled
|
If True, use filled contours (e.g.
TYPE:
|
extend
|
Argument passed to Matplotlib for handling out-of-range values in filled contours. |
contour_linewidth
|
Line width used for contour lines (legacy / backup parameter).
TYPE:
|
contour_color
|
Default colour for contour lines.
TYPE:
|
contour_width
|
Line width for HDR or contour outlines.
TYPE:
|
fill_color
|
Default fill colour for filled areas.
TYPE:
|
fill_alpha
|
Opacity for filled regions (0–1).
TYPE:
|
skip_low_levels
|
Number of lowest-density bands to drop (0 means keep all).
TYPE:
|
min_frac
|
If set to a value in [0, 1], discard all contour levels below
TYPE:
|
axis_line_color
|
Colour of the central horizontal and vertical axes (x=0, y=0).
TYPE:
|
axis_line_style
|
Line style for central axes.
TYPE:
|
axis_line_width
|
Line width for central axes.
TYPE:
|
diag_color
|
Colour of the ±45° diagonals.
TYPE:
|
diag_style
|
Line style for diagonals.
TYPE:
|
diag_width
|
Line width for diagonals.
TYPE:
|
xmajor_step
|
Spacing for major and minor ticks on the x-axis.
TYPE:
|
xminor_step
|
Spacing for major and minor ticks on the x-axis.
TYPE:
|
ymajor_step
|
Spacing for major and minor ticks on the y-axis.
TYPE:
|
yminor_step
|
Spacing for major and minor ticks on the y-axis.
TYPE:
|
grid_major
|
Style dictionary for major grid lines (linestyle, linewidth, alpha).
TYPE:
|
grid_minor
|
Style dictionary for minor grid lines.
TYPE:
|
minor_tick_length
|
Length of minor tick marks; default 0 (invisible).
TYPE:
|
eval_n
|
Number of evaluation points per axis for the 2D KDE grid.
TYPE:
|
hdr_p
|
Probability mass of the high-density region (HDR), default 0.5.
TYPE:
|
show_points
|
Whether to overlay individual data points.
TYPE:
|
point_size
|
Marker size for scatter points.
TYPE:
|
point_alpha
|
Transparency for scatter points.
TYPE:
|
point_color
|
Default colour for scatter points. |
group_by_col
|
Name of the column used for categorical grouping.
TYPE:
|
palette
|
Palette specification used by |
legend_loc
|
Legend location string (passed to Matplotlib).
TYPE:
|
category_order
|
Optional ordering of categories for plotting and legends.
TYPE:
|
show_quadrant_labels
|
Whether to show textual labels in the four quadrants.
TYPE:
|
labels
|
Mapping of label identifiers to positions and text (e.g. "Overpowering", "Detached", etc.).
TYPE:
|
labels_style
|
Text style for quadrant labels (fontsize, fontstyle, alpha, ...).
TYPE:
|
show_marginals
|
Whether to create axes and draw 1D KDE marginals.
TYPE:
|
marginal_height_ratio
|
Relative size of top and right marginal axes w.r.t. main axes.
TYPE:
|
marginal_linewidth
|
Line width of marginal KDE curves.
TYPE:
|
marginal_fill_alpha
|
Opacity of 1D KDE filled regions.
TYPE:
|
marginal_bw
|
Bandwidth for 1D KDE; if None, GaussianKDE defaults are used.
TYPE:
|
savefig
|
Flag indicating whether saving is expected downstream.
TYPE:
|
dpi
|
Default resolution in dots per inch for saved figures.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
params
|
Dictionary containing all default plotting parameters.
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 | |
hdr_threshold_from_grid
hdr_threshold_from_grid(zi, p, xlim, ylim)
Compute the density threshold for a high-density region (HDR) of mass p
from a 2D KDE grid.
The function assumes that zi has been evaluated on a regular rectangular
grid spanning [xlim[0], xlim[1]] × [ylim[0], ylim[1]]. It computes the
cell area, sorts the density values in descending order and finds the
threshold such that the cumulative integral reaches p times the total
mass.
| PARAMETER | DESCRIPTION |
|---|---|
zi
|
2D array of density values on a regular grid.
TYPE:
|
p
|
Target probability mass of the HDR (0 < p ≤ 1).
TYPE:
|
xlim
|
x-axis limits of the grid. |
ylim
|
y-axis limits of the grid. |
| RETURNS | DESCRIPTION |
|---|---|
threshold
|
Density value defining the HDR boundary, i.e. the smallest
value such that the region {z >= threshold} contains mass
TYPE:
|
zmax
|
Maximum density value in
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 | |
kde1d
kde1d(values, grid, bw=None)
Compute a 1D Gaussian kernel density estimate (KDE) on a given grid.
| PARAMETER | DESCRIPTION |
|---|---|
values
|
Input sample values. Non-finite values are filtered out before computing the KDE.
TYPE:
|
grid
|
Points at which to evaluate the 1D KDE.
TYPE:
|
bw
|
Bandwidth specification passed to |
| RETURNS | DESCRIPTION |
|---|---|
density
|
KDE evaluated on
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 | |
kde_on_grid
kde_on_grid(x_sub, y_sub, XX, YY)
Compute a 2D Gaussian kernel density estimate (KDE) on a predefined grid.
| PARAMETER | DESCRIPTION |
|---|---|
x_sub
|
1D array of x values.
TYPE:
|
y_sub
|
1D array of y values. Must have the same length as
TYPE:
|
XX
|
2D array of x-coordinates defining the evaluation grid (e.g. from
TYPE:
|
YY
|
2D array of y-coordinates defining the evaluation grid.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ZZ
|
2D array of KDE values evaluated on (XX, YY) and reshaped to
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 | |
order_values_for_frames
order_values_for_frames(s, order_override=None)
Restituisce l'ordine dei valori da usare per gli slider dei frame.
Comportamento:
- Se l'utente fornisce un ordine esplicito tramite order_override,
quello viene usato (dopo filtraggio dei valori presenti).
- Se la serie è datetime → ordine cronologico crescente.
- Altrimenti → ordine di prima apparizione nella serie.
Source code in src/smellscapy/plotting/utils.py
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
set_fig_layout
set_fig_layout(ax, params)
Configure axes layout, grid, central axes, diagonals and quadrant labels.
This function applies a consistent visual style to the main 2D pleasantness– presence axes, including axis limits and labels, tick locators, grid styles, central axes at x=0 and y=0, diagonal reference lines, and optional quadrant labels.
| PARAMETER | DESCRIPTION |
|---|---|
ax
|
Axes object to configure.
TYPE:
|
params
|
Plot configuration dictionary. The following keys are used:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ax
|
The same axes object, configured.
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
update_params
update_params(params, **kwargs)
Update default parameters with ones provided by the user
| PARAMETER | DESCRIPTION |
|---|---|
params
|
A dictionary with default parameters
TYPE:
|
**kwargs
|
Key-value pairs used to update
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
params
|
Updated dictionary
TYPE:
|
Source code in src/smellscapy/plotting/utils.py
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 | |