Matplotlib Markers
Highlight data points with customizable markers, colors, and annotations to surface key insights.
Marker Basics
Add markers by passing the marker argument to plot().
ax.plot(x, y, marker="o", markersize=6, markerfacecolor="white", markeredgecolor="navy")Combine markers with line styles to differentiate series.
Marker Catalogue
Common options include:
"o"— circle"s"— square"^"— triangle up"D"— diamond"x","+"— cross-hair markers"*"— star
Per-Point Styling
Use scatter() or repeated plot() calls to vary marker properties per point.
ax.scatter(x, y, c=y, cmap="viridis", s=60)The s parameter controls marker area, while c maps colors to values.
Annotations
Pair markers with annotations to label outliers or thresholds.
ax.annotate(
"Breakpoint",
xy=(x0, y0),
xytext=(x0 + 0.5, y0 + 0.5),
arrowprops=dict(arrowstyle="->", color="crimson")
)Legend Integration
Markers appear in legends when you provide labels. Customize legend markers by passing markerfirst, handlelength, and scatterpoints options.
Next Steps
Advance to the line chapter to control line styles, colors, and advanced rendering options.
Common Marker & Format Shortcuts
| Code | Meaning |
|---|---|
"o" "s" "^" "*" | circle, square, triangle, star |
"--" ":" "-." | dashed, dotted, dash-dot line |
"r" "g" "b" | red, green, blue |
"o:r" | circles + dotted + red (combined fmt) |
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 1, 5], marker="o", linestyle="--", color="green",
markersize=10)
plt.show()Try It Yourself
Exercise: Plot three points with red star markers and no connecting line.
Show solution
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [2, 5, 3], "r*", linestyle="none", markersize=14)
plt.show()Key Takeaways
- Set
marker,linestyle, andcolorto style a line. - Combine them in a short format string like
"o:r".
📘 Real-World Deep Dive
Knowing <strong>Matplotlib Markers (Matplotlib)</strong> well is what turns Matplotlib from a curiosity into a daily tool — you'll reach for it in nearly every real project.
Real-Life Scenario
An end-to-end usage of Matplotlib Markers that you'd actually see in a data pipeline or analytics notebook.
Real-Life Example
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9], "o-", linewidth=2, markersize=10)
plt.show()Expected Output
(no output)Common mistakes
- Calling
plt.plotwithoutplt.show()in scripts (orplt.savefig) produces no visible output. - Passing mismatched array shapes to
plt.plot(x, y)raises a uselessValueError— verify shapes withx.shapeandy.shapefirst. - Mixing
numpy.float64withpandas.Seriesis OK, but mixingnumpy.datetime64with Pythondatetime.dateneeds an explicit cast. - Treating Matplotlib Markers as a black box without reading the docs — the API has subtle defaults that bite when you scale.
🚀 Performance & Best Practices
- For large scatter plots use
plt.scatter(..., s=1, rasterised=True)or switch todatashader. - Use the
aggbackend on web services:import matplotlib; matplotlib.use("Agg"). - Plot once and reuse the figure: avoid embedding Matplotlib into tight Python loops without an explicit
clear(). - When working with Matplotlib, prefer vectorised / batched operations over Python loops.
🧪 Try It Yourself
- Reproduce the snippet on a representative slice of your own data.
- Profile the snippet with
cProfileortimeitand find the single biggest improvement. - Generalise the snippet into a small, reusable function you can drop into future projects.