Skip to content

Commit 77c0639

Browse files
author
Joseph Damiba
committed
update to content
1 parent e505acd commit 77c0639

File tree

1 file changed

+83
-1
lines changed

1 file changed

+83
-1
lines changed

doc/python/box-plots.md

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,49 @@ fig = px.box(df, x="time", y="total_bill", points="all")
7171
fig.show()
7272
```
7373

74+
### Choosing The Algorithm For Computing Quartiles
75+
76+
By default, quartiles for box plots are computed using a linear algorithm method (see #10 listed on http://www.amstat.org/publications/jse/v14n3/langford.html for more details). However, you can also choose to use an `exclusive` or an `inclusive` algorithm to compute quartiles.
77+
78+
The *exclusive* algorithm uses the median to divide the ordered dataset into two halves. If the sample is odd, it does not includes the median in either half. Q1 is then the median of the lower half and Q3 is the median of the upper half.
79+
80+
The *inclusive* algorithm also uses the median to divide the ordered dataset into two halves, but if the sample is odd, it includes the median in both halves. Q1 is then the median of the lower half and Q3 the median of the upper half.
81+
82+
```python
83+
import plotly.express as px
84+
85+
df = px.data.tips()
86+
87+
fig = px.box(df, x="day", y="total_bill", color="smoker")
88+
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
89+
fig.show()
90+
```
91+
92+
#### Difference Between Quartile Algorithms
93+
It can sometimes be difficult to see the difference between the linear, inclusive, and exclusive algorithms for computing quartiles. In the following example, the same dataset is visualized using each of the three different quartile computation algorithms.
94+
95+
```python
96+
import plotly.express as px
97+
import pandas as pd
98+
99+
data = [1,2,3,4,5,6,7,8,9]
100+
df = pd.DataFrame(dict(
101+
linear=data,
102+
inclusive=data,
103+
exclusive=data
104+
)).melt(var_name="quartilemethod")
105+
106+
107+
fig = px.box(df, y="value",
108+
facet_col="quartilemethod", boxmode="overlay", color="quartilemethod")
109+
110+
fig.update_traces(quartilemethod="linear", col=1)
111+
fig.update_traces(quartilemethod="inclusive", col=2)
112+
fig.update_traces(quartilemethod="exclusive", col=3)
113+
114+
fig.show()
115+
```
116+
74117
#### Styled box plot
75118

76119
For the interpretation of the notches, see https://en.wikipedia.org/wiki/Box_plot#Variations.
@@ -124,7 +167,7 @@ fig.add_trace(go.Box(x=x1))
124167
fig.show()
125168
```
126169

127-
### Box Plot That Displays the Underlying Data
170+
### Box Plot That Displays The Underlying Data
128171

129172
```python
130173
import plotly.graph_objects as go
@@ -138,6 +181,45 @@ fig = go.Figure(data=[go.Box(y=[0, 1, 1, 2, 3, 5, 8, 13, 21],
138181
fig.show()
139182
```
140183

184+
### Choosing The Algorithm For Computing Quartiles
185+
186+
```python
187+
import plotly.graph_objects as go
188+
189+
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
190+
191+
fig = go.Figure()
192+
fig.add_trace(go.Box(y=data, quartilemethod="linear", name="Linear Quartile Mode"))
193+
fig.add_trace(go.Box(y=data, quartilemethod="inclusive", name="Inclusive Quartile Mode"))
194+
fig.add_trace(go.Box(y=data, quartilemethod="exclusive", name="Exclusive Quartile Mode"))
195+
fig.show()
196+
```
197+
198+
### Box Plot With Precomputed Quartiles
199+
200+
You can specify precomputed quartile attributes rather than using a built-in quartile computation algorithm.
201+
202+
This could be useful if you have already pre-computed those values or if you need to use a different algorithm than the ones provided.
203+
204+
```python
205+
import plotly.graph_objects as go
206+
207+
fig = go.Figure()
208+
209+
fig.add_trace(go.Box(y=[
210+
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
211+
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
212+
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
213+
], name="Precompiled Quartiles"))
214+
215+
fig.update_traces(q1=[ 1, 2, 3 ], median=[ 4, 5, 6 ],
216+
q3=[ 7, 8, 9 ], lowerfence=[-1, 0, 1],
217+
upperfence=[5, 6, 7], mean=[ 2.2, 2.8, 3.2 ],
218+
sd=[ 0.2, 0.4, 0.6 ], notchspan=[ 0.2, 0.4, 0.6 ] )
219+
220+
fig.show()
221+
```
222+
141223
### Colored Box Plot
142224

143225
```python

0 commit comments

Comments
 (0)