Skip to content

Plotting Functions

Plotting functions for visualizing circumplex data.

FUNCTION DESCRIPTION
allocate_subplot_axes

Allocate the subplot axes based on the number of data subsets.

create_circumplex_subplots

Create a figure with subplots containing circumplex plots.

create_iso_subplots

Create a set of subplots displaying data visualizations for soundscape analysis.

density

Plot a density plot of ISOCoordinates.

density_plot

Wrapper for the density function to maintain backwards compatibility.

iso_annotation

Add text annotations to circumplex plot based on coordinate values.

iso_plot

Plot a soundscape visualization based on the specified metrics using different

jointplot

Create a jointplot with a central distribution and marginal plots.

scatter

Plot ISOcoordinates as scatter points on a soundscape circumplex grid.

scatter_plot

Wrapper for the scatter function to maintain backwards compatibility.

allocate_subplot_axes

allocate_subplot_axes(nrows, ncols, n_subplots)

Allocate the subplot axes based on the number of data subsets.

PARAMETER DESCRIPTION
nrows

Number of rows for subplots. Can be None to auto-determine.

TYPE: int | None

ncols

Number of columns for subplots. Can be None to auto-determine.

TYPE: int | None

n_subplots

Total number of subplots needed.

TYPE: int

RETURNS DESCRIPTION
tuple[int, int]

The number of rows and columns for the subplot grid.

Notes

Logic for determining subplot layout: - If both nrows and ncols are specified, use those values - If both are None, calculate a grid as close to square as possible - If only one is specified, calculate the other to fit all subplots

Source code in soundscapy/plotting/plot_functions.py
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
599
600
601
602
603
604
def allocate_subplot_axes(
    nrows: int | None, ncols: int | None, n_subplots: int
) -> tuple[int, int, int]:
    """
    Allocate the subplot axes based on the number of data subsets.

    Parameters
    ----------
    nrows : int | None
        Number of rows for subplots. Can be None to auto-determine.
    ncols : int | None
        Number of columns for subplots. Can be None to auto-determine.
    n_subplots : int
        Total number of subplots needed.

    Returns
    -------
    tuple[int, int]
        The number of rows and columns for the subplot grid.

    Notes
    -----
    Logic for determining subplot layout:
    - If both nrows and ncols are specified, use those values
    - If both are None, calculate a grid as close to square as possible
    - If only one is specified, calculate the other to fit all subplots

    """
    # If both dimensions are specified, return them as is
    if nrows is not None and ncols is not None:
        return nrows, ncols, n_subplots

    # If both dimensions are None, create a grid as close to square as possible
    if nrows is None and ncols is None:
        ncols = int(np.ceil(np.sqrt(n_subplots)))
        nrows = int(np.ceil(n_subplots / ncols))
    # If only one dimension is specified, calculate the other
    elif nrows is not None and ncols is None:
        ncols = int(np.ceil(n_subplots / nrows))
    elif nrows is None and ncols is not None:
        nrows = int(np.ceil(n_subplots / ncols))
    else:
        msg = (
            "We should never reach this point - both nrows and ncols are None, "
            "but were missed in earlier check."
        )
        raise ValueError(msg)

    n_subplots = nrows * ncols

    return nrows, ncols, n_subplots

create_circumplex_subplots

create_circumplex_subplots(data_list, plot_type='density', incl_scatter=True, subtitles=None, title='Circumplex Subplots', nrows=None, ncols=None, figsize=(10, 10), **kwargs)

Create a figure with subplots containing circumplex plots.

.. deprecated:: 0.8.0 Use :func:create_iso_subplots instead.

RETURNS DESCRIPTION
matplotlib.figure.Figure: A figure containing the subplots.
Example
>>> import pandas as pd
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> np.random.seed(42)
>>> data1 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
>>> data2 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
>>> fig = create_circumplex_subplots(
...     [data1, data2], plot_type="scatter", nrows=1, ncols=2
... )
>>> plt.show() # xdoctest: +SKIP
>>> isinstance(fig, plt.Figure)
True
>>> plt.close('all')
Source code in soundscapy/plotting/plot_functions.py
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
def create_circumplex_subplots(
    data_list: list[pd.DataFrame],
    plot_type: Literal["density", "scatter", "simple_density"] = "density",
    incl_scatter: bool = True,  # noqa: FBT001, FBT002
    subtitles: list[str] | None = None,
    title: str = "Circumplex Subplots",
    nrows: int | None = None,
    ncols: int | None = None,
    figsize: tuple[int, int] = (10, 10),
    **kwargs: Any,
) -> Figure:
    """
    Create a figure with subplots containing circumplex plots.

    .. deprecated:: 0.8.0
       Use :func:`create_iso_subplots` instead.

    Parameters
    ----------
        data_list : List of DataFrames to plot.
        plot_type : Type of plot to create.
        incl_scatter : Whether to include scatter points on density plots.
        subtitles : List of subtitles for each subplot.
        title : Title for the entire figure.
        nrows : Number of rows in the subplot grid.
        ncols : Number of columns in the subplot grid.
        figsize : Figure size (width, height) in inches.
        **kwargs: Additional keyword arguments to pass to scatter_plot or density_plot.

    Returns
    -------
        matplotlib.figure.Figure: A figure containing the subplots.

    Example
    -------
        >>> import pandas as pd
        >>> import numpy as np
        >>> import matplotlib.pyplot as plt
        >>> np.random.seed(42)
        >>> data1 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
        ...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
        >>> data2 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
        ...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
        >>> fig = create_circumplex_subplots(
        ...     [data1, data2], plot_type="scatter", nrows=1, ncols=2
        ... )
        >>> plt.show() # xdoctest: +SKIP
        >>> isinstance(fig, plt.Figure)
        True
        >>> plt.close('all')

    """
    warnings.warn(
        "The `create_circumplex_subplots` function is deprecated and will be removed "
        "in a future version. Use `create_iso_subplots` instead.",
        DeprecationWarning,
        stacklevel=2,
    )

    # Map plot_type to plot_layers
    if plot_type == "scatter":
        plot_layers = ["scatter"]
    elif plot_type == "density":
        plot_layers = ["density"]
        if incl_scatter:
            plot_layers.insert(0, "scatter")
    elif plot_type == "simple_density":
        plot_layers = ["simple_density"]
        if incl_scatter:
            plot_layers.insert(0, "scatter")
    else:
        warnings.warn(
            "Can't recognize plot type. Using default 'density' plot type with scatter.",
            UserWarning,
            stacklevel=2,
        )
        plot_layers = ["scatter", "density"]

    # Map subtitles to subplot_titles
    subplot_titles = subtitles

    # Call create_iso_subplots with translated parameters
    fig, _ = create_iso_subplots(
        data=data_list,
        subplot_titles=subplot_titles,
        title=title,
        nrows=nrows,
        ncols=ncols,
        figsize=figsize,
        plot_layers=plot_layers,  # type: ignore[arg-type]
        **kwargs,
    )

    return fig

create_iso_subplots

create_iso_subplots(data, x='ISOPleasant', y='ISOEventful', subplot_by=None, title='Soundscapy Plot', plot_layers=('scatter', 'density'), *, subplot_size=(4, 4), subplot_titles='by_group', subplot_title_prefix='Plot', nrows=None, ncols=None, **kwargs)

Create a set of subplots displaying data visualizations for soundscape analysis.

This function generates a collection of subplots, where each subplot corresponds to a subset of the input data. The subplots can display scatter plots, density plots, or simplified density plots, and can be organized by specific grouping criteria. Users can specify titles, overall size, row and column layout, and layering of plot types.

PARAMETER DESCRIPTION
data

Input data to be visualized. Can be a single data frame or a list of data frames for use in multiple subplots.

TYPE: pandas.DataFrame or list of pandas.DataFrame

x

The name of the column in the data to be used for the x-axis. Default is "ISOPleasant".

TYPE: str DEFAULT: 'ISOPleasant'

y

The name of the column in the data to be used for the y-axis. Default is "ISOEventful".

TYPE: str DEFAULT: 'ISOEventful'

subplot_by

The column name by which to group data into subplots. If None, data is not grouped and plotted in a single set of axes. Default is None.

TYPE: str or None DEFAULT: None

title

The overarching title of the figure. If None, no overall title is added. Default is "Soundscapy Plot".

TYPE: str or None DEFAULT: 'Soundscapy Plot'

plot_layers

such Literals, optional Type(s) of plot layers to include in each subplot. Can be a single type or a sequence of types. Default is ("scatter", "density").

TYPE: Literal["scatter", "density", "simple_density"] or Sequence of DEFAULT: ('scatter', 'density')

subplot_size

Size of each subplot in inches as (width, height). Default is (4, 4).

TYPE: tuple of int DEFAULT: (4, 4)

subplot_titles

optional Determines how subplot titles are assigned. Options are "by_group" (titles derived from group names), "numbered" (titles as indices), or a list of custom titles. If None, no titles are added. Default is "by_group".

TYPE: Literal["by_group", "numbered"], list of str, or None, DEFAULT: 'by_group'

subplot_title_prefix

Prefix for subplot titles if "numbered" is selected as subplot_titles. Default is "Plot".

TYPE: str DEFAULT: 'Plot'

nrows

Number of rows for the subplot grid. If None, automatically calculated based on the number of subplots. Default is None.

TYPE: int or None DEFAULT: None

ncols

Number of columns for the subplot grid. If None, automatically calculated based on the number of subplots. Default is None.

TYPE: int or None DEFAULT: None

**kwargs

Additional keyword arguments to pass to matplotlib's plt.subplots or for customizing the figure and subplots.

DEFAULT: {}

RETURNS DESCRIPTION
tuple

A tuple containing: - fig : matplotlib.figure.Figure The created matplotlib figure object containing the subplots. - np.ndarray An array of matplotlib.axes.Axes objects corresponding to the subplots.

Examples:

Basic subplots with default settings:

>>> import soundscapy as sspy
>>> import matplotlib.pyplot as plt
>>> import pandas as pd
>>> data = sspy.isd.load()
>>> data = sspy.add_iso_coords(data)
>>> four_locs = sspy.isd.select_location_ids(data,
...     ['CamdenTown', 'PancrasLock', 'RegentsParkJapan', 'RegentsParkFields']
... )
>>> fig, axes = sspy.create_iso_subplots(four_locs, subplot_by="LocationID")
>>> plt.show() # xdoctest: +SKIP

Create subplots by specifying a list of data

>>> data1 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
>>> data2 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
>>> fig, axes = create_iso_subplots(
...     [data1, data2], plot_layers="scatter", nrows=1, ncols=2
... )
>>> plt.show() # xdoctest: +SKIP
>>> assert len(axes) == 2
>>> plt.close('all')
Source code in soundscapy/plotting/plot_functions.py
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
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
324
325
326
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
def create_iso_subplots(
    data: pd.DataFrame | list[pd.DataFrame],
    x: str = "ISOPleasant",
    y: str = "ISOEventful",
    subplot_by: str | None = None,
    title: str | None = "Soundscapy Plot",
    plot_layers: Literal["scatter", "density", "simple_density"]
    | Sequence[Literal["scatter", "simple_density", "density"]] = (
        "scatter",
        "density",
    ),
    *,
    subplot_size: tuple[int, int] = (4, 4),
    subplot_titles: Literal["by_group", "numbered"] | list[str] | None = "by_group",
    subplot_title_prefix: str = "Plot",  # Only used if subplot_titles = 'numbered'
    nrows: int | None = None,
    ncols: int | None = None,
    **kwargs,
) -> tuple[Figure, np.ndarray]:
    """
    Create a set of subplots displaying data visualizations for soundscape analysis.

    This function generates a collection of subplots, where each subplot corresponds
    to a subset of the input data. The subplots can display scatter plots, density
    plots, or simplified density plots, and can be organized by specific grouping
    criteria. Users can specify titles, overall size, row and column layout, and
    layering of plot types.

    Parameters
    ----------
    data : pandas.DataFrame or list of pandas.DataFrame
        Input data to be visualized. Can be a single data frame or a list of data
        frames for use in multiple subplots.
    x : str, optional
        The name of the column in the data to be used for the x-axis. Default is
        "ISOPleasant".
    y : str, optional
        The name of the column in the data to be used for the y-axis. Default is
        "ISOEventful".
    subplot_by : str or None, optional
        The column name by which to group data into subplots. If None, data is not
        grouped and plotted in a single set of axes. Default is None.
    title : str or None, optional
        The overarching title of the figure. If None, no overall title is added.
        Default is "Soundscapy Plot".
    plot_layers : Literal["scatter", "density", "simple_density"] or Sequence of
        such Literals, optional
        Type(s) of plot layers to include in each subplot. Can be a single type
        or a sequence of types. Default is ("scatter", "density").
    subplot_size : tuple of int, optional
        Size of each subplot in inches as (width, height). Default is (4, 4).
    subplot_titles : Literal["by_group", "numbered"], list of str, or None,
        optional
        Determines how subplot titles are assigned. Options are "by_group" (titles
        derived from group names), "numbered" (titles as indices), or a list of
        custom titles. If None, no titles are added. Default is "by_group".
    subplot_title_prefix : str, optional
        Prefix for subplot titles if "numbered" is selected as `subplot_titles`.
        Default is "Plot".
    nrows : int or None, optional
        Number of rows for the subplot grid. If None, automatically calculated
        based on the number of subplots. Default is None.
    ncols : int or None, optional
        Number of columns for the subplot grid. If None, automatically calculated
        based on the number of subplots. Default is None.
    **kwargs
        Additional keyword arguments to pass to matplotlib's `plt.subplots` or for
        customizing the figure and subplots.

    Returns
    -------
    tuple
        A tuple containing:
        - fig : matplotlib.figure.Figure
            The created matplotlib figure object containing the subplots.
        - np.ndarray
            An array of matplotlib.axes.Axes objects corresponding to the subplots.

    Examples
    --------
    Basic subplots with default settings:
    >>> import soundscapy as sspy
    >>> import matplotlib.pyplot as plt
    >>> import pandas as pd
    >>> data = sspy.isd.load()
    >>> data = sspy.add_iso_coords(data)
    >>> four_locs = sspy.isd.select_location_ids(data,
    ...     ['CamdenTown', 'PancrasLock', 'RegentsParkJapan', 'RegentsParkFields']
    ... )
    >>> fig, axes = sspy.create_iso_subplots(four_locs, subplot_by="LocationID")
    >>> plt.show() # xdoctest: +SKIP

    Create subplots by specifying a list of data
    >>> data1 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
    ...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
    >>> data2 = pd.DataFrame({'ISOPleasant': np.random.uniform(-1, 1, 50),
    ...                       'ISOEventful': np.random.uniform(-1, 1, 50)})
    >>> fig, axes = create_iso_subplots(
    ...     [data1, data2], plot_layers="scatter", nrows=1, ncols=2
    ... )
    >>> plt.show() # xdoctest: +SKIP
    >>> assert len(axes) == 2
    >>> plt.close('all')

    """
    # Process input data and prepare for subplot creation
    data_list, subplot_titles_list, n_subplots = _prepare_subplot_data(
        data=data, x=y, y=y, subplot_by=subplot_by, subplot_titles=subplot_titles
    )

    # Calculate subplot layout
    nrows, ncols, n_subplots = allocate_subplot_axes(nrows, ncols, n_subplots)

    # Set up figure and subplots
    if title:
        vert_adjust = 1.2
    else:
        vert_adjust = 1.0
    figsize = kwargs.pop(
        "figsize", (ncols * subplot_size[0], nrows * (vert_adjust * subplot_size[1]))
    )

    subplots_params = SubplotsParams()
    subplots_params.update(
        nrows=nrows,
        ncols=ncols,
        figsize=figsize,
        subplot_by=subplot_by,
        extra="ignore",
        **kwargs,
    )

    fig, axes = plt.subplots(**subplots_params.as_plt_subplots_args())

    # Create each subplot
    _create_subplots(
        data_list,
        axes,
        n_subplots,
        subplot_titles_list,
        x,
        y,
        plot_layers,
        subplot_title_prefix,
        **kwargs,
    )

    # Add overall title and adjust layout
    if title:
        fig.suptitle(title, fontsize=DEFAULT_STYLE_PARAMS["title_fontsize"])

    fig.tight_layout()

    return fig, axes

density

density(data, title='Soundscape Density Plot', ax=None, *, x='ISOPleasant', y='ISOEventful', hue=None, incl_scatter=True, density_type='full', palette='colorblind', scatter_kws=None, legend='auto', prim_labels=None, **kwargs)

Plot a density plot of ISOCoordinates.

Creates a kernel density estimate visualization of data distribution on a circumplex grid with the custom Soundscapy styling for soundscape circumplex visualisations. Can optionally include a scatter plot of the underlying data points.

PARAMETER DESCRIPTION
data

Input data structure containing coordinate data, typically with ISOPleasant and ISOEventful columns.

TYPE: DataFrame

title

Title to add to circumplex plot, by default "Soundscape Density Plot"

TYPE: str | None DEFAULT: 'Soundscape Density Plot'

ax

Pre-existing axes object to use for the plot, by default None If None call matplotlib.pyplot.subplots with figsize internally.

TYPE: Axes DEFAULT: None

x

Column name for x variable, by default "ISOPleasant"

TYPE: str DEFAULT: 'ISOPleasant'

y

Column name for y variable, by default "ISOEventful"

TYPE: str DEFAULT: 'ISOEventful'

hue

Grouping variable that will produce density contours with different colors. Can be either categorical or numeric, although color mapping will behave differently in latter case, by default None

TYPE: str | ndarray | Series | None DEFAULT: None

incl_scatter

Whether to include a scatter plot of the data points, by default True

TYPE: bool DEFAULT: True

density_type

Type of density plot to draw. "full" uses default parameters, "simple" uses a lower number of levels (2), higher threshold (0.5), and lower alpha (0.5) for a cleaner visualization, by default "full"

TYPE: (full, simple) DEFAULT: "full"

palette

Method for choosing the colors to use when mapping the hue semantic. String values are passed to seaborn.color_palette(). List or dict values imply categorical mapping, while a colormap object implies numeric mapping, by default "colorblind"

TYPE: SeabornPaletteType | None DEFAULT: 'colorblind'

scatter_kws

Keyword arguments to pass to seaborn.scatterplot if incl_scatter is True, by default {"s": 25, "linewidth": 0}

TYPE: dict | None DEFAULT: None

incl_outline

Whether to include an outline for the density contours, by default False

TYPE: bool

legend

How to draw the legend. If "brief", numeric hue variables will be represented with a sample of evenly spaced values. If "full", every group will get an entry in the legend. If "auto", choose between brief or full representation based on number of levels. If False, no legend data is added and no legend is drawn, by default "auto"

TYPE: (auto, brief, full, False) DEFAULT: "auto"

prim_labels

Deprecated. Use xlabel and ylabel parameters instead.

TYPE: bool | None DEFAULT: None

**kwargs

Additional styling parameters:

  • alpha : float, optional Proportional opacity of the density fill, by default 0.8
  • fill : bool, optional If True, fill in the area between bivariate contours, by default True
  • levels : int | Iterable[float], optional Number of contour levels or values to draw contours at, by default 10
  • thresh : float, optional Lowest iso-proportional level at which to draw a contour line, by default 0.05
  • bw_adjust : float, optional Factor that multiplicatively scales the bandwidth, by default 1.2
  • xlabel, ylabel : str | Literal[False], optional Custom axis labels. By default, "\(P_{ISO}\)" and "\(E_{ISO}\)" with math rendering. If None is passed, the column names (x and y) will be used as labels. If a string is provided, it will be used as the label. If False is passed, axis labels will be hidden.
  • xlim, ylim : tuple[float, float], optional Limits for x and y axes, by default (-1, 1) for both
  • legend_loc : MplLegendLocType, optional Location of legend, by default "best"
  • diagonal_lines : bool, optional Whether to include diagonal dimension labels (e.g. calm, etc.), by default False
  • figsize : tuple[int, int], optional Size of the figure to return if ax is None, by default (5, 5)
  • prim_ax_fontdict : dict, optional Font dictionary for axis labels with these defaults:

    { "family": "sans-serif", "fontstyle": "normal", "fontsize": "large", "fontweight": "medium", "parse_math": True, "c": "black", "alpha": 1, } - fontsize, fontweight, fontstyle, family, c, alpha, parse_math: Direct parameters for font styling in axis labels

Also accepts additional keyword arguments for matplotlib's contour and contourf functions.

TYPE: dict DEFAULT: {}

RETURNS DESCRIPTION
Axes object containing the plot.
Notes

This function will raise a warning if the dataset has fewer than RECOMMENDED_MIN_SAMPLES (30) data points, as density plots are not reliable with small sample sizes.

Examples:

Basic density plot with default settings:

>>> import soundscapy as sspy
>>> import matplotlib.pyplot as plt
>>> data = sspy.isd.load()
>>> data = sspy.add_iso_coords(data)
>>> ax = sspy.density(data)
>>> plt.show() # xdoctest: +SKIP

Simple density plot with fewer contour levels:

>>> ax = sspy.density(data, density_type="simple")
>>> plt.show() # xdoctest: +SKIP

Density plot with custom styling:

>>> sub_data = sspy.isd.select_location_ids(
...    data, ['CamdenTown', 'PancrasLock', 'RegentsParkJapan', 'RegentsParkFields'])
>>> ax = sspy.density(
...     sub_data,
...     hue="SessionID",
...     incl_scatter=True,
...     legend_loc="upper right",
...     fill = False,
...     density_type = "simple",
... )
>>> plt.show() # xdoctest: +SKIP

Add density to existing plots:

>>> fig, axes = plt.subplots(1, 2, figsize=(12, 6))
>>> axes[0] = sspy.density(
...     sspy.isd.select_location_ids(data, ['CamdenTown', 'PancrasLock']),
...     ax=axes[0], title="CamdenTown and PancrasLock", hue="LocationID",
...     density_type="simple"
... )
>>> axes[1] = sspy.density(
...     sspy.isd.select_location_ids(data, ['RegentsParkJapan']),
...     ax=axes[1], title="RegentsParkJapan"
... )
>>> plt.tight_layout()
>>> plt.show() # xdoctest: +SKIP
>>> plt.close('all')
Source code in soundscapy/plotting/plot_functions.py
 771
 772
 773
 774
 775
 776
 777
 778
 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
 819
 820
 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
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
def density(
    data: pd.DataFrame,
    title: str | None = "Soundscape Density Plot",
    ax: Axes | None = None,
    *,
    x: str | None = "ISOPleasant",
    y: str | None = "ISOEventful",
    hue: str | None = None,
    incl_scatter: bool = True,
    density_type: str = "full",
    palette: SeabornPaletteType | None = "colorblind",
    scatter_kws: dict | None = None,
    legend: Literal["auto", "brief", "full", False] = "auto",
    prim_labels: bool | None = None,  # Alias for primary_labels, deprecated
    **kwargs,
) -> Axes:
    """
    Plot a density plot of ISOCoordinates.

    Creates a kernel density estimate visualization of data distribution on a
    circumplex grid with the custom Soundscapy styling for soundscape circumplex
    visualisations. Can optionally include a scatter plot of the underlying data points.

    Parameters
    ----------
    data : pd.DataFrame
        Input data structure containing coordinate data, typically with ISOPleasant
        and ISOEventful columns.
    title : str | None, optional
        Title to add to circumplex plot, by default "Soundscape Density Plot"
    ax : matplotlib.axes.Axes, optional
        Pre-existing axes object to use for the plot, by default None
        If `None` call `matplotlib.pyplot.subplots` with `figsize` internally.
    x : str, optional
        Column name for x variable, by default "ISOPleasant"
    y : str, optional
        Column name for y variable, by default "ISOEventful"
    hue : str | np.ndarray | pd.Series | None, optional
        Grouping variable that will produce density contours with different colors.
        Can be either categorical or numeric, although color mapping will behave
        differently in latter case, by default None
    incl_scatter : bool, optional
        Whether to include a scatter plot of the data points, by default True
    density_type : {"full", "simple"}, optional
        Type of density plot to draw. "full" uses default parameters, "simple"
        uses a lower number of levels (2), higher threshold (0.5), and lower alpha (0.5)
        for a cleaner visualization, by default "full"
    palette : SeabornPaletteType | None, optional
        Method for choosing the colors to use when mapping the hue semantic.
        String values are passed to seaborn.color_palette().
        List or dict values imply categorical mapping, while a colormap object
        implies numeric mapping, by default "colorblind"
    scatter_kws : dict | None, optional
        Keyword arguments to pass to `seaborn.scatterplot` if incl_scatter is True,
        by default {"s": 25, "linewidth": 0}
    incl_outline : bool, optional
        Whether to include an outline for the density contours, by default False
    legend : {"auto", "brief", "full", False}, optional
        How to draw the legend. If "brief", numeric hue variables will be
        represented with a sample of evenly spaced values. If "full", every group will
        get an entry in the legend. If "auto", choose between brief or full
        representation based on number of levels.
        If False, no legend data is added and no legend is drawn, by default "auto"
    prim_labels : bool | None, optional
        Deprecated. Use xlabel and ylabel parameters instead.

    **kwargs : dict, optional
        Additional styling parameters:

        - alpha : float, optional
            Proportional opacity of the density fill, by default 0.8
        - fill : bool, optional
            If True, fill in the area between bivariate contours, by default True
        - levels : int | Iterable[float], optional
            Number of contour levels or values to draw contours at, by default 10
        - thresh : float, optional
            Lowest iso-proportional level at which to draw a contour line,
            by default 0.05
        - bw_adjust : float, optional
            Factor that multiplicatively scales the bandwidth, by default 1.2
        - xlabel, ylabel : str | Literal[False], optional
            Custom axis labels. By default, "$P_{ISO}$" and "$E_{ISO}$" with math
            rendering.
            If None is passed, the column names (x and y) will be used as labels.
            If a string is provided, it will be used as the label.
            If False is passed, axis labels will be hidden.
        - xlim, ylim : tuple[float, float], optional
            Limits for x and y axes, by default (-1, 1) for both
        - legend_loc : MplLegendLocType, optional
            Location of legend, by default "best"
        - diagonal_lines : bool, optional
            Whether to include diagonal dimension labels (e.g. calm, etc.),
            by default False
        - figsize : tuple[int, int], optional
            Size of the figure to return if `ax` is None, by default (5, 5)
        - prim_ax_fontdict : dict, optional
            Font dictionary for axis labels with these defaults:

            {
                "family": "sans-serif",
                "fontstyle": "normal",
                "fontsize": "large",
                "fontweight": "medium",
                "parse_math": True,
                "c": "black",
                "alpha": 1,
            }
        - fontsize, fontweight, fontstyle, family, c, alpha, parse_math:
            Direct parameters for font styling in axis labels

        Also accepts additional keyword arguments for matplotlib's contour and contourf
        functions.

    Returns
    -------
        Axes object containing the plot.

    Notes
    -----
    This function will raise a warning if the dataset has fewer than
    RECOMMENDED_MIN_SAMPLES (30) data points, as density plots are not reliable
    with small sample sizes.

    Examples
    --------
    Basic density plot with default settings:

    >>> import soundscapy as sspy
    >>> import matplotlib.pyplot as plt
    >>> data = sspy.isd.load()
    >>> data = sspy.add_iso_coords(data)
    >>> ax = sspy.density(data)
    >>> plt.show() # xdoctest: +SKIP

    Simple density plot with fewer contour levels:

    >>> ax = sspy.density(data, density_type="simple")
    >>> plt.show() # xdoctest: +SKIP

    Density plot with custom styling:

    >>> sub_data = sspy.isd.select_location_ids(
    ...    data, ['CamdenTown', 'PancrasLock', 'RegentsParkJapan', 'RegentsParkFields'])
    >>> ax = sspy.density(
    ...     sub_data,
    ...     hue="SessionID",
    ...     incl_scatter=True,
    ...     legend_loc="upper right",
    ...     fill = False,
    ...     density_type = "simple",
    ... )
    >>> plt.show() # xdoctest: +SKIP

    Add density to existing plots:

    >>> fig, axes = plt.subplots(1, 2, figsize=(12, 6))
    >>> axes[0] = sspy.density(
    ...     sspy.isd.select_location_ids(data, ['CamdenTown', 'PancrasLock']),
    ...     ax=axes[0], title="CamdenTown and PancrasLock", hue="LocationID",
    ...     density_type="simple"
    ... )
    >>> axes[1] = sspy.density(
    ...     sspy.isd.select_location_ids(data, ['RegentsParkJapan']),
    ...     ax=axes[1], title="RegentsParkJapan"
    ... )
    >>> plt.tight_layout()
    >>> plt.show() # xdoctest: +SKIP
    >>> plt.close('all')

    """
    style_args, subplots_args, kwargs = _setup_style_and_subplots_args_from_kwargs(
        x=x, y=y, prim_labels=prim_labels, kwargs=kwargs
    )

    # Set up density parameters
    density_args = _setup_density_params(
        data=data,
        x=x,
        y=y,
        hue=hue,
        density_type=density_type,
        palette=palette,
        legend=legend,
        **kwargs,
    )

    # Check if dataset is large enough for density plots
    _valid_density(data)

    if ax is None:
        _, ax = plt.subplots(1, 1, figsize=subplots_args.get("figsize"))

    # Removes the palette if no hue is specified
    if density_args.get("hue") is None:
        density_args.update(palette=None)

    # Set up scatter parameters if needed
    scatter_args = ScatterParams()
    scatter_args.update(
        data=data,
        x=x,
        y=y,
        palette=palette,
        hue=density_args.get("hue"),
        color=density_args.get("color"),
        **(scatter_kws or {}),
    )

    scatter_args.crosscheck_palette_hue()
    density_args.crosscheck_palette_hue()

    if incl_scatter:
        d = sns.scatterplot(ax=ax, **scatter_args.as_seaborn_kwargs())

    if density_type == "simple":
        d = sns.kdeplot(ax=ax, **density_args.as_seaborn_kwargs())
        d = sns.kdeplot(ax=ax, **density_args.to_outline().as_seaborn_kwargs())

    elif density_type == "full":
        d = sns.kdeplot(ax=ax, **density_args.as_seaborn_kwargs())
    else:
        raise ValueError

    _set_style()
    _circumplex_grid(
        ax=ax,
        xlim=style_args.get("xlim"),
        ylim=style_args.get("ylim"),
        xlabel=style_args.get("xlabel"),
        ylabel=style_args.get("ylabel"),
        diagonal_lines=style_args.get("diagonal_lines"),
        prim_ax_fontdict=style_args.get("prim_ax_fontdict"),
    )
    if title is not None:
        _set_circum_title(
            ax=ax,
            title=title,
            xlabel=style_args.get("xlabel"),
            ylabel=style_args.get("ylabel"),
        )
    if legend is not None and hue is not None:
        _move_legend(ax=ax, new_loc=style_args.get("legend_loc"))

    return d

density_plot

density_plot(*args, **kwargs)

Wrapper for the density function to maintain backwards compatibility.

PARAMETER DESCRIPTION
*args

Positional arguments to pass to the density function.

TYPE: tuple DEFAULT: ()

**kwargs

Keyword arguments to pass to the density function.

TYPE: dict DEFAULT: {}

RETURNS DESCRIPTION
Axes

The Axes object containing the plot.

Source code in soundscapy/plotting/plot_functions.py
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
def density_plot(*args, **kwargs) -> Axes | np.ndarray | ISOPlot:  # noqa: ANN002
    """
    Wrapper for the density function to maintain backwards compatibility.

    Parameters
    ----------
    *args : tuple
        Positional arguments to pass to the density function.
    **kwargs : dict
        Keyword arguments to pass to the density function.

    Returns
    -------
    Axes
        The Axes object containing the plot.

    """  # noqa: D401
    warnings.warn(
        "The `density_plot` function is deprecated and will be removed in a "
        "future version. Use `density` instead."
        "\nAs of v0.8, `density_plot` is an alias for `density` and does not maintain "
        "full backwards compatibility with v0.7. It may work, or some arguments may "
        "fail.",
        DeprecationWarning,
        stacklevel=2,
    )
    if kwargs.pop("backend", None) is not None:
        warnings.warn(
            "`Backend` is no longer supported in the density_plot function (v0.8+).",
            DeprecationWarning,
            stacklevel=2,
        )
    filtered_args = [a for a in args if not isinstance(a, Backend)]

    kwargs = {
        k: v
        for k, v in kwargs.items()
        if k
        not in (
            "backend",
            "show_labels",
            "apply_styling",
            "simple_density",
            "simple_density_thresh",
            "simple_density_levels",
            "simple_density_alpha",
        )
    }

    # Convert simple_density parameters to the new API if they exist
    if "density_type" not in kwargs and kwargs.pop("simple_density", False):
        kwargs["density_type"] = "simple"

    return density(*filtered_args, **kwargs)

iso_annotation

iso_annotation(ax, data, location, *, x_adj=0, y_adj=0, x_key=DEFAULT_XCOL, y_key=DEFAULT_YCOL, ha='center', va='center', fontsize='small', arrowprops=None, **text_kwargs)

Add text annotations to circumplex plot based on coordinate values.

Directly uses plt.annotate

PARAMETER DESCRIPTION
ax

existing plt axes to add to

TYPE: Axes

data

dataframe of coordinate points

TYPE: Dataframe

location

name of the coordinate to plot

TYPE: str

x_adj

value to adjust x location by, by default 0

TYPE: int DEFAULT: 0

y_adj

value to adjust y location by, by default 0

TYPE: int DEFAULT: 0

x_key

name of x column, by default "ISOPleasant"

TYPE: str DEFAULT: DEFAULT_XCOL

y_key

name of y column, by default "ISOEventful"

TYPE: str DEFAULT: DEFAULT_YCOL

ha

horizontal alignment, by default "center"

TYPE: str DEFAULT: 'center'

va

vertical alignment, by default "center"

TYPE: str DEFAULT: 'center'

fontsize

by default "small"

TYPE: str DEFAULT: 'small'

arrowprops

dict of properties to send to plt.annotate, by default dict(arrowstyle="-", ec="black")

TYPE: dict DEFAULT: None

Source code in soundscapy/plotting/plot_functions.py
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
def iso_annotation(
    ax: Axes,
    data: pd.DataFrame,
    location: str,
    *,
    x_adj: int = 0,
    y_adj: int = 0,
    x_key: str = DEFAULT_XCOL,
    y_key: str = DEFAULT_YCOL,
    ha: str = "center",
    va: str = "center",
    fontsize: str = "small",
    arrowprops: dict | None = None,
    **text_kwargs,
) -> None:
    """
    Add text annotations to circumplex plot based on coordinate values.

    Directly uses plt.annotate

    Parameters
    ----------
    ax : matplotlib.pyplot.Axes
        existing plt axes to add to
    data : pd.Dataframe
        dataframe of coordinate points
    location : str
        name of the coordinate to plot
    x_adj : int, optional
        value to adjust x location by, by default 0
    y_adj : int, optional
        value to adjust y location by, by default 0
    x_key : str, optional
        name of x column, by default "ISOPleasant"
    y_key : str, optional
        name of y column, by default "ISOEventful"
    ha : str, optional
        horizontal alignment, by default "center"
    va : str, optional
        vertical alignment, by default "center"
    fontsize : str, optional
        by default "small"
    arrowprops : dict, optional
        dict of properties to send to plt.annotate,
        by default dict(arrowstyle="-", ec="black")

    """
    if arrowprops is None:
        arrowprops = {"arrowstyle": "-", "ec": "black"}

    # noinspection PyTypeChecker
    ax.annotate(
        text=data["LocationID"][location],
        xy=(
            data[x_key][location],
            data[y_key][location],
        ),
        xytext=(
            data[x_key][location] + x_adj,
            data[y_key][location] + y_adj,
        ),
        ha=ha,
        va=va,
        arrowprops=arrowprops,
        annotation_clip=True,
        fontsize=fontsize,
        **text_kwargs,
    )

iso_plot

iso_plot(data, x='ISOPleasant', y='ISOEventful', title='Soundscapy Plot', plot_layers=('scatter', 'density'), **kwargs)

Plot a soundscape visualization based on the specified metrics using different combinations of layers such as scatter, density, or simple density plots.

The function generates a 2D plot (via Matplotlib Axes object) based on the x and y metrics provided. Users can choose between individual layers or specify a combination of the supported plot layers. It supports automatic handling for specific layer combinations, such as "scatter + density". The core plotting functionality is delegated to other helper functions (scatter and density).

PARAMETER DESCRIPTION
data

The dataset containing the metrics to be plotted. Must include the columns specified for x and y.

TYPE: DataFrame

x

The column name within data to be used for the x-axis. Defaults to "ISOPleasant".

TYPE: str DEFAULT: 'ISOPleasant'

y

The column name within data to be used for the y-axis. Defaults to "ISOEventful".

TYPE: str DEFAULT: 'ISOEventful'

title

The title of the plot. If not specified, defaults to "Soundscapy Plot".

TYPE: str or None DEFAULT: 'Soundscapy Plot'

plot_layers

or Sequence[{"scatter", "density", "simple_density"}], optional

Specifies the type or combination of plot layers to generate. Valid options include: - "scatter": A scatter plot - "density": A density plot (without scatter points, unless combined) - "simple_density": A simplified density plot (without scatter points, unless combined).

Can be passed as a string (single layer) or as a sequence of strings (combination of layers). Defaults to ("scatter", "density").

TYPE: (scatter, density, simple_density) DEFAULT: "scatter"

**kwargs

Additional keyword arguments to be passed to the underlying plotting functions (scatter or density). These allow for customization such as marker styles, colors, etc.

TYPE: any DEFAULT: {}

RETURNS DESCRIPTION
Axes

A Matplotlib Axes object corresponding to the generated plot.

TYPE: Axes

Notes

This function supports only specific combinations of layers. If an unsupported combination is specified, an exception will be raised. Layer compatibility rules: - Single layers: "scatter", "density", or "simple_density". - Dual layers: - "scatter" + "density" - "scatter" + "simple_density"

RAISES DESCRIPTION
TypeError

If the plot_layers argument is not a string or a sequence of strings.

ValueError

If the plot_layers argument specifies an unsupported plot type or combination of plot layers.

Examples:

Basic density and scatter plot with default settings:

>>> import soundscapy as sspy
>>> import matplotlib.pyplot as plt
>>> data = sspy.isd.load()
>>> data = sspy.add_iso_coords(data)
>>> ax = sspy.iso_plot(data)
>>> plt.show()  # xdoctest: +SKIP

Basic scatter plot:

>>> ax = sspy.iso_plot(data, plot_layers="scatter")
>>> plt.show()  # xdoctest: +SKIP

Simple density plot with fewer contour levels:

>>> ax = sspy.iso_plot(data, plot_layers="simple_density")
>>> plt.show() # xdoctest: +SKIP

Simple density with scatter points

>>> ax = sspy.iso_plot(data, plot_layers=["scatter", "simple_density"])
>>> plt.show() # xdoctest: +SKIP

Density plot with custom styling:

>>> sub_data = sspy.isd.select_location_ids(
...    data, ['CamdenTown', 'PancrasLock', 'RegentsParkJapan', 'RegentsParkFields'])
>>> ax = sspy.iso_plot(
...     sub_data,
...     hue="SessionID",
...     plot_layers = ["scatter", "simple_density"],
...     legend_loc="upper right",
...     fill = False,
... )
>>> plt.show() # xdoctest: +SKIP

Add density to existing plots:

>>> fig, axes = plt.subplots(1, 2, figsize=(12, 6))
>>> axes[0] = sspy.iso_plot(
...     sspy.isd.select_location_ids(data, ['CamdenTown', 'PancrasLock']),
...     ax=axes.flatten()[0], title="CamdenTown and PancrasLock", hue="LocationID",
... )
>>> axes[1] = sspy.iso_plot(
...     sspy.isd.select_location_ids(data, ['RegentsParkJapan']),
...     ax=axes.flatten()[1], title="RegentsParkJapan"
... )
>>> plt.tight_layout()
>>> plt.show() # xdoctest: +SKIP
>>> plt.close('all')
Source code in soundscapy/plotting/plot_functions.py
 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
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
def iso_plot(
    data: pd.DataFrame,
    x: str = "ISOPleasant",
    y: str = "ISOEventful",
    title: str | None = "Soundscapy Plot",
    plot_layers: Literal["scatter", "density", "simple_density"]
    | Sequence[Literal["scatter", "density", "simple_density"]] = (
        "scatter",
        "density",
    ),
    **kwargs,
) -> Axes:
    """
    Plot a soundscape visualization based on the specified metrics using different
    combinations of layers such as scatter, density, or simple density plots.

    The function generates a 2D plot (via Matplotlib Axes object) based on the `x` and
    `y` metrics provided. Users can choose between individual layers or specify a
    combination of the supported plot layers. It supports automatic handling for
    specific layer combinations, such as "scatter + density". The core plotting
    functionality is delegated to other helper functions (`scatter` and `density`).

    Parameters
    ----------
    data : pandas.DataFrame
        The dataset containing the metrics to be plotted. Must include the columns
        specified for `x` and `y`.
    x : str, optional
        The column name within `data` to be used for the x-axis. Defaults to
        "ISOPleasant".
    y : str, optional
        The column name within `data` to be used for the y-axis. Defaults to
        "ISOEventful".
    title : str or None, optional
        The title of the plot. If not specified, defaults to "Soundscapy Plot".
    plot_layers : {"scatter", "density", "simple_density"}
        or Sequence[{"scatter", "density", "simple_density"}], optional

        Specifies the type or combination of plot layers to generate. Valid options
        include:
         - "scatter": A scatter plot
         - "density": A density plot (without scatter points, unless combined)
         - "simple_density": A simplified density plot (without scatter points,
           unless combined).

        Can be passed as a string (single layer) or as a sequence of strings
        (combination of layers). Defaults to ("scatter", "density").
    **kwargs : any
        Additional keyword arguments to be passed to the underlying plotting
        functions (`scatter` or `density`). These allow for customization such as
        marker styles, colors, etc.

    Returns
    -------
    Axes : matplotlib.axes._axes.Axes
        A Matplotlib Axes object corresponding to the generated plot.

    Notes
    -----
    This function supports only specific combinations of layers. If an unsupported
    combination is specified, an exception will be raised. Layer compatibility
    rules:
      - Single layers: "scatter", "density", or "simple_density".
      - Dual layers:
        - "scatter" + "density"
        - "scatter" + "simple_density"

    Raises
    ------
    TypeError
        If the `plot_layers` argument is not a string or a sequence of strings.
    ValueError
        If the `plot_layers` argument specifies an unsupported plot type or
        combination of plot layers.

    Examples
    --------
    Basic density and scatter plot with default settings:

    >>> import soundscapy as sspy
    >>> import matplotlib.pyplot as plt
    >>> data = sspy.isd.load()
    >>> data = sspy.add_iso_coords(data)
    >>> ax = sspy.iso_plot(data)
    >>> plt.show()  # xdoctest: +SKIP

    Basic scatter plot:

    >>> ax = sspy.iso_plot(data, plot_layers="scatter")
    >>> plt.show()  # xdoctest: +SKIP

    Simple density plot with fewer contour levels:

    >>> ax = sspy.iso_plot(data, plot_layers="simple_density")
    >>> plt.show() # xdoctest: +SKIP

    Simple density with scatter points

    >>> ax = sspy.iso_plot(data, plot_layers=["scatter", "simple_density"])
    >>> plt.show() # xdoctest: +SKIP

    Density plot with custom styling:

    >>> sub_data = sspy.isd.select_location_ids(
    ...    data, ['CamdenTown', 'PancrasLock', 'RegentsParkJapan', 'RegentsParkFields'])
    >>> ax = sspy.iso_plot(
    ...     sub_data,
    ...     hue="SessionID",
    ...     plot_layers = ["scatter", "simple_density"],
    ...     legend_loc="upper right",
    ...     fill = False,
    ... )
    >>> plt.show() # xdoctest: +SKIP

    Add density to existing plots:

    >>> fig, axes = plt.subplots(1, 2, figsize=(12, 6))
    >>> axes[0] = sspy.iso_plot(
    ...     sspy.isd.select_location_ids(data, ['CamdenTown', 'PancrasLock']),
    ...     ax=axes.flatten()[0], title="CamdenTown and PancrasLock", hue="LocationID",
    ... )
    >>> axes[1] = sspy.iso_plot(
    ...     sspy.isd.select_location_ids(data, ['RegentsParkJapan']),
    ...     ax=axes.flatten()[1], title="RegentsParkJapan"
    ... )
    >>> plt.tight_layout()
    >>> plt.show() # xdoctest: +SKIP
    >>> plt.close('all')

    """  # noqa: D205
    if isinstance(plot_layers, str):
        plot_layers = [plot_layers]

    if not isinstance(plot_layers, Sequence):
        raise TypeError(PLOT_LAYER_TYPE_ERROR.format(type=type(plot_layers)))

    # Handle single layer case
    if len(plot_layers) == 1:
        layer_type = plot_layers[0]
        if layer_type == "scatter":
            return scatter(data, x=x, y=y, title=title, **kwargs)
        if layer_type == "simple_density":
            return density(
                data,
                x=x,
                y=y,
                title=title,
                density_type="simple",
                incl_scatter=False,
                **kwargs,
            )
        if layer_type == "density":
            return density(data, x=x, y=y, title=title, incl_scatter=False, **kwargs)

        raise ValueError(PLOT_LAYER_VALUE_ERROR.format(layers=plot_layers))

    # Handle two layer case
    if len(plot_layers) == 2:
        layers_set = set(plot_layers)

        if "scatter" in layers_set and "density" in layers_set:
            return density(data, x=x, y=y, title=title, incl_scatter=True, **kwargs)

        if "scatter" in layers_set and "simple_density" in layers_set:
            return density(
                data,
                x=x,
                y=y,
                title=title,
                density_type="simple",
                incl_scatter=True,
                **kwargs,
            )

        # Default case for unrecognized but valid length combinations
        return density(data, x=x, y=y, title=title, incl_scatter=True, **kwargs)

    # More than 2 layers is not supported
    raise ValueError(PLOT_LAYER_VALUE_ERROR.format(layers=plot_layers))

jointplot

jointplot(data, *, x=DEFAULT_XCOL, y=DEFAULT_YCOL, title='Soundscape Joint Plot', hue=None, incl_scatter=True, density_type='full', palette='colorblind', color=DEFAULT_COLOR, figsize=DEFAULT_FIGSIZE, scatter_kws=None, incl_outline=False, alpha=DEFAULT_SEABORN_PARAMS['alpha'], fill=True, levels=10, thresh=0.05, bw_adjust=DEFAULT_BW_ADJUST, legend='auto', prim_labels=None, joint_kws=None, marginal_kws=None, marginal_kind='kde', **kwargs)

Create a jointplot with a central distribution and marginal plots.

Creates a visualization with a main plot (density or scatter) in the center and marginal distribution plots along the x and y axes. The main plot uses the custom Soundscapy styling for soundscape circumplex visualisations, and the marginals show the individual distributions of each variable.

PARAMETER DESCRIPTION
data

Input data structure containing coordinate data, typically with ISOPleasant and ISOEventful columns.

TYPE: DataFrame

x

Column name for x variable, by default "ISOPleasant"

TYPE: str DEFAULT: DEFAULT_XCOL

y

Column name for y variable, by default "ISOEventful"

TYPE: str DEFAULT: DEFAULT_YCOL

title

Title to add to the jointplot, by default "Soundscape Joint Plot"

TYPE: str | None DEFAULT: 'Soundscape Joint Plot'

hue

Grouping variable that will produce plots with different colors. Can be either categorical or numeric, although color mapping will behave differently in latter case, by default None

TYPE: str | ndarray | Series | None DEFAULT: None

incl_scatter

Whether to include a scatter plot of the data points in the joint plot, by default True

TYPE: bool DEFAULT: True

density_type

Type of density plot to draw. "full" uses default parameters, "simple" uses a lower number of levels (2), higher threshold (0.5), and lower alpha (0.5) for a cleaner visualization, by default "full"

TYPE: (full, simple) DEFAULT: "full"

palette

Method for choosing the colors to use when mapping the hue semantic. String values are passed to seaborn.color_palette(). List or dict values imply categorical mapping, while a colormap object implies numeric mapping, by default "colorblind"

TYPE: SeabornPaletteType | None DEFAULT: 'colorblind'

color

Color to use for the plot elements when not using hue mapping, by default "#0173B2" (first color from colorblind palette)

TYPE: ColorType | None DEFAULT: DEFAULT_COLOR

figsize

Size of the figure to create (determines height, width is proportional), by default (5, 5)

TYPE: tuple[int, int] DEFAULT: DEFAULT_FIGSIZE

scatter_kws

Additional keyword arguments to pass to scatter plot if incl_scatter is True, by default None

TYPE: dict[str, Any] | None DEFAULT: None

incl_outline

Whether to include an outline for the density contours, by default False

TYPE: bool DEFAULT: False

alpha

Opacity level for the density fill, by default 0.8

TYPE: float DEFAULT: DEFAULT_SEABORN_PARAMS['alpha']

fill

Whether to fill the density contours, by default True

TYPE: bool DEFAULT: True

levels

Number of contour levels or specific levels to draw. A vector argument must have increasing values in [0, 1], by default 10

TYPE: int | Iterable[float] DEFAULT: 10

thresh

Lowest iso-proportion level at which to draw contours, by default 0.05

TYPE: float DEFAULT: 0.05

bw_adjust

Factor that multiplicatively scales the bandwidth. Increasing will make the density estimate smoother, by default 1.2

TYPE: float DEFAULT: DEFAULT_BW_ADJUST

legend

How to draw the legend for hue mapping, by default "auto"

TYPE: (auto, brief, full, False) DEFAULT: "auto"

prim_labels

Deprecated. Use xlabel and ylabel parameters instead.

TYPE: bool | None DEFAULT: None

joint_kws

Additional keyword arguments to pass to the joint plot, by default None

TYPE: dict[str, Any] | None DEFAULT: None

marginal_kws

Additional keyword arguments to pass to the marginal plots, by default {"fill": True, "common_norm": False}

TYPE: dict[str, Any] | None DEFAULT: None

marginal_kind

Type of plot to draw in the marginal axes, either "kde" for kernel density estimation or "hist" for histogram, by default "kde"

TYPE: (kde, hist) DEFAULT: "kde"

**kwargs

Additional styling parameters:

  • xlabel, ylabel : str | Literal[False], optional Custom axis labels. By default "\(P_{ISO}\)" and "\(E_{ISO}\)" with math rendering.

    If None is passed, the column names (x and y) will be used as labels.

    If a string is provided, it will be used as the label.

    If False is passed, axis labels will be hidden. - xlim, ylim : tuple[float, float], optional Limits for x and y axes, by default (-1, 1) for both - legend_loc : MplLegendLocType, optional Location of legend, by default "best" - diagonal_lines : bool, optional Whether to include diagonal dimension labels (e.g. calm, etc.), by default False - prim_ax_fontdict : dict, optional Font dictionary for axis labels with these defaults:

    { "family": "sans-serif", "fontstyle": "normal", "fontsize": "large", "fontweight": "medium", "parse_math": True, "c": "black", "alpha": 1, }

TYPE: dict DEFAULT: {}

RETURNS DESCRIPTION
JointGrid

The seaborn JointGrid object containing the plot

Notes

This function will raise a warning if the dataset has fewer than RECOMMENDED_MIN_SAMPLES (30) data points, as density plots are not reliable with small sample sizes.

Examples:

Basic jointplot with default settings:

>>> import soundscapy as sspy
>>> import matplotlib.pyplot as plt
>>> data = sspy.isd.load()
>>> data = sspy.add_iso_coords(data)
>>> g = sspy.jointplot(data)
>>> plt.show() # xdoctest: +SKIP

Jointplot with histogram marginals:

>>> g = sspy.jointplot(data, marginal_kind="hist")
>>> plt.show() # xdoctest: +SKIP

Jointplot with custom styling and grouping:

>>> g = sspy.jointplot(
...     data,
...     hue="LocationID",
...     incl_scatter=True,
...     density_type="simple",
...     diagonal_lines=True,
...     figsize=(6, 6),
...     title="Grouped Soundscape Analysis"
... )
>>> plt.show() # xdoctest: +SKIP
>>> plt.close('all')
Source code in soundscapy/plotting/plot_functions.py
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
def jointplot(
    data: pd.DataFrame,
    *,
    x: str = DEFAULT_XCOL,
    y: str = DEFAULT_YCOL,
    title: str | None = "Soundscape Joint Plot",
    hue: str | None = None,
    incl_scatter: bool = True,
    density_type: str = "full",
    palette: SeabornPaletteType | None = "colorblind",
    color: ColorType | None = DEFAULT_COLOR,
    figsize: tuple[int, int] = DEFAULT_FIGSIZE,
    scatter_kws: dict[str, Any] | None = None,
    incl_outline: bool = False,
    alpha: float = DEFAULT_SEABORN_PARAMS["alpha"],
    fill: bool = True,
    levels: int | tuple[float, ...] = 10,
    thresh: float = 0.05,
    bw_adjust: float = DEFAULT_BW_ADJUST,
    legend: Literal["auto", "brief", "full", False] = "auto",
    prim_labels: bool | None = None,  # Alias for primary_labels, deprecated
    joint_kws: dict[str, Any] | None = None,
    marginal_kws: dict[str, Any] | None = None,
    marginal_kind: str = "kde",
    **kwargs,
) -> sns.JointGrid:
    """
    Create a jointplot with a central distribution and marginal plots.

    Creates a visualization with a main plot (density or scatter) in the center and
    marginal distribution plots along the x and y axes. The main plot uses the custom
    Soundscapy styling for soundscape circumplex visualisations, and the marginals show
    the individual distributions of each variable.

    Parameters
    ----------
    data : pd.DataFrame
        Input data structure containing coordinate data, typically with ISOPleasant
        and ISOEventful columns.
    x : str, optional
        Column name for x variable, by default "ISOPleasant"
    y : str, optional
        Column name for y variable, by default "ISOEventful"
    title : str | None, optional
        Title to add to the jointplot, by default "Soundscape Joint Plot"
    hue : str | np.ndarray | pd.Series | None, optional
        Grouping variable that will produce plots with different colors.
        Can be either categorical or numeric, although color mapping will behave
        differently in latter case, by default None
    incl_scatter : bool, optional
        Whether to include a scatter plot of the data points in the joint plot,
        by default True
    density_type : {"full", "simple"}, optional
        Type of density plot to draw. "full" uses default parameters, "simple"
        uses a lower number of levels (2), higher threshold (0.5), and lower alpha (0.5)
        for a cleaner visualization, by default "full"
    palette : SeabornPaletteType | None, optional
        Method for choosing the colors to use when mapping the hue semantic.
        String values are passed to seaborn.color_palette().
        List or dict values imply categorical mapping, while a colormap object
        implies numeric mapping, by default "colorblind"
    color : ColorType | None, optional
        Color to use for the plot elements when not using hue mapping,
        by default "#0173B2" (first color from colorblind palette)
    figsize : tuple[int, int], optional
        Size of the figure to create (determines height, width is proportional),
        by default (5, 5)
    scatter_kws : dict[str, Any] | None, optional
        Additional keyword arguments to pass to scatter plot if incl_scatter is True,
        by default None
    incl_outline : bool, optional
        Whether to include an outline for the density contours, by default False
    alpha : float, optional
        Opacity level for the density fill, by default 0.8
    fill : bool, optional
        Whether to fill the density contours, by default True
    levels : int | Iterable[float], optional
        Number of contour levels or specific levels to draw. A vector argument
        must have increasing values in [0, 1], by default 10
    thresh : float, optional
        Lowest iso-proportion level at which to draw contours, by default 0.05
    bw_adjust : float, optional
        Factor that multiplicatively scales the bandwidth. Increasing will make
        the density estimate smoother, by default 1.2
    legend : {"auto", "brief", "full", False}, optional
        How to draw the legend for hue mapping, by default "auto"
    prim_labels : bool | None, optional
        Deprecated. Use xlabel and ylabel parameters instead.
    joint_kws : dict[str, Any] | None, optional
        Additional keyword arguments to pass to the joint plot, by default None
    marginal_kws : dict[str, Any] | None, optional
        Additional keyword arguments to pass to the marginal plots,
        by default {"fill": True, "common_norm": False}
    marginal_kind : {"kde", "hist"}, optional
        Type of plot to draw in the marginal axes, either "kde" for kernel
        density estimation or "hist" for histogram, by default "kde"

    **kwargs : dict, optional
        Additional styling parameters:

        - xlabel, ylabel : str | Literal[False], optional
            Custom axis labels. By default "$P_{ISO}$" and "$E_{ISO}$" with
            math rendering.

            If None is passed, the column names (x and y) will be used as labels.

            If a string is provided, it will be used as the label.

            If False is passed, axis labels will be hidden.
        - xlim, ylim : tuple[float, float], optional
            Limits for x and y axes, by default (-1, 1) for both
        - legend_loc : MplLegendLocType, optional
            Location of legend, by default "best"
        - diagonal_lines : bool, optional
            Whether to include diagonal dimension labels (e.g. calm, etc.),
            by default False
        - prim_ax_fontdict : dict, optional
            Font dictionary for axis labels with these defaults:

            {
                "family": "sans-serif",
                "fontstyle": "normal",
                "fontsize": "large",
                "fontweight": "medium",
                "parse_math": True,
                "c": "black",
                "alpha": 1,
            }

    Returns
    -------
    sns.JointGrid
        The seaborn JointGrid object containing the plot

    Notes
    -----
    This function will raise a warning if the dataset has fewer than
    RECOMMENDED_MIN_SAMPLES (30) data points, as density plots are not reliable
    with small sample sizes.

    Examples
    --------
    Basic jointplot with default settings:

    >>> import soundscapy as sspy
    >>> import matplotlib.pyplot as plt
    >>> data = sspy.isd.load()
    >>> data = sspy.add_iso_coords(data)
    >>> g = sspy.jointplot(data)
    >>> plt.show() # xdoctest: +SKIP

    Jointplot with histogram marginals:

    >>> g = sspy.jointplot(data, marginal_kind="hist")
    >>> plt.show() # xdoctest: +SKIP

    Jointplot with custom styling and grouping:

    >>> g = sspy.jointplot(
    ...     data,
    ...     hue="LocationID",
    ...     incl_scatter=True,
    ...     density_type="simple",
    ...     diagonal_lines=True,
    ...     figsize=(6, 6),
    ...     title="Grouped Soundscape Analysis"
    ... )
    >>> plt.show() # xdoctest: +SKIP
    >>> plt.close('all')

    """
    # Check if dataset is large enough for density plots
    _valid_density(data)

    style_args, subplots_args, kwargs = _setup_style_and_subplots_args_from_kwargs(
        x=x, y=y, prim_labels=prim_labels, kwargs=kwargs
    )

    # Initialize default dicts if None
    scatter_args = ScatterParams()
    scatter_args.update(**scatter_kws) if scatter_kws is not None else None

    joint_kws = {} if joint_kws is None else joint_kws
    marginal_kws = (
        {"fill": True, "common_norm": False} if marginal_kws is None else marginal_kws
    )

    if density_type == "simple":
        thresh = DEFAULT_SIMPLE_DENSITY_PARAMS["thresh"]
        levels = DEFAULT_SIMPLE_DENSITY_PARAMS["levels"]
        alpha = DEFAULT_SIMPLE_DENSITY_PARAMS["alpha"]
        incl_outline = True

    # Handle hue and color
    if hue is None:
        # Removes the palette if no hue is specified
        palette = None
        color = sns.color_palette("colorblind", 1)[0] if color is None else color

    # Create the joint grid
    g = sns.JointGrid(
        data=data,
        x=x,
        y=y,
        hue=hue,
        palette=palette,
        # height=figsize[0],  # Use figsize for height
        xlim=style_args.xlim,
        ylim=style_args.ylim,
    )

    # Add the density plot to the joint plot area
    density(
        data,
        x=x,
        y=y,
        incl_scatter=incl_scatter,
        density_type=density_type,
        title=None,  # We'll set the title separately
        ax=g.ax_joint,
        hue=hue,
        palette=palette,
        color=color,
        scatter_kws=scatter_kws,
        incl_outline=incl_outline,
        legend_loc=style_args.legend_loc,
        alpha=alpha,
        legend=legend,
        fill=fill,
        levels=levels,
        thresh=thresh,
        bw_adjust=bw_adjust,
        diagonal_lines=style_args.diagonal_lines,
        xlim=style_args.xlim,
        ylim=style_args.ylim,
        **joint_kws,
    )

    # Add the marginal plots
    if marginal_kind == "hist":
        sns.histplot(
            data=data,
            x=x,
            hue=hue,
            palette=palette,
            ax=g.ax_marg_x,
            binrange=style_args.xlim,
            legend=False,
            **marginal_kws,
        )
        sns.histplot(
            data=data,
            y=y,
            hue=hue,
            palette=palette,
            ax=g.ax_marg_y,
            binrange=style_args.ylim,
            legend=False,
            **marginal_kws,
        )
    elif marginal_kind == "kde":
        sns.kdeplot(
            data=data,
            x=x,
            hue=hue,
            palette=palette,
            ax=g.ax_marg_x,
            bw_adjust=bw_adjust,
            legend=False,
            **marginal_kws,
        )
        sns.kdeplot(
            data=data,
            y=y,
            hue=hue,
            palette=palette,
            ax=g.ax_marg_y,
            bw_adjust=bw_adjust,
            legend=False,
            **marginal_kws,
        )

    # Set title
    if title is not None:
        g.ax_marg_x.set_title(title, pad=6.0)

    _set_style()
    _circumplex_grid(
        ax=g.ax_joint,
        xlim=style_args.get("xlim"),
        ylim=style_args.get("ylim"),
        xlabel=style_args.get("xlabel"),
        ylabel=style_args.get("ylabel"),
        diagonal_lines=style_args.get("diagonal_lines"),
        prim_ax_fontdict=style_args.get("prim_ax_fontdict"),
    )

    if legend is not None and hue is not None:
        _move_legend(ax=g.ax_joint, new_loc=style_args.get("legend_loc"))

    return g

scatter

scatter(data, title='Soundscape Scatter Plot', ax=None, *, x='ISOPleasant', y='ISOEventful', hue=None, palette='colorblind', legend='auto', prim_labels=None, **kwargs)

Plot ISOcoordinates as scatter points on a soundscape circumplex grid.

Creates a scatter plot of data on a standardized circumplex grid with the custom Soundscapy styling for soundscape circumplex visualisations.

PARAMETER DESCRIPTION
data

Input data structure containing coordinate data, typically with ISOPleasant and ISOEventful columns.

TYPE: DataFrame

x

Column name for x variable, by default "ISOPleasant"

TYPE: str DEFAULT: 'ISOPleasant'

y

Column name for y variable, by default "ISOEventful"

TYPE: str DEFAULT: 'ISOEventful'

title

Title to add to circumplex plot, by default "Soundscape Scatter Plot"

TYPE: str | None DEFAULT: 'Soundscape Scatter Plot'

ax

Pre-existing matplotlib axes for the plot, by default None If None call matplotlib.pyplot.subplots with figsize internally.

TYPE: Axes DEFAULT: None

hue

Grouping variable that will produce points with different colors.

Can be either categorical or numeric, although color mapping will behave differently in latter case, by default None

TYPE: str | ndarray | Series | None DEFAULT: None

palette

Method for choosing the colors to use when mapping the hue semantic. String values are passed to seaborn.color_palette(). List or dict values imply categorical mapping, while a colormap object implies numeric mapping, by default "colorblind"

TYPE: SeabornPaletteType DEFAULT: 'colorblind'

color

Color to use for the plot elements when not using hue mapping, by default "#0173B2" (first color from colorblind palette)

TYPE: ColorType | None

figsize

Size of the figure to return if ax is None, by default (5, 5)

TYPE: tuple[int, int]

s

Size of scatter points, by default 20

TYPE: float

legend

How to draw the legend. If "brief", numeric hue and size variables will be represented with a sample of evenly spaced values. If "full", every group will get an entry in the legend. If "auto", choose between brief or full representation based on number of levels.

If False, no legend data is added and no legend is drawn, by default "auto"

TYPE: (auto, brief, full, False) DEFAULT: "auto"

prim_labels

Deprecated. Use xlabel and ylabel parameters instead.

TYPE: bool | None DEFAULT: None

PARAMETER DESCRIPTION
xlabel

Custom axis labels. By default "\(P_{ISO}\)" and "\(E_{ISO}\)" with math rendering.

If None is passed, the column names (x and y) will be used as labels.

If a string is provided, it will be used as the label.

If False is passed, axis labels will be hidden.

ylabel

Custom axis labels. By default "\(P_{ISO}\)" and "\(E_{ISO}\)" with math rendering.

If None is passed, the column names (x and y) will be used as labels.

If a string is provided, it will be used as the label.

If False is passed, axis labels will be hidden.

xlim

Limits for x and y axes, by default (-1, 1) for both

TYPE: tuple[float, float]

ylim

Limits for x and y axes, by default (-1, 1) for both

TYPE: tuple[float, float]

legend_loc

Location of legend, by default "best"

TYPE: MplLegendLocType

diagonal_lines

Whether to include diagonal dimension labels (e.g. calm, etc.), by default False

TYPE: bool

prim_ax_fontdict

Font dictionary for axis labels with these defaults:

{ "family": "sans-serif", "fontstyle": "normal", "fontsize": "large", "fontweight": "medium", "parse_math": True, "c": "black", "alpha": 1, }

TYPE: dict

fontsize

Direct parameters for font styling in axis labels

fontweight

Direct parameters for font styling in axis labels

fontstyle

Direct parameters for font styling in axis labels

family

Direct parameters for font styling in axis labels

c

Direct parameters for font styling in axis labels

alpha

Direct parameters for font styling in axis labels

parse_math

Direct parameters for font styling in axis labels

RETURNS DESCRIPTION
Axes object containing the plot.
Notes

This function applies special styling appropriate for circumplex plots including gridlines, axis labels, and proportional axes.

Examples:

Basic scatter plot with default settings:

>>> import soundscapy as sspy
>>> import matplotlib.pyplot as plt
>>> data = sspy.isd.load()
>>> data = sspy.add_iso_coords(data)
>>> ax = sspy.scatter(data)
>>> plt.show() # xdoctest: +SKIP

Scatter plot with grouping by location:

>>> ax = sspy.scatter(data, hue="LocationID", diagonal_lines=True, legend=False)
>>> plt.show() # xdoctest: +SKIP
>>> plt.close('all')
Source code in soundscapy/plotting/plot_functions.py
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
718
719
720
721
722
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
def scatter(
    data: pd.DataFrame,
    title: str | None = "Soundscape Scatter Plot",
    ax: Axes | None = None,
    *,
    x: str | None = "ISOPleasant",
    y: str | None = "ISOEventful",
    hue: str | None = None,
    palette: SeabornPaletteType | None = "colorblind",
    legend: Literal["auto", "brief", "full", False] = "auto",
    prim_labels: bool | None = None,  # Alias for primary_labels, deprecated
    **kwargs,
) -> Axes:
    """
    Plot ISOcoordinates as scatter points on a soundscape circumplex grid.

    Creates a scatter plot of data on a standardized circumplex grid with the custom
    Soundscapy styling for soundscape circumplex visualisations.

    Parameters
    ----------
    data
        Input data structure containing coordinate data, typically with ISOPleasant
        and ISOEventful columns.
    x : str, optional
        Column name for x variable, by default "ISOPleasant"
    y : str, optional
        Column name for y variable, by default "ISOEventful"
    title : str | None, optional
        Title to add to circumplex plot, by default "Soundscape Scatter Plot"
    ax : matplotlib.axes.Axes, optional
        Pre-existing matplotlib axes for the plot, by default None
        If `None` call `matplotlib.pyplot.subplots` with `figsize` internally.
    hue : str | np.ndarray | pd.Series | None, optional
        Grouping variable that will produce points with different colors.

        Can be either categorical or numeric,
        although color mapping will behave differently in latter case, by default None
    palette : SeabornPaletteType, optional
        Method for choosing the colors to use when mapping the hue semantic.
        String values are passed to seaborn.color_palette().
        List or dict values imply categorical mapping, while a colormap object
        implies numeric mapping, by default "colorblind"
    color : ColorType | None, optional
        Color to use for the plot elements when not using hue mapping,
        by default "#0173B2" (first color from colorblind palette)
    figsize : tuple[int, int], optional
        Size of the figure to return if `ax` is None, by default (5, 5)
    s : float, optional
        Size of scatter points, by default 20
    legend : {"auto", "brief", "full", False}, optional
        How to draw the legend. If "brief", numeric hue and size variables will be
        represented with a sample of evenly spaced values. If "full", every group will
        get an entry in the legend. If "auto", choose between brief or full
        representation based on number of levels.

        If False, no legend data is added and no legend is drawn, by default "auto"
    prim_labels : bool | None, optional
        Deprecated. Use xlabel and ylabel parameters instead.

    Other Parameters
    ----------------
    xlabel, ylabel
        Custom axis labels. By default "$P_{ISO}$" and "$E_{ISO}$"
        with math rendering.

        If None is passed, the column names (x and y) will be used as labels.

        If a string is provided, it will be used as the label.

        If False is passed, axis labels will be hidden.
    xlim, ylim : tuple[float, float], optional
        Limits for x and y axes, by default (-1, 1) for both
    legend_loc : MplLegendLocType, optional
        Location of legend, by default "best"
    diagonal_lines : bool, optional
        Whether to include diagonal dimension labels (e.g. calm, etc.),
        by default False
    prim_ax_fontdict : dict, optional
        Font dictionary for axis labels with these defaults:

        {
            "family": "sans-serif",
            "fontstyle": "normal",
            "fontsize": "large",
            "fontweight": "medium",
            "parse_math": True,
            "c": "black",
            "alpha": 1,
        }
    fontsize, fontweight, fontstyle, family, c, alpha, parse_math:
        Direct parameters for font styling in axis labels

    Returns
    -------
        Axes object containing the plot.

    Notes
    -----
    This function applies special styling appropriate for circumplex plots including
    gridlines, axis labels, and proportional axes.

    Examples
    --------
    Basic scatter plot with default settings:

    >>> import soundscapy as sspy
    >>> import matplotlib.pyplot as plt
    >>> data = sspy.isd.load()
    >>> data = sspy.add_iso_coords(data)
    >>> ax = sspy.scatter(data)
    >>> plt.show() # xdoctest: +SKIP

    Scatter plot with grouping by location:

    >>> ax = sspy.scatter(data, hue="LocationID", diagonal_lines=True, legend=False)
    >>> plt.show() # xdoctest: +SKIP
    >>> plt.close('all')

    """
    style_args, subplots_args, kwargs = _setup_style_and_subplots_args_from_kwargs(
        x=x, y=y, prim_labels=prim_labels, kwargs=kwargs
    )

    scatter_args = ScatterParams()
    scatter_args.update(
        data=data,
        x=x,
        y=y,
        palette=palette,
        hue=hue,
        legend=legend,
        extra="allow",
        ignore_null=False,
        **kwargs,
    )  # pass all the rest to scatter

    # Removes the palette if no hue is specified
    scatter_args.crosscheck_palette_hue()

    if ax is None:
        _, ax = plt.subplots(1, 1, figsize=subplots_args.figsize)

    p = sns.scatterplot(ax=ax, **scatter_args.as_dict())

    _set_style()
    _circumplex_grid(
        ax=ax,
        **style_args.get_multiple(
            ["xlim", "ylim", "xlabel", "ylabel", "diagonal_lines", "prim_ax_fontdict"]
        ),
    )
    if title is not None:
        _set_circum_title(
            ax=ax,
            title=title,
            xlabel=style_args.get("xlabel"),
            ylabel=style_args.get("ylabel"),
        )
    if legend is not None and hue is not None and style_args.legend_loc is not False:
        _move_legend(ax=ax, new_loc=style_args.get("legend_loc"))
    return p

scatter_plot

scatter_plot(*args, **kwargs)

Wrapper for the scatter function to maintain backwards compatibility.

PARAMETER DESCRIPTION
*args

Positional arguments to pass to the scatter function.

TYPE: tuple DEFAULT: ()

**kwargs

Keyword arguments to pass to the scatter function.

TYPE: dict DEFAULT: {}

RETURNS DESCRIPTION
Axes

The Axes object containing the plot.

Source code in soundscapy/plotting/plot_functions.py
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
def scatter_plot(*args, **kwargs) -> Axes:  # noqa: ANN002
    """
    Wrapper for the scatter function to maintain backwards compatibility.

    Parameters
    ----------
    *args : tuple
        Positional arguments to pass to the scatter function.
    **kwargs : dict
        Keyword arguments to pass to the scatter function.

    Returns
    -------
    Axes
        The Axes object containing the plot.

    """  # noqa: D401
    warnings.warn(
        "The `scatter_plot` function is deprecated and will be removed in a "
        "future version. Use `scatter` instead."
        "\nAs of v0.8, `scatter_plot` is an alias for `scatter` and does not maintain "
        "full backwards compatibility with v0.7. It may work, or some arguments may "
        "fail.",
        DeprecationWarning,
        stacklevel=2,
    )
    if kwargs.pop("backend", None) is not None:
        warnings.warn(
            "`Backend` is no longer supported in the scatter_plot function (v0.8+).",
            DeprecationWarning,
            stacklevel=2,
        )

    kwargs = {
        k: v
        for k, v in kwargs.items()
        if k not in ("backend", "show_labels", "apply_styling")
    }

    return scatter(*args, **kwargs)

show_submodules: true