1
"""Matplotlib distplot."""
2 4
import matplotlib.pyplot as plt
3 4
import numpy as np
4

5 4
from ....stats.density_utils import get_bins
6 4
from ...kdeplot import plot_kde
7 4
from ...plot_utils import _scale_fig_size
8 4
from . import backend_kwarg_defaults, backend_show, create_axes_grid, matplotlib_kwarg_dealiaser
9

10

11 4
def plot_dist(
12
    values,
13
    values2,
14
    color,
15
    kind,
16
    cumulative,
17
    label,
18
    rotated,
19
    rug,
20
    bw,
21
    circular,
22
    quantiles,
23
    contour,
24
    fill_last,
25
    figsize,
26
    textsize,
27
    plot_kwargs,
28
    fill_kwargs,
29
    rug_kwargs,
30
    contour_kwargs,
31
    contourf_kwargs,
32
    pcolormesh_kwargs,
33
    hist_kwargs,
34
    is_circular,
35
    ax,
36
    backend_kwargs,
37
    show,
38
):
39
    """Matplotlib distplot."""
40 4
    if backend_kwargs is None:
41 4
        backend_kwargs = {}
42

43 4
    backend_kwargs = {
44
        **backend_kwarg_defaults(),
45
        **backend_kwargs,
46
    }
47

48 4
    figsize, *_ = _scale_fig_size(figsize, textsize)
49

50 4
    backend_kwargs.setdefault("figsize", figsize)
51 4
    backend_kwargs["squeeze"] = True
52 4
    backend_kwargs.setdefault("subplot_kw", {})
53 4
    backend_kwargs["subplot_kw"].setdefault("polar", is_circular)
54

55 4
    if ax is None:
56 4
        _, ax = create_axes_grid(1, backend_kwargs=backend_kwargs)
57

58 4
    if kind == "hist":
59 4
        hist_kwargs = matplotlib_kwarg_dealiaser(hist_kwargs, "hist")
60 4
        hist_kwargs.setdefault("cumulative", cumulative)
61 4
        hist_kwargs.setdefault("color", color)
62 4
        hist_kwargs.setdefault("label", label)
63 4
        hist_kwargs.setdefault("rwidth", 0.9)
64 4
        hist_kwargs.setdefault("align", "left")
65 4
        hist_kwargs.setdefault("density", True)
66

67 4
        if rotated:
68 4
            hist_kwargs.setdefault("orientation", "horizontal")
69
        else:
70 4
            hist_kwargs.setdefault("orientation", "vertical")
71

72 4
        ax = _histplot_mpl_op(
73
            values=values,
74
            values2=values2,
75
            rotated=rotated,
76
            ax=ax,
77
            hist_kwargs=hist_kwargs,
78
            is_circular=is_circular,
79
        )
80

81 4
    elif kind == "kde":
82 4
        plot_kwargs = matplotlib_kwarg_dealiaser(plot_kwargs, "plot")
83 4
        plot_kwargs.setdefault("color", color)
84 4
        legend = label is not None
85

86 4
        ax = plot_kde(
87
            values,
88
            values2,
89
            cumulative=cumulative,
90
            rug=rug,
91
            label=label,
92
            bw=bw,
93
            circular=circular,
94
            quantiles=quantiles,
95
            rotated=rotated,
96
            contour=contour,
97
            legend=legend,
98
            fill_last=fill_last,
99
            textsize=textsize,
100
            plot_kwargs=plot_kwargs,
101
            fill_kwargs=fill_kwargs,
102
            rug_kwargs=rug_kwargs,
103
            contour_kwargs=contour_kwargs,
104
            contourf_kwargs=contourf_kwargs,
105
            pcolormesh_kwargs=pcolormesh_kwargs,
106
            ax=ax,
107
            backend="matplotlib",
108
            backend_kwargs=backend_kwargs,
109
            is_circular=is_circular,
110
            show=show,
111
        )
112

113 4
    if backend_show(show):
114 0
        plt.show()
115

116 4
    return ax
117

118

119 4
def _histplot_mpl_op(values, values2, rotated, ax, hist_kwargs, is_circular):
120
    """Add a histogram for the data to the axes."""
121 4
    bins = hist_kwargs.pop("bins", None)
122

123 4
    if is_circular == "degrees":
124 4
        if bins is None:
125 4
            bins = get_bins(values)
126 4
        values = np.deg2rad(values)
127 4
        bins = np.deg2rad(bins)
128

129 4
    elif is_circular:
130 4
        labels = [
131
            r"0",
132
            r"π/4",
133
            r"π/2",
134
            r"3π/4",
135
            r"π",
136
            r"5π/4",
137
            r"3π/2",
138
            r"7π/4",
139
        ]
140

141 4
        ax.set_xticklabels(labels)
142

143 4
    if values2 is not None:
144 0
        raise NotImplementedError("Insert hexbin plot here")
145

146 4
    if bins is None:
147 4
        bins = get_bins(values)
148

149 4
    n, bins, _ = ax.hist(np.asarray(values).flatten(), bins=bins, **hist_kwargs)
150

151 4
    if rotated:
152 4
        ax.set_yticks(bins[:-1])
153 4
    elif not is_circular:
154 4
        ax.set_xticks(bins[:-1])
155

156 4
    if is_circular:
157 4
        ax.set_ylim(0, 1.5 * n.max())
158 4
        ax.set_yticklabels([])
159

160 4
    if hist_kwargs.get("label") is not None:
161 0
        ax.legend()
162

163 4
    return ax

Read our documentation on viewing source code .

Loading