This repository was archived by the owner on Mar 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 562
Expand file tree
/
Copy pathgeom_boxplot.py
More file actions
executable file
·84 lines (71 loc) · 2.82 KB
/
Copy pathgeom_boxplot.py
File metadata and controls
executable file
·84 lines (71 loc) · 2.82 KB
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
from .geom import geom
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import numpy as np
class geom_boxplot(geom):
"""
Box and whiskers chart
Parameters
----------
x:
x values for bins/categories
y:
values that will be used for box/whisker calculations
color:
color of line
flier_marker:
type of marker used ('o', '^', 'D', 'v', 's', '*', 'p', '8', "_", "|", "_")
Examples
--------
"""
DEFAULT_AES = {'y': None, 'color': 'black', 'flier_marker': '+'}
REQUIRED_AES = {'x', 'y'}
DEFAULT_PARAMS = {}
def plot(self, ax, data, _aes, x_levels):
(data, _aes) = self._update_data(data, _aes)
params = self._get_plot_args(data, _aes)
variables = _aes.data
x = data[variables['x']]
y = data[variables['y']]
xticks = []
for (i, xvalue) in enumerate(x_levels):
subset = data[data[variables['x']] == xvalue]
xi = np.repeat(i, len(subset))
yvalues = subset[variables['y']]
xticks.append(i)
bounds_25_75 = yvalues.quantile([0.25, 0.75]).values
bounds_5_95 = yvalues.quantile([0.05, 0.95]).values
if self.params.get('outliers', True):
mask = ((yvalues > bounds_5_95[1]) | (yvalues < bounds_5_95[0])).values
ax.scatter(x=xi[mask], y=yvalues[mask], c=self.params.get('outlier_color', 'black'))
if self.params.get('lines', True):
ax.vlines(x=i, ymin=bounds_25_75[1], ymax=bounds_5_95[1])
ax.vlines(x=i, ymin=bounds_5_95[0], ymax=bounds_25_75[0])
if self.params.get('notch', False):
ax.hlines(bounds_5_95[0], i - 0.25/2, i + 0.25/2, linewidth=2)
ax.hlines(bounds_5_95[1], i - 0.25/2, i + 0.25/2, linewidth=2)
if self.params.get('median', True):
ax.hlines(yvalues.median(), i - 0.25, i + 0.25, linewidth=2)
if self.params.get('box', True):
params = {
'facecolor': 'white',
'edgecolor': 'black',
'linewidth': 1
}
ax.add_patch(
patches.Rectangle(
(i - 0.25, bounds_25_75[0]),
0.5,
bounds_25_75[1] - bounds_25_75[0],
**params
)
)
else:
ax.vlines(x=i, ymin=bounds_25_75[0], ymax=bounds_25_75[1])
# q = ax.boxplot(x, vert=True)
# plt.setp(q['boxes'], color=params['color'])
# plt.setp(q['whiskers'], color=params['color'])
# plt.setp(q['fliers'], color=params['color'])
ax.autoscale_view()
ax.set_xticks(xticks)
ax.set_xticklabels(x_levels)