Matplotlib Markers
The markers module in Matplotlib helps highlight individual data points on plots, improving readability and aesthetics. With various marker styles, users can customize plots to distinguish data series and emphasize key points, enhancing the effectiveness of visualizations.
To illustrate this concept, consider the following simple example where we plot a line graph with circular markers:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='o', linestyle='-', label='Data Points')
plt.title('Simple Plot with Markers')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.show()
Output:

Simple Line plot with Markets
In this example, each point on the line is marked with a circle (‘o’), making it easy to identify individual data points.
Matplotlib Markers Module in Python
The Matplotlib.markers module provides functions to handle markers in Matplotlib. Each marker has a specific character or symbol associated with it that represents how the data point will appear on the graph. This flexibility allows users to customize their plots according to their preferences or requirements
Below is the table defining most commonly used markers in Matplotlib:
Marker | Symbol | Description |
---|---|---|
“.” |
• | point |
“o” |
⬤ | circle |
“v” |
▼ | triangle_down |
“^” |
▲ | triangle_up |
“s” |
■ | square |
“p” |
⬟ | pentagon |
“*” |
★ | star |
“8” |
⬢ | octagon |
Let’s mark each point by star symbol:
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, marker='*', linestyle='--', color='r', label='Data Points')
plt.title('Modified Plot with Markers')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()
plt.grid(True)
plt.show()
Output:

Simple plot with * marker
Visualizing Multiple Marker Shapes
This code generates a plot showcasing different Matplotlib markers. It iterates through a list of marker styles and displays them on the same x-axis, with each marker positioned along a horizontal line at different y-values.
import matplotlib.pyplot as plt
x = range(1, 11)
markers = ['o', 's', '^', 'v', 'D', '*', '+', 'x']
for i, marker in enumerate(markers):
plt.plot(x, [i*2]*10, marker=marker, linestyle='')
plt.title('Different Matplotlib Markers')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output:

Visualising Multiple markers
Using fmt Parameter in Matplotlib
The syntax for using the fmt parameter in Matplotlib is actually a combination of marker, line style, and color, where the format string follows this structure:
Syntax: fmt = ‘marker|line|color’
- Marker: Specifies the style of the points, e.g.,
'o'
for circles,'s'
for squares, etc.- Line: Specifies the style of the line, e.g.,
'-'
for a solid line,':'
for a dotted line, etc.- Color: Specifies the color of the plot elements, e.g.,
'r'
for red,'b'
for blue, etc.
Let’s have a look at the example:
import matplotlib.pyplot as plt
x = [4,1,7,5,8]
plt.plot(x,'o-r') # Red circles with a solid line
plt.title('Plot with fmt')
plt.xlabel('X-axis')
plt.show()
Output:

using fmt parameter
Line and color reference
The line and color reference in Matplotlib allows you to customise the appearance of plot lines and markers. You can quickly set the line style and color to enhance visual clarity and aesthetics.
Line Style Reference
Line Style | Symbol |
---|---|
Solid line |
‘-‘ |
Dashed line |
‘┈’ |
Dash-dot line |
‘-.’ |
Dotted line |
‘:’ |
Long dashed line |
‘–‘ |
Custom dash | ‘dotted’ |
Visualizing line style reference
import matplotlib.pyplot as plt
x = [1, 3, 2, 9, 8]
plt.plot(x,'--')
plt.title('Line Style')
plt.xlabel('X-axis')
plt.show()
Output:

Line style reference
Color Reference
Color | Symbol |
---|---|
Red | ‘r’ |
Green | ‘g’ |
Blue | ‘b’ |
Cyan | ‘c’ |
Magenta | ‘m’ |
Black | ‘k’ |
Yellow | ‘y’ |
Orange | ‘orange’ |
Purple | ‘purple’ |
White | ‘w’ |
Visualizing color style reference
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y, 'ro')
plt.title('Color Example')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.show()
Output:

Color Style
Marker Size
You can use the keyword argument markersize
or the shorter version, ms
to set the size of the markers:
Visualizing markers with increased size
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([4, 9, 1, 10])
plt.plot(ypoints,color="hotpink", marker = '*', ms = 20)
plt.show()
Output:

Marker size increased