Heatmap in Plotly
Last Updated: 08 Nov 2025
Heatmaps show data density or intensity using color gradients — perfect for correlation matrices, time vs category sales, website clicks, or sensor grid data.
Hinglish Tip: Heatmap = "jahan zyada value, wahan garm color" — jaise weather map! Dark red = high, blue = low.
Basic Heatmap
import plotly.express as px
import numpy as np
# Sample 5x5 matrix
data = np.random.rand(5, 5)
fig = px.imshow(data, title="Random Heatmap")
fig.show()
Key Parameters in px.imshow() (Heatmap)
| Parameter | Values / Examples | Purpose |
|---|---|---|
| text_auto | True, '.2f' | Show values on cells |
| color_continuous_scale | 'Viridis', 'Reds', 'Blues' | Color gradient |
| zmin, zmax | 0, 1 | Fix color scale range |
| labels | 'x': 'Day', 'y': 'Hour' | Axis labels |
| x, y | List of labels | Row/column names |
| aspect | 'auto', 'equal' | Cell shape |
| title | String | Chart title |
| color_continuous_midpoint | 0 | For diverging scales |
Full Example: Website Traffic (Hour vs Day)
import plotly.express as px
import pandas as pd
import numpy as np
# Generate sample traffic data
np.random.seed(42)
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
hours = [f'{h:02d}:00' for h in range(24)]
traffic = np.random.randint(50, 500, size=(len(hours), len(days)))
df = pd.DataFrame(traffic, index=hours, columns=days)
fig = px.imshow(
df,
text_auto=True,
color_continuous_scale='YlOrRd', # Yellow to Red
title='Website Traffic: Hour vs Day',
labels=dict(x='Day', y='Hour', color='Visitors'),
zmin=0,
zmax=500,
aspect='auto'
)
fig.update_layout(
font=dict(size=10),
coloraxis_colorbar=dict(
title="Visitors",
tickvals=[0, 250, 500],
ticktext=['Low', 'Medium', 'High']
)
)
fig.show()
Hinglish Tip: text_auto=True → har cell mein number dikhega — no guesswork!
Correlation Heatmap
import plotly.express as px
import pandas as pd
import seaborn as sns
# Sample dataset
df = sns.load_dataset('iris').select_dtypes('number')
corr = df.corr()
fig = px.imshow(
corr,
text_auto='.2f',
color_continuous_scale='RdBu_r', # Red-Blue diverging
color_continuous_midpoint=0,
title='Iris Dataset: Feature Correlation',
labels=dict(color='Correlation'),
zmin=-1, zmax=1
)
fig.update_layout(
xaxis_title="", yaxis_title="",
coloraxis_colorbar=dict(
title="Corr",
tickvals=[-1, -0.5, 0, 0.5, 1],
ticktext=['-1', '-0.5', '0', '0.5', '+1']
)
)
fig.show()
Hinglish Tip: RdBu_r → red = negative, blue = positive — ML mein correlation samajhne ke liye best!
Annotated Heatmap with Custom Labels
fig = px.imshow(
df.head(10), # First 10 hours
text_auto='.0f',
color_continuous_scale='Greens',
title='Top 10 Hours Traffic',
labels=dict(color='Users')
)
fig.update_xaxes(side="top") # Labels on top
fig.show()
Common Mistakes to Avoid
| Mistake | Fix |
|---|---|
| No text on cells | Use text_auto=True or '.1f' |
| Color scale auto → misleading | Fix with zmin, zmax |
| Too many rows/cols → unreadable | Filter top N or use df.head(20) |
| Wrong color scheme | Use Viridis (sequential), RdBu_r (diverging) |
| Can't export | Install kaleido |