Skip to content

ridgeplot.ridge_plot

This module contains the main function to plot ridgeplots.

ridgeplot(ax, data, xlim=None, fill_colors=None, line_colors=None, label_size=10.0, fill_alpha=0.5)

plotting a ridgeplot

Example
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from ridgeplot.ridge_plot import ridgeplot

>>> data = {}
>>> for i in range(10):
>>>    data['data_{}'.format(i)] = np.random.randn(100) * (i+1)

>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ridgeplot(ax, data, xlim=(-20,20))

Parameters:

Name Type Description Default
ax Axes

a matplotlib ax object for writing the plot

required
data dict[str, list[float]]

a dictionary of data, key is the label of the group, values are the data values in the group

required
xlim Optional[tuple[float, float]]

x-limits for the plot (xmin, xmax)

None
fill_colors Optional[list[str]]

colors for the fill under the distribution, must be same length as input data (default: all steelblue)

None
line_colors Optional[list[str]]

colors for the line drawing the distribution, must be same length as input data (default: all white)

None
label_size float

label size of the name of each distribution

10.0
fill_alpha float

alpha value for the fill under the distribution (default: 0.5)

0.5

Returns:

Type Description
None

NoneType

Source code in ridgeplot/ridge_plot.py
 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
def ridgeplot(
    ax: matplotlib.axes._axes.Axes,
    data: dict[str, list[float]],
    xlim: Optional[tuple[float, float]] = None,
    fill_colors: Optional[list[str]] = None,
    line_colors: Optional[list[str]] = None,
    label_size: float = 10.0,
    fill_alpha: float = 0.5,
) -> None:
    """
    plotting a ridgeplot

    Example:
        ```
        >>> import numpy as np
        >>> import matplotlib.pyplot as plt
        >>> from ridgeplot.ridge_plot import ridgeplot

        >>> data = {}
        >>> for i in range(10):
        >>>    data['data_{}'.format(i)] = np.random.randn(100) * (i+1)

        >>> fig = plt.figure()
        >>> ax = fig.add_subplot(111)
        >>> ridgeplot(ax, data, xlim=(-20,20))
        ```

    Args:
        ax: a matplotlib ax object for writing the plot
        data: a dictionary of data, key is the label of
            the group, values are the data values in the group
        xlim: x-limits for the plot (xmin, xmax)
        fill_colors: colors for the fill under the distribution,
            must be same length as input data (default: all steelblue)
        line_colors: colors for the line drawing the distribution,
            must be same length as input data (default: all white)
        label_size: label size of the name of each distribution
        fill_alpha: alpha value for the fill under the distribution (default: 0.5)

    Returns:
        NoneType
    """

    # assigning colors if not given
    if fill_colors is None:
        fill_colors = len(data) * ["steelblue"]

    if line_colors is None:
        line_colors = len(data) * ["white"]

    # assigning xlims if not given
    if xlim is not None:
        xmin, xmax = xlim
    else:
        xmin = min(first(data.values()))
        xmax = max(first(data.values()))

    # data validation
    if len(fill_colors) != len(data):
        raise RidgePlotError("fill_colors must be same length as data")

    if len(line_colors) != len(data):
        raise RidgePlotError("line_colors must be same length as data")

    xlines = []
    for sample_number, (data_key, data_values) in enumerate(data.items()):
        data_values_array = np.array(data_values, dtype="float")
        xs = np.arange(xmin, xmax * 1.1, 0.01)  # xaxis is 10% wider than data max
        kde = gaussian_kde(data_values_array)

        baseline = -sample_number * 0.7
        ys = scaling(kde.pdf(xs)) + baseline
        ax.plot(xs, ys, color=line_colors[sample_number], lw=2)
        ax.fill_between(x=xs, y1=ys, y2=baseline, color=fill_colors[sample_number], alpha=fill_alpha)
        xlines.append(baseline)
        ax.text(xmin, baseline, data_key, ha="right", va="bottom", fontsize=label_size)
    # ax.hlines(xlines, xmin=xmin, xmax=xmax * 1.1, color="black", lw=1)
    ax.legend(loc="center").set_visible(False)
    ax.get_yaxis().set_visible(False)
    for side in ["left", "right", "top"]:
        ax.spines[side].set_visible(False)
    ax.set_xlim(xmin, xmax)