-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
distplot.py
129 lines (113 loc) · 3.68 KB
/
distplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""Bokeh Distplot."""
import bokeh.plotting as bkp
import numpy as np
from . import BACKEND_KWARG_DEFAULTS
from ...kdeplot import plot_kde
from ...plot_utils import get_bins
from ....rcparams import rcParams
def plot_dist(
values,
values2=None,
color="C0",
kind="auto",
cumulative=False,
label=None,
rotated=False,
rug=False,
bw=4.5,
quantiles=None,
contour=True,
fill_last=True,
plot_kwargs=None,
fill_kwargs=None,
rug_kwargs=None,
contour_kwargs=None,
contourf_kwargs=None,
pcolormesh_kwargs=None,
hist_kwargs=None,
ax=None,
backend_kwargs=None,
**kwargs # pylint: disable=unused-argument
):
"""Bokeh distplot."""
if backend_kwargs is None:
backend_kwargs = {}
backend_kwargs = {**BACKEND_KWARG_DEFAULTS, **backend_kwargs}
if ax is None:
tools = rcParams["plot.bokeh.tools"]
output_backend = rcParams["plot.bokeh.output_backend"]
ax = bkp.figure(
width=rcParams["plot.bokeh.figure.width"],
height=rcParams["plot.bokeh.figure.height"],
output_backend=output_backend,
tools=tools,
)
if kind == "auto":
kind = "hist" if values.dtype.kind == "i" else "kde"
if kind == "hist":
_histplot_bokeh_op(
values=values, values2=values2, rotated=rotated, ax=ax, hist_kwargs=hist_kwargs
)
elif kind == "kde":
if plot_kwargs is None:
plot_kwargs = {}
plot_kwargs.setdefault("line_color", color)
legend = label is not None
plot_kde(
values,
values2,
cumulative=cumulative,
rug=rug,
label=label,
bw=bw,
quantiles=quantiles,
rotated=rotated,
contour=contour,
legend=legend,
fill_last=fill_last,
plot_kwargs=plot_kwargs,
fill_kwargs=fill_kwargs,
rug_kwargs=rug_kwargs,
contour_kwargs=contour_kwargs,
contourf_kwargs=contourf_kwargs,
pcolormesh_kwargs=pcolormesh_kwargs,
ax=ax,
backend="bokeh",
# TODO: Revisit this when I refactor backend args for kde
show=False,
)
else:
raise TypeError('Invalid "kind":{}. Select from {{"auto","kde","hist"}}'.format(kind))
# TODO: Temporary setting just to make sure tests work. This needs to be removed
if backend_kwargs["show"] is True:
bkp.show(ax, toolbar_location="above")
return ax
def _histplot_bokeh_op(values, values2, rotated, ax, hist_kwargs):
"""Add a histogram for the data to the axes."""
if values2 is not None:
raise NotImplementedError("Insert hexbin plot here")
legend_label = hist_kwargs.pop("label", None)
if legend_label:
hist_kwargs["legend_label"] = legend_label
color = hist_kwargs.pop("color", False)
if color:
hist_kwargs["fill_color"] = color
hist_kwargs["line_color"] = color
# remove defaults for mpl
hist_kwargs.pop("rwidth", None)
hist_kwargs.pop("align", None)
hist_kwargs.pop("density", None)
hist_kwargs.pop("orientation", None)
bins = hist_kwargs.pop("bins", None)
if bins is None:
bins = get_bins(values)
density = hist_kwargs.pop("density", True)
hist, edges = np.histogram(values, density=density, bins=bins)
if hist_kwargs.pop("cumulative", False):
hist = np.cumsum(hist)
hist /= hist[-1]
if rotated:
ax.quad(top=edges[:-1], bottom=edges[1:], left=0, right=hist, **hist_kwargs)
else:
ax.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], **hist_kwargs)
return ax