Matplotlib Markers
A marker is the symbol drawn at each data point. Markers make individual measurements visible, which matters when you have few points or when the gaps between them mean something.
The marker argument
Instead of a format string you can use the marker keyword. It is more readable and combines freely with other options:
import matplotlib.pyplot as plt
week = [1, 2, 3, 4, 5, 6]
steps = [6200, 8100, 7400, 9900, 5600, 11200]
plt.plot(week, steps, marker="o")
plt.title("Daily steps")
plt.show()
print("Average:", round(sum(steps) / len(steps)), "steps")Average: 8067 steps
Common marker symbols
| Symbol | Shape | Symbol | Shape |
|---|---|---|---|
"o" |
circle | "s" |
square |
"." |
point | "D" / "d" |
diamond / thin diamond |
"," |
pixel | "p" |
pentagon |
"^" "v" "<" ">" |
triangles | "h" / "H" |
hexagons |
"*" |
star | "x" / "X" |
x / filled x |
"+" / "P" |
plus / filled plus | "|" / "_" |
vertical / horizontal line |
The next example draws one row per marker so you can compare them:
import matplotlib.pyplot as plt
symbols = ["o", "s", "^", "v", "D", "p", "*", "h", "x", "+", "P", "X"]
plt.figure(figsize=(7, 4))
for row, m in enumerate(symbols):
plt.plot([1, 2, 3, 4, 5], [row] * 5, marker=m, linestyle="None", markersize=10)
plt.yticks(range(len(symbols)), symbols)
plt.xlim(0.5, 5.5)
plt.title("Marker gallery")
plt.show()
print(len(symbols), "marker styles shown")12 marker styles shown
plt.yticks(positions, labels) puts each marker's symbol string next to its row.
Size and colour
Markers have their own size and colour settings, separate from the line:
| Keyword | Short form | Meaning |
|---|---|---|
markersize |
ms |
Size in points |
markerfacecolor |
mfc |
Fill colour |
markeredgecolor |
mec |
Outline colour |
markeredgewidth |
mew |
Outline width |
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 3, 5, 4, 6]
plt.plot(x, y, marker="o", ms=14, mfc="gold", mec="darkred", mew=2, color="gray")
plt.title("Styled markers")
plt.show()
line = plt.gca().lines[0]
print("size:", line.get_markersize())
print("face:", line.get_markerfacecolor(), "| edge:", line.get_markeredgecolor())size: 14.0
face: gold | edge: darkred
Colours can be names ("gold"), single letters ("r") or hex codes ("#1f77b4"). The Colors & Colormaps lesson explains all the options.
Hollow markers and marking only some points
Set the face colour to "none" for hollow markers. With many points, markevery draws a marker only on every n-th point so the line stays readable:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 10, 101)
plt.plot(x, np.sin(x), marker="o", mfc="none", markevery=10, label="every 10th")
plt.plot(x, np.cos(x), marker="s", markevery=[0, 50, 100], label="3 chosen points")
plt.legend()
plt.show()
print("Points per line:", x.size)
print("Markers on the first line:", len(range(0, x.size, 10)))Points per line: 101
Markers on the first line: 11
Tip: For scatter-style data where each point should have its own size or colour, use
plt.scatter()instead (see the Scatter lesson).plot()uses one marker style for the whole line.
Test yourself
Which keyword makes markers larger?
- markerwidth
- size
- markersize (or ms)
- linewidth
How do you draw hollow circle markers?
- marker="O"
- marker="o", markerfacecolor="none"
- marker="o", hollow=True
- marker="circle"
What does markevery=5 do?
- Draws 5 markers in total
- Draws a marker on every 5th data point
- Makes every marker 5 points big
- Repeats the line 5 times