Matplotlib Plot Markers
During the plotting process, if we want to customize some unique markers for the coordinates, we can use the marker
parameter of the plot() method to define them.
The following example defines a solid circle marker:
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([1, 3, 4, 5, 8, 9, 6, 1, 3, 4, 5, 2, 4])
plt.plot(ypoints, marker='o')
plt.show()
The display result is as follows:
The marker
parameter can define the following symbols:
Marker | Symbol | Description |
---|---|---|
"." | Point | |
"," | Pixel | |
"o" | Solid circle | |
"v" | Triangle down | |
"^" | Triangle up | |
"<" | Triangle left | |
">" | Triangle right | |
"1" | Tri-down | |
"2" | Tri-up | |
"3" | Tri-left | |
"4" | Tri-right | |
"8" | Octagon | |
"s" | Square | |
"p" | Pentagon | |
"P" | Plus (filled) | |
"*" | Star | |
"h" | Hexagon1 | |
"H" | Hexagon2 | |
"+" | Plus | |
"x" | X | |
"X" | X (filled) | |
"D" | Diamond | |
"d" | Thin diamond | |
"|" | Vertical line | |
"_" | Horizontal line | |
0 (TICKLEFT) | Left tick | |
1 (TICKRIGHT) | Right tick | |
2 (TICKUP) | Up tick | |
3 (TICKDOWN) | Down tick | |
4 (CARETLEFT) | Left caret | |
5 (CARETRIGHT) | Right caret | |
6 (CARETUP) | Up caret | |
7 (CARETDOWN) | Down caret | |
8 (CARETLEFTBASE) | Left caret (centered at base) | |
9 (CARETRIGHTBASE) | Right caret (centered at base) | |
10 (CARETUPBASE) | Up caret (centered at base) | |
11 (CARETDOWNBASE) | Down caret (centered at base) | |
"None", " " or "" | No marker | |
'$...$' | Render specified character. For example, "$f$" for letter f as marker. |
The following example defines a *
marker:
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([1, 3, 4, 5, 8, 9, 6, 1, 3, 4, 5, 2, 4])
plt.plot(ypoints, marker='*')
plt.show()
The display result is as follows:
The following example defines a down arrow:
Example
import matplotlib.pyplot as plt
import matplotlib.markers
plt.plot([1, 2, 3], marker=matplotlib.markers.CARETDOWNBASE)
plt.show()
The display result is as follows:
fmt Parameter
The fmt
parameter defines the basic format, such as marker, line style, and color.
fmt = '[marker][line][color]'
For example, o:r
, where o
represents a solid circle marker, :
represents a dotted line, and r
represents red color.
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, 'o:r')
plt.show()
The display result is as follows:
Line types:
Line Type | Description |
---|---|
'-' | Solid line |
':' | Dotted line |
'--' | Dashed line |
'-.' | Dash-dot line |
Color types:
Color | Description |
---|---|
'r' | Red |
'g' | Green |
'b' | Blue |
'c' | Cyan |
'm' | Magenta |
'y' | Yellow |
'k' | Black |
'w' | White |
Marker Size and Color
markersize
, shorthandms
: Defines the size of the marker.markerfacecolor
, shorthandmfc
: Defines the color inside the marker.markeredgecolor
, shorthandmec
: Defines the color of the marker's edge.
Setting the marker size:
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, marker='o', ms=20)
plt.show()
The display result is as follows:
Setting the marker edge color:
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, marker='o', ms=20, mec='r')
plt.show()
The display result is as follows:
Setting the marker face color:
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, marker='o', ms=20, mfc='r')
plt.show()
The display result is as follows:
Customizing the marker face and edge colors:
Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6, 2, 13, 10])
plt.plot(ypoints, marker='o', ms=20, mec='#4CAF50', mfc='#4CAF50')
plt.show()
The display result is as follows: