Skip to content

Commit b83aaef

Browse files
author
Joseph Damiba
authored
Merge pull request #2063 from plotly/update-box-plot-with-quartiles
Update box plots doc with precomputed quartiles and quartilemethod
2 parents 135cedf + dbffa39 commit b83aaef

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

doc/python/box-plots.md

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,51 @@ 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 the `linear` method (for more about linear interpolation, see #10 listed on [http://www.amstat.org/publications/jse/v14n3/langford.html](http://www.amstat.org/publications/jse/v14n3/langford.html) and [https://en.wikipedia.org/wiki/Quartile](https://en.wikipedia.org/wiki/Quartile) for more details).
77+
78+
However, you can also choose to use an `exclusive` or an `inclusive` algorithm to compute quartiles.
79+
80+
The *exclusive* algorithm uses the median to divide the ordered dataset into two halves. If the sample is odd, it does not include the median in either half. Q1 is then the median of the lower half and Q3 is the median of the upper half.
81+
82+
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.
83+
84+
```python
85+
import plotly.express as px
86+
87+
df = px.data.tips()
88+
89+
fig = px.box(df, x="day", y="total_bill", color="smoker")
90+
fig.update_traces(quartilemethod="exclusive") # or "inclusive", or "linear" by default
91+
fig.show()
92+
```
93+
94+
#### Difference Between Quartile Algorithms
95+
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.
96+
97+
```python
98+
import plotly.express as px
99+
import pandas as pd
100+
101+
data = [1,2,3,4,5,6,7,8,9]
102+
df = pd.DataFrame(dict(
103+
linear=data,
104+
inclusive=data,
105+
exclusive=data
106+
)).melt(var_name="quartilemethod")
107+
108+
109+
fig = px.box(df, y="value",
110+
facet_col="quartilemethod", boxmode="overlay", color="quartilemethod")
111+
112+
fig.update_traces(quartilemethod="linear", col=1)
113+
fig.update_traces(quartilemethod="inclusive", col=2)
114+
fig.update_traces(quartilemethod="exclusive", col=3)
115+
116+
fig.show()
117+
```
118+
74119
#### Styled box plot
75120

76121
For the interpretation of the notches, see https://en.wikipedia.org/wiki/Box_plot#Variations.
@@ -124,7 +169,7 @@ fig.add_trace(go.Box(x=x1))
124169
fig.show()
125170
```
126171

127-
### Box Plot That Displays the Underlying Data
172+
### Box Plot That Displays The Underlying Data
128173

129174
```python
130175
import plotly.graph_objects as go
@@ -138,6 +183,47 @@ fig = go.Figure(data=[go.Box(y=[0, 1, 1, 2, 3, 5, 8, 13, 21],
138183
fig.show()
139184
```
140185

186+
### Modifying The Algorithm For Computing Quartiles
187+
188+
For an explanation of how each algorithm works, see [Choosing The Algorithm For Computing Quartiles](#choosing-the-algorithm-for-computing-quartiles).
189+
190+
```python
191+
import plotly.graph_objects as go
192+
193+
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
194+
195+
fig = go.Figure()
196+
fig.add_trace(go.Box(y=data, quartilemethod="linear", name="Linear Quartile Mode"))
197+
fig.add_trace(go.Box(y=data, quartilemethod="inclusive", name="Inclusive Quartile Mode"))
198+
fig.add_trace(go.Box(y=data, quartilemethod="exclusive", name="Exclusive Quartile Mode"))
199+
fig.show()
200+
```
201+
202+
### Box Plot With Precomputed Quartiles
203+
204+
You can specify precomputed quartile attributes rather than using a built-in quartile computation algorithm.
205+
206+
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.
207+
208+
```python
209+
import plotly.graph_objects as go
210+
211+
fig = go.Figure()
212+
213+
fig.add_trace(go.Box(y=[
214+
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
215+
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
216+
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
217+
], name="Precompiled Quartiles"))
218+
219+
fig.update_traces(q1=[ 1, 2, 3 ], median=[ 4, 5, 6 ],
220+
q3=[ 7, 8, 9 ], lowerfence=[-1, 0, 1],
221+
upperfence=[5, 6, 7], mean=[ 2.2, 2.8, 3.2 ],
222+
sd=[ 0.2, 0.4, 0.6 ], notchspan=[ 0.2, 0.4, 0.6 ] )
223+
224+
fig.show()
225+
```
226+
141227
### Colored Box Plot
142228

143229
```python

0 commit comments

Comments
 (0)