-
Notifications
You must be signed in to change notification settings - Fork 369
/
boxplot.js
315 lines (295 loc) · 11.6 KB
/
boxplot.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import React from 'react';
import TitleAndDescription from '../components/TitleAndDescription';
import Layout from '../components/Layout';
import Container from 'react-bootstrap/Container';
import Contact from '../components/Contact';
import Row from 'react-bootstrap/Row';
import ChartImageContainer from '../components/ChartImageContainer';
import ChartFamilySection from '../components/ChartFamilySection';
import { Link } from 'gatsby';
import { Matplotlib, Seaborn } from '../components/MiscellaneousLogos';
import { Button, Col } from 'react-bootstrap';
import CodeChunk from '../components/CodeChunk';
import ChartImage from '../components/ChartImage';
import Spacing from '../components/Spacing';
import { SEO } from '../components/SEO';
const chartDescription = (
<>
<p>
A <a href="https://www.data-to-viz.com/caveat/boxplot.html">boxplot</a>{' '}
summarizes the <b>distribution</b> of a numeric variable for one or
several groups. It allows to quickly get the <b>median</b>,{' '}
<b>quartiles</b> and <b>outliers</b> but also hides the dataset individual
data points.
</p>
<p>
In python, boxplots can be made with both{' '}
<Link href="/seaborn">seaborn</Link> and{' '}
<Link href="/matplotlib">matplotlib</Link> as they both offer a{' '}
<code>boxplot()</code> function made for the job.
</p>
</>
);
const quickCode = `# library & dataset
import seaborn as sns
df = sns.load_dataset('iris')
sns.boxplot( x=df["species"], y=df["sepal_length"] )
`;
export const Head = () => (
<SEO
title="Python Boxplot Gallery | Dozens of examples with code"
seoDescription="A collection of boxplot examples made with Python, coming with explanation and reproducible code"
isRaptiveEnabled={false}
/>
);
export default function Boxplot() {
return (
<Layout isTocEnabled>
<TitleAndDescription title="Boxplot" description={chartDescription} />
<Container>
<h2 id="Quick">⏱ Quick start</h2>
<Row className="align-items-center">
<Col md={6}>
<p>
<code>Seaborn</code> is definitely the best library to quickly
build a boxplot. It offers a dedicated <code>boxplot()</code>{' '}
function that roughly works as follows:🔥
</p>
</Col>
<Col md={6}>
<Link to={'/30-basic-boxplot-with-seaborn'}>
<ChartImage
imgName="30_Basic_Box_seaborn2"
caption="Basic boxplot with Python and Seaborn from various data input formats."
/>
</Link>
</Col>
</Row>
<CodeChunk>{quickCode}</CodeChunk>
</Container>
<Spacing />
<div className="greySection">
<Container>
<h2 id="Warning">⚠️ Mind the boxplot</h2>
<p>
A boxplot is an awesome way to summarize the distribution of a
variable. However it hides the real distribution and the sample
size. Check the 3 charts below that are based on the exact same
dataset.
</p>
<p>
To read more about this, visit{' '}
<a href="https://www.data-to-viz.com/caveat/boxplot.html">
data-to-viz.com
</a>{' '}
that has a dedicated article.
</p>
<Row>
<ChartImageContainer
imgName="39_Bad_boxplot1"
caption="Basic boxplot. You can quickly read the median, quartiles and outliers of each group."
linkTo="/39-hidden-data-under-boxplot"
/>
<ChartImageContainer
imgName="39_Bad_boxplot2"
caption="If you add individual points with jitter, a bimodal distribution appears for group B"
linkTo="/39-hidden-data-under-boxplot"
/>
<ChartImageContainer
imgName="39_Bad_boxplot3"
caption="If you have a very large dataset, the violin plot is a better alternative than jittering"
linkTo="/39-hidden-data-under-boxplot"
/>
</Row>
<Link to="/39-hidden-data-under-boxplot">
<Button size="sm">Code and more</Button>
</Link>
</Container>
</div>
<Spacing />
<Container>
<h2 id="Seaborn">
<Seaborn />
Boxplots with <code>Seaborn</code>
</h2>
<p>
<code>Seaborn</code> is a python library allowing to make better
charts easily. The <code>boxplot</code> function should get you
started in minutes. The examples below aim at showcasing the various
possibilities this function offers.
</p>
<Row>
<ChartImageContainer
imgName="30_Basic_Box_seaborn2"
caption="Let's start basic. The most simple boxplot, based on 3 differents input formats"
linkTo="/30-basic-boxplot-with-seaborn"
/>
<ChartImageContainer
imgName="33_Custom_Boxplot_color_Seaborn5"
caption="Everything you need concerning color customization on your boxplot: transparency, palette in use, manual control.."
linkTo="/33-control-colors-of-boxplot-seaborn"
/>
<ChartImageContainer
imgName="33_Custom_Boxplot_color_Seaborn4"
caption="Learn how to highlight a specific group in the dataset to make your point more obvious"
linkTo="/33-control-colors-of-boxplot-seaborn"
/>
<ChartImageContainer
imgName="34_Grouped_Boxplot_Seaborn"
caption="If you have group and subgroups, you can build a grouped boxplot"
linkTo="/34-grouped-boxplot"
/>
<ChartImageContainer
imgName="35_Specific_order_Boxplot_Seaborn1"
caption="Control the order of groups in the boxplot. It makes the chart more insightful"
linkTo="/35-control-order-of-boxplot"
/>
<ChartImageContainer
imgName="36_Boxplot_with_Jitter_Seaborn"
caption="To avoid hiding information, you can add individual data points with jitter"
linkTo="/36-add-jitter-over-boxplot-seaborn"
/>
<ChartImageContainer
imgName="38_Number_of_obs_on_boxplot_seaborn"
caption="Since individual data points are hidden, it is a good practice to show the sample size under each box"
linkTo="/38-show-number-of-observation-on-boxplot"
/>
<ChartImageContainer
imgName="32_Custom_Boxplot_Appearance_Seaborn1-1"
caption="Customization: border width"
linkTo="/32-custom-boxplot-appearance-seaborn"
/>
<ChartImageContainer
imgName="32_Custom_Boxplot_Appearance_Seaborn2"
caption="Customization: use notch"
linkTo="/32-custom-boxplot-appearance-seaborn"
/>
<ChartImageContainer
imgName="32_Custom_Boxplot_Appearance_Seaborn3"
caption="Customization: box width"
linkTo="/32-custom-boxplot-appearance-seaborn"
/>
<ChartImageContainer
imgName="54_Grouped_violinplot_Seaborn"
caption="If you have both groups and subgroups, you'll be interested in a grouped violin plot"
linkTo="/54-grouped-violinplot"
/>
<ChartImageContainer
imgName="31-horizontal-boxplot-with-seaborn"
caption="Horizontal boxplot with seaborn"
linkTo="/31-horizontal-boxplot-with-seaborn"
/>
</Row>
</Container>
<Spacing />
<Container>
<h2 id="matplotlib">
<Matplotlib />
Boxplots with <code>Matplotlib</code>
</h2>
<p>
<Link href="/matplotlib">Matplotlib</Link> also has a{' '}
<code>boxplot()</code> function made to build boxplots.
</p>
<p>
The following tutorials will guide you from its basic usage to the
finest customization:
</p>
<Row>
<ChartImageContainer
imgName="533-introduction-boxplots-matplotlib"
caption="Simple boxplot with matplotlib"
linkTo="/533-introduction-boxplots-matplotlib"
/>
<ChartImageContainer
imgName="542-custom-boxplots-matplotlib"
caption="Flipped, notched and customized boxplot"
linkTo="/542-custom-boxplots-matplotlib"
/>
<ChartImageContainer
imgName="543-grouped-boxplots-matplotlib"
caption="Grouped boxplot"
linkTo="/543-grouped-boxplots-matplotlib"
/>{' '}
<ChartImageContainer
imgName="509-introduction-to-swarm-plot-in-matplotlib-3"
caption="Beeswarm and boxplot combination"
linkTo="/509-introduction-to-swarm-plot-in-matplotlib"
/>
<ChartImageContainer
imgName="557-anova-visualization-with-matplotlib-1"
caption="Boxplot and ANOVA results on top"
linkTo="/557-anova-visualization-with-matplotlib"
/>{' '}
<ChartImageContainer
imgName="534-highly-customized-layout"
caption="Subplot, title, and margin customization"
linkTo="/534-highly-customized-layout"
/>
<ChartImageContainer
imgName="584-introduction-hatch-matplotlib-4"
caption="Add patterns to your boxplot"
linkTo="/584-introduction-hatch-matplotlib"
/>
</Row>
</Container>
{/* <div className="greySection">
<Container>
<FunctionExploration functionName={'violin'} />
</Container>
</div> */}
<Spacing />
<Container>
<h2 id="Best">
<Matplotlib />
Best python boxplot examples
</h2>
<p>
The web is full of astonishing charts made by awesome bloggers, (often
using <a href="https://www.r-graph-gallery.com">R</a>). The{' '}
<a href="https://python-graph-gallery.com">Python graph gallery</a>{' '}
tries to display (or translate from R) some of the best creations and
explain how their source code works. If you want to display your work
here, please drop me a word or even better, submit a{' '}
<a href="https://github.com/holtzy/The-Python-Graph-Gallery">
Pull Request
</a>
!
</p>
<Row>
<Col xs={12} md={6}>
<Link to={'/web-ggbetweenstats-with-matplotlib'}>
<ChartImage
imgName={'web-ggbetweenstats-with-matplotlib-square'}
caption={
'A combination of a violin plot and a boxplot. Allows the comparison of several groups with statistical test results on top.'
}
/>
</Link>
</Col>
<Col xs={12} md={6}>
<Link to={'/raincloud-plot-with-matplotlib-and-ptitprince'}>
<ChartImage
imgName={'raincloud-plot-with-matplotlib-and-ptitprince'}
caption={
'Combining boxplot and density chart. A great way to display the distribution of a variable for several groups.'
}
/>
</Link>
</Col>
</Row>
</Container>
<Spacing />
<div className="greySection" id="related">
<Container>
<ChartFamilySection chartFamily="distribution" />
</Container>
</div>
<Spacing />
<Container>
<Contact />
</Container>
<Spacing />
</Layout>
);
}