-
-
Notifications
You must be signed in to change notification settings - Fork 394
/
stats.py
162 lines (113 loc) · 5.18 KB
/
stats.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
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
import param
from ..mixins import MultiDistributionMixin
from .chart import ChartPlot
from .element import ColorbarPlot, ElementPlot
from .selection import PlotlyOverlaySelectionDisplay
class BivariatePlot(ChartPlot, ColorbarPlot):
filled = param.Boolean(default=False)
ncontours = param.Integer(default=None)
style_opts = ['visible', 'cmap', 'showlabels', 'labelfont', 'labelformat', 'showlines']
_style_key = 'contours'
selection_display = PlotlyOverlaySelectionDisplay()
@classmethod
def trace_kwargs(cls, is_geo=False, **kwargs):
return {'type': 'histogram2dcontour'}
def graph_options(self, element, ranges, style, **kwargs):
opts = super().graph_options(element, ranges, style, **kwargs)
copts = self.get_color_opts(element.vdims[0], element, ranges, style)
if self.ncontours:
opts['autocontour'] = False
opts['ncontours'] = self.ncontours
# Make line width a little wider (default is less than 1)
opts['line'] = {'width': 1}
# Configure contours
opts['contours'] = {
'coloring': 'fill' if self.filled else 'lines',
'showlines': style.get('showlines', True)
}
# Add colorscale
opts['colorscale'] = copts['colorscale']
# Add colorbar
if 'colorbar' in copts:
opts['colorbar'] = copts['colorbar']
opts['showscale'] = copts.get('showscale', False)
# Add visible
opts['visible'] = style.get('visible', True)
return opts
class DistributionPlot(ElementPlot):
bandwidth = param.Number(default=None, doc="""
The bandwidth of the kernel for the density estimate.""")
cut = param.Number(default=3, doc="""
Draw the estimate to cut * bw from the extreme data points.""")
filled = param.Boolean(default=True, doc="""
Whether the bivariate contours should be filled.""")
style_opts = ['visible', 'color', 'dash', 'line_width']
_style_key = 'line'
selection_display = PlotlyOverlaySelectionDisplay()
@classmethod
def trace_kwargs(cls, is_geo=False, **kwargs):
return {'type': 'scatter', 'mode': 'lines'}
class MultiDistributionPlot(MultiDistributionMixin, ElementPlot):
def get_data(self, element, ranges, style, **kwargs):
if element.kdims:
groups = element.groupby(element.kdims).items()
else:
groups = [(element.label, element)]
plots = []
axis = 'x' if self.invert_axes else 'y'
for key, group in groups:
if element.kdims:
if isinstance(key, str):
key = (key,)
label = ','.join([d.pprint_value(v) for d, v in zip(element.kdims, key)])
else:
label = key
data = {axis: group.dimension_values(group.vdims[0]), 'name': label}
plots.append(data)
return plots
class BoxWhiskerPlot(MultiDistributionPlot):
boxpoints = param.ObjectSelector(objects=["all", "outliers",
"suspectedoutliers", False],
default='outliers', doc="""
Which points to show, valid options are 'all', 'outliers',
'suspectedoutliers' and False""")
jitter = param.Number(default=0, doc="""
Sets the amount of jitter in the sample points drawn. If "0",
the sample points align along the distribution axis. If "1",
the sample points are drawn in a random jitter of width equal
to the width of the box(es).""")
mean = param.ObjectSelector(default=False, objects=[True, False, 'sd'],
doc="""
If "True", the mean of the box(es)' underlying distribution
is drawn as a dashed line inside the box(es). If "sd" the
standard deviation is also drawn.""")
style_opts = ['visible', 'color', 'alpha', 'outliercolor', 'marker', 'size']
_style_key = 'marker'
selection_display = PlotlyOverlaySelectionDisplay()
@classmethod
def trace_kwargs(cls, is_geo=False, **kwargs):
return {'type': 'box'}
def graph_options(self, element, ranges, style, **kwargs):
options = super().graph_options(element, ranges, style, **kwargs)
options['boxmean'] = self.mean
options['jitter'] = self.jitter
return options
class ViolinPlot(MultiDistributionPlot):
box = param.Boolean(default=True, doc="""
Whether to draw a boxplot inside the violin""")
meanline = param.Boolean(default=False, doc="""
If "True", the mean of the box(es)' underlying distribution
is drawn as a dashed line inside the box(es). If "sd" the
standard deviation is also drawn.""")
style_opts = ['visible', 'color', 'alpha', 'outliercolor', 'marker', 'size']
_style_key = 'marker'
@classmethod
def trace_kwargs(cls, is_geo=False, **kwargs):
return {'type': 'violin'}
def graph_options(self, element, ranges, style, **kwargs):
options = super().graph_options(
element, ranges, style, **kwargs
)
options['meanline'] = {'visible': self.meanline}
options['box'] = {'visible': self.box}
return options