Matplotlib Ref Boxplot
* * *
[ Matplotlib Reference](https://example.com/matplotlib/matplotlib-apiref.html)
`boxplot()` is used to draw box plots, visually displaying the five-number summary of data: minimum, first quartile, median, third quartile, maximum, and possible outliers.
Box plots are a core tool for exploratory data analysis, suitable for comparing distribution characteristics of multiple groups of data.
## Function Definition
matplotlib.pyplot.boxplot(x, notch=None, sym=None, vert=None, whis=None, positions=None, widths=None, patch_artist=None, bootstrap=None, usermedians=None, conf_intervals=None, meanline=None, showmeans=None, showcaps=None, showbox=None, showfliers=None, boxprops=None, labels=None, flierprops=None, medianprops=None, meanprops=None, capprops=None, whiskerprops=None, manage_ticks=True, autorange=False, zorder=None, capwidths=None, **kwargs)
## Parameter Description
| Parameter | Type | Description |
| --- | --- | --- |
| x | array or array sequence | Input data, 1D array draws one box plot, 2D array sequence draws multiple |
| notch | bool | Whether to draw notched box plot (notch for median confidence interval), default False |
| vert | bool | True=vertical (default), False=horizontal |
| whis | float or (float, float) | Whisker length (multiple of IQR), default 1.5. e.g., (5, 95) represents 5th and 95th percentiles |
| sym | str or None | Marker style for outliers, None means not showing outliers |
| widths | float or array-like | Width of each box |
| patch_artist | bool | If True, box uses fill color (can customize with facecolor) |
| showmeans | bool | Whether to show mean point, default False |
| showfliers | bool | Whether to show outliers, default True |
| labels | list | Labels for each box |
| boxprops / flierprops / medianprops / meanprops | dict | Control appearance properties of box/outliers/median line/mean respectively |
> Box plot structure: box spans from Q1 to Q3, middle line is median. Whiskers extend to the farthest data points within Q1-1.5*IQR to Q3+1.5*IQR range. Values beyond whiskers are outliers.
* * *
## Usage Examples
### Example 1: Basic Box Plot
## Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# Three groups of data with different distributions
data =[
np.random.normal(0,1,100),# standard normal
np.random.normal(2,1.5,100),# mean=2, std=1.5
np.random.normal(-1,0.5,100),# mean=-1, std=0.5
]
fig, ax = plt.subplots(figsize=(7,5), layout='constrained')
bp = ax.boxplot(data, labels=['Group A','Group B','Group C'],
patch_artist=True)
# Custom colors
colors =['#3498db','#e74c3c','#2ecc71']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
ax.set_title('Box Plot: Comparing Three Groups')
ax.set_ylabel('Value')
ax.grid(axis='y', alpha=0.3)
plt.show()
### Example 2: Notched Box Plot + Show Means
## Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
data =[
np.random.normal(0,1,100),
np.random.normal(0,1.2,100),
np.random.normal(0.3,1,100),
]
fig, ax = plt.subplots(figsize=(7,5), layout='constrained')
bp = ax.boxplot(data,
notch=True,# notch (median confidence interval)
showmeans=True,# show means
meanprops=dict(marker='D', markerfacecolor='red',
markersize=8),
patch_artist=True,
labels=['Control','Test A','Test B'])
colors =['#bdc3c7','#3498db','#2ecc71']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
ax.set_title('Notched Box Plot with Means')
ax.set_ylabel('Measurement')
ax.grid(axis='y', alpha=0.3)
plt.show()
### Example 3: Horizontal Box Plot
## Example
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
data =[np.random.exponential(scale=s, size=100)for s in[1,2,3,4]]
labels =['Scale=1','Scale=2','Scale=3','Scale=4']
fig, ax = plt.subplots(figsize=(8,5), layout='constrained')
bp = ax.boxplot(data, labels=labels,
vert=False,# horizontal
patch_artist=True)
colors =['#e74c3c','#f39c12','#2ecc71','#3498db']
for patch, color in zip(bp['boxes'], colors):
patch.set_facecolor(color)
patch.set_alpha(0.7)
ax.set_title('Horizontal Box Plot')
ax.set_xlabel('Value')
ax.grid(axis='x', alpha=0.3)
plt.show()
* * *
## FAQ
### What do the parts of a box plot mean?
Box: IQR from Q1 (25%) to Q3 (75%).
Median line: line in the middle of box = Q2 (50%).
Whiskers: extend to the farthest data points within Q1-1.5*IQR and Q3+1.5*IQR.
Outliers: individual data points outside the whisker range.
[ Matplotlib Reference](https://example.com/matplotlib/matplotlib-apiref.html)
YouTip