Box Plot in Plotly
Last Updated: 08 Nov 2025
Box plots (or box-and-whisker plots) show data distribution through quartiles — perfect for comparing groups, detecting outliers, exam results, or sensor readings.
Hinglish Tip: Box plot = "5-number summary" — min, Q1, median, Q3, max — ek jhalak mein data ka mood pata chal jata hai!
Basic Box Plot
import plotly.express as px
import numpy as np
# Sample exam scores
np.random.seed(42)
scores = np.random.normal(75, 10, 200)
fig = px.box(y=scores, title="Exam Scores - Box Plot")
fig.show()
Key Parameters in px.box()
| Parameter | Values / Examples | Purpose |
|---|---|---|
| data_frame | df | Use Pandas DataFrame |
| x, y | Column names | Group (x), values (y) |
| color | Column name | Color by category |
| points | 'all', 'outliers', False | Show raw data points |
| notched | True | Show confidence interval around median |
| hover_data | ['name'] | Extra info on hover |
| title | String | Chart title |
| labels | 'y': 'Score' | Rename axes |
| orientation | 'v', 'h' | Vertical or horizontal |
Full Example: Compare Classes
import plotly.express as px
import pandas as pd
import numpy as np
# Sample data: 3 classes
np.random.seed(42)
class_a = np.random.normal(70, 12, 100)
class_b = np.random.normal(78, 8, 100)
class_c = np.random.normal(65, 15, 100)
# Add some outliers
class_a = np.append(class_a, [30, 95])
class_c = np.append(class_c, [100])
df = pd.DataFrame({
'Score': np.concatenate([class_a, class_b, class_c]),
'Class': ['A']*102 + ['B']*100 + ['C']*101
})
fig = px.box(
df,
x='Class',
y='Score',
color='Class',
notched=True,
points='outliers', # Only show outliers
title='Exam Scores by Class (Box Plot)',
labels={'Score': 'Marks', 'Class': 'Class'},
hover_data={'Score': ':.1f'}
)
fig.update_layout(
showlegend=False,
font=dict(size=12)
)
fig.show()
Hinglish Tip:
points='outliers' → sirf extreme students dikhao — teacher ko turant pata chalega kon fail/pass!
Horizontal Box Plot (orientation='h')
fig = px.box(
df, y='Class', x='Score',
orientation='h',
color='Class',
title='Horizontal Box Plot (Better for long labels)'
)
fig.show()
Hinglish Tip: Horizontal → class names lambi ho toh overlap nahi hota!
Add Mean & Custom Styling
fig = px.box(df, x='Class', y='Score', color='Class', points='all')
# Add mean as diamond
fig.update_traces(
boxpoints='all',
jitter=0.3,
pointpos=-1.8,
marker=dict(size=5)
)
# Add mean line
fig.update_traces(meanline_visible=True, meanline_color="black", meanline_width=2)
fig.update_layout(title="Box Plot with All Points + Mean Line")
fig.show()
Combine with Violin Plot (Advanced)
import plotly.graph_objects as go
fig = go.Figure()
for cls in df['Class'].unique():
fig.add_trace(go.Violin(
x=df['Class'][df['Class'] == cls],
y=df['Score'][df['Class'] == cls],
name=cls,
box_visible=True,
meanline_visible=True
))
fig.update_layout(title="Violin + Box Plot (Distribution + Summary)")
fig.show()
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| Hide outliers | Use points='outliers' or 'all' |
| No notch → can't compare medians | Use notched=True |
| Too many points → messy | Use points='outliers' only |
| Vertical labels cut off | Use orientation='h' |
| Forget kaleido | pip install -U kaleido |