Histogram in Plotly
Last Updated: 08 Nov 2025
Histograms show frequency distribution of continuous data — perfect for age groups, exam scores, temperature, or sales ranges.
Hinglish Tip: Histogram = "kitne log kis range mein aate hain" — jaise class mein marks ka bucket!
Basic Histogram
import plotly.express as px
import numpy as np
# Generate sample data
np.random.seed(42)
data = np.random.normal(100, 15, 1000) # Mean=100, Std=15
fig = px.histogram(x=data, title="Exam Scores Distribution")
fig.show()
Key Parameters in px.histogram()
| Parameter | Values / Examples | Purpose |
|---|---|---|
| data_frame | df | Use Pandas DataFrame |
| x | Column name or list | Data to bin |
| nbins | 20, 50 | Number of bins |
| color | Column name | Color by category (stacked) |
| barmode | 'overlay', 'stack', 'relative' | How multiple groups display |
| histnorm | '', 'percent', 'probability', 'density' | Normalize: count, %, or density |
| opacity | 0.7 | Transparency (for overlay) |
| text_auto | True | Show count on top of bars |
| title, labels | String, dict | Title & axis labels |
Full Example: Student Scores by Class (Overlay + Density)
import plotly.express as px
import pandas as pd
import numpy as np
# Sample data: Two classes
np.random.seed(42)
class_a = np.random.normal(75, 10, 500)
class_b = np.random.normal(82, 12, 500)
df = pd.DataFrame({
'Score': np.concatenate([class_a, class_b]),
'Class': ['A']*500 + ['B']*500
})
# Overlay Histogram
fig1 = px.histogram(
df, x='Score', color='Class',
nbins=30,
barmode='overlay',
opacity=0.7,
title='Score Distribution: Class A vs B (Overlay)',
labels={'Score': 'Marks', 'count': 'No. of Students'},
histnorm='',
text_auto=True
)
fig1.update_layout(barmode='overlay')
fig1.show()
# Density (Probability)
fig2 = px.histogram(
df, x='Score', color='Class',
nbins=30,
barmode='overlay',
opacity=0.8,
title='Score Distribution (Density Curve Style)',
histnorm='probability density',
marginal='rug' # Shows individual points below
)
fig2.show()
Hinglish Tip: histnorm='probability density' → area under curve = 1 — jaise normal distribution graph!
Stacked Histogram (Total + Breakdown)
fig = px.histogram(
df, x='Score', color='Class',
nbins=25,
barmode='stack',
title='Total Students by Score Range',
text_auto=True
)
fig.update_traces(textposition='inside')
fig.show()
Horizontal Histogram (orientation='h')
fig = px.histogram(
df, y='Score', color='Class',
orientation='h',
nbins=20,
title='Horizontal Histogram (Good for long labels)'
)
fig.show()
Hinglish Tip: Horizontal → bin labels overlap nahi hote — clean for reports!
Add Mean Line & Stats
fig = px.histogram(df, x='Score', color='Class', nbins=30, barmode='overlay', opacity=0.7)
# Add mean lines
mean_a = class_a.mean()
mean_b = class_b.mean()
fig.add_vline(x=mean_a, line_dash="dash", line_color="blue", annotation_text=f"Class A Mean: {mean_a:.1f}")
fig.add_vline(x=mean_b, line_dash="dash", line_color="red", annotation_text=f"Class B Mean: {mean_b:.1f}")
fig.update_layout(title="Histogram with Mean Lines")
fig.show()
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| Wrong bin size → misleading | Test nbins=20, 30, 50 → pick best |
| Overlay bars hide each other | Use opacity=0.6 + barmode='overlay' |
| No normalization | Use histnorm='percent' for fair comparison |
| Too many bins → noisy | Start with nbins=30 |
| Forget to install kaleido | pip install -U kaleido |