Python Plotting
Library Decision
| Use Plotly when |
Use matplotlib when |
| Interactivity is needed (hover, zoom, pan) |
Publication-quality static output (paper, PDF) |
| Output is a web page or notebook |
Fine-grained control over every element |
| Working with Plotly Express (fast, idiomatic) |
Using a seaborn-style statistical chart |
| Building charts for a Dash app |
Complex multi-panel scientific figures |
Quick Start
Plotly Express (most common):
import plotly.express as px
fig = px.scatter(df, x="col_a", y="col_b", color="category",
title="My Chart", template="plotly_white")
fig.show() # interactive in notebook/browser
fig.write_html("chart.html") # shareable standalone file
fig.write_image("chart.png", scale=2) # high-res static (requires kaleido)
matplotlib (static):
import matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams.update({"font.family": "sans-serif", "figure.dpi": 150})
fig, ax = plt.subplots(figsize=(8, 5))
ax.plot(x, y, color="#4C78A8", linewidth=2, label="Series A")
ax.set(title="My Chart", xlabel="X", ylabel="Y")
ax.legend(frameon=False)
ax.spines[["top", "right"]].set_visible(False)
plt.tight_layout()
plt.savefig("chart.png", dpi=300, bbox_inches="tight")
Aesthetics Quick Reference
Plotly templates (set via template=):
"plotly_white" — clean white background (general use)
"plotly_dark" — dark theme (presentations)
"seaborn" — seaborn-inspired style
"ggplot2" — R ggplot2-inspired style
"simple_white" — minimal, publication-friendly
Plotly color sequences (set via color_discrete_sequence=):
px.colors.qualitative.Safe — colorblind-safe categorical
px.colors.qualitative.Plotly — default Plotly palette
px.colors.sequential.Viridis — sequential (maps, heatmaps)
matplotlib style shortcuts:
plt.style.use("seaborn-v0_8-whitegrid") # clean grid style
plt.style.use("bmh") # Bayesian Methods for Hackers style
Plotly Layout Polish
fig.update_layout(
font_family="Inter, Arial, sans-serif",
title_font_size=18,
margin=dict(l=40, r=20, t=60, b=40),
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
plot_bgcolor="white",
paper_bgcolor="white",
)
fig.update_xaxes(showgrid=True, gridcolor="#EEEEEE", zeroline=False)
fig.update_yaxes(showgrid=True, gridcolor="#EEEEEE", zeroline=False)
Hover Templates (Plotly)
fig.update_traces(
hovertemplate="<b>%{x}</b><br>Value: %{y:,.0f}<extra></extra>"
)
Common Chart Patterns
See the reference files for detailed patterns and examples:
- references/plotly-charts.md — Plotly chart recipes: scatter, line, bar, histogram, box, heatmap, subplots, animations
- references/matplotlib-charts.md — matplotlib chart recipes: line, bar, histogram, heatmap, subplots, twin axes, annotations
Output & Sharing
| Goal |
Method |
| Interactive notebook display |
fig.show() |
| Standalone shareable HTML |
fig.write_html("out.html") |
| High-res PNG/SVG/PDF |
fig.write_image("out.png", scale=2) (install kaleido) |
| matplotlib PNG |
plt.savefig("out.png", dpi=300, bbox_inches="tight") |
| matplotlib PDF (vector) |
plt.savefig("out.pdf", bbox_inches="tight") |
Install kaleido for Plotly static export: pip install kaleido
1---2name: py-plotting3description: Creating intuitive and beautiful data visualizations in Python using Plotly and matplotlib. Use when users ask to plot, chart, graph, or visualize data — including scatter plots, line charts, bar charts, histograms, heatmaps, box plots, and more. Applies to both interactive web-ready figures (Plotly) and publication-quality static images (matplotlib).4---56# Python Plotting78## Library Decision910| Use **Plotly** when | Use **matplotlib** when |11|---|---|12| Interactivity is needed (hover, zoom, pan) | Publication-quality static output (paper, PDF) |13| Output is a web page or notebook | Fine-grained control over every element |14| Working with Plotly Express (fast, idiomatic) | Using a seaborn-style statistical chart |15| Building charts for a Dash app | Complex multi-panel scientific figures |1617## Quick Start1819**Plotly Express (most common):**20```python21import plotly.express as px2223fig = px.scatter(df, x="col_a", y="col_b", color="category",24 title="My Chart", template="plotly_white")25fig.show() # interactive in notebook/browser26fig.write_html("chart.html") # shareable standalone file27fig.write_image("chart.png", scale=2) # high-res static (requires kaleido)28```2930**matplotlib (static):**31```python32import matplotlib.pyplot as plt33import matplotlib as mpl3435mpl.rcParams.update({"font.family": "sans-serif", "figure.dpi": 150})3637fig, ax = plt.subplots(figsize=(8, 5))38ax.plot(x, y, color="#4C78A8", linewidth=2, label="Series A")39ax.set(title="My Chart", xlabel="X", ylabel="Y")40ax.legend(frameon=False)41ax.spines[["top", "right"]].set_visible(False)42plt.tight_layout()43plt.savefig("chart.png", dpi=300, bbox_inches="tight")44```4546## Aesthetics Quick Reference4748**Plotly templates** (set via `template=`):49- `"plotly_white"` — clean white background (general use)50- `"plotly_dark"` — dark theme (presentations)51- `"seaborn"` — seaborn-inspired style52- `"ggplot2"` — R ggplot2-inspired style53- `"simple_white"` — minimal, publication-friendly5455**Plotly color sequences** (set via `color_discrete_sequence=`):56- `px.colors.qualitative.Safe` — colorblind-safe categorical57- `px.colors.qualitative.Plotly` — default Plotly palette58- `px.colors.sequential.Viridis` — sequential (maps, heatmaps)5960**matplotlib style shortcuts:**61```python62plt.style.use("seaborn-v0_8-whitegrid") # clean grid style63plt.style.use("bmh") # Bayesian Methods for Hackers style64```6566## Plotly Layout Polish6768```python69fig.update_layout(70 font_family="Inter, Arial, sans-serif",71 title_font_size=18,72 margin=dict(l=40, r=20, t=60, b=40),73 legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),74 plot_bgcolor="white",75 paper_bgcolor="white",76)77fig.update_xaxes(showgrid=True, gridcolor="#EEEEEE", zeroline=False)78fig.update_yaxes(showgrid=True, gridcolor="#EEEEEE", zeroline=False)79```8081## Hover Templates (Plotly)8283```python84fig.update_traces(85 hovertemplate="<b>%{x}</b><br>Value: %{y:,.0f}<extra></extra>"86)87```8889## Common Chart Patterns9091See the reference files for detailed patterns and examples:92- **[references/plotly-charts.md](references/plotly-charts.md)** — Plotly chart recipes: scatter, line, bar, histogram, box, heatmap, subplots, animations93- **[references/matplotlib-charts.md](references/matplotlib-charts.md)** — matplotlib chart recipes: line, bar, histogram, heatmap, subplots, twin axes, annotations9495## Output & Sharing9697| Goal | Method |98|---|---|99| Interactive notebook display | `fig.show()` |100| Standalone shareable HTML | `fig.write_html("out.html")` |101| High-res PNG/SVG/PDF | `fig.write_image("out.png", scale=2)` (install `kaleido`) |102| matplotlib PNG | `plt.savefig("out.png", dpi=300, bbox_inches="tight")` |103| matplotlib PDF (vector) | `plt.savefig("out.pdf", bbox_inches="tight")` |104105Install kaleido for Plotly static export: `pip install kaleido`