Matplotlib Marker in Python With Examples and Illustrations

Hello programmers, in today’s article, we will discuss matplotlib markers in python. Matplotlib marker module is a wonderful multi-platform data visualization library in python used to plot 2D arrays and vectors. Matplotlib is designed to work with the broader SciPy stack. The matplotlib markers module in python provides all the functions to handle markers. Both the plot and scatter use the marker functionality.

Matplotlib Marker is a special way of handling markers in Matplotlib graphs. As graphs contain different types of markers and other indicating icons, you can customize them by using marker functions. In this post, we’ll go through all the available markers and ways to use them.

Matplotlib Marker Lists

Before starting with different matplotlib illustrations, here is the list of all possible markers in the matplotlib module along with their symbol and description :

MARKERDESCRIPTION
“.”point
“, “pixel
“o”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
“|”vline
“_”hline
0 (TICKLEFT)tickleft
1 (TICKRIGHT)tickright
2 (TICKUP)tickup
3 (TICKDOWN)tickdown
4 (CARETLEFT)caretleft
5 (CARETRIGHT)caretright
6 (CARETUP)caretup
7 (CARETDOWN)caretdown
8 (CARETLEFTBASE)caretleft (centered at base)
9 (CARETRIGHTBASE)caretright (centered at base)
10 (CARETUPBASE)caretup (centered at base)
11 (CARETDOWNBASE)caretdown (centered at base)
“None”, ” ” or “”nothing
‘$…$’Render the string using mathtext. E.g “$r$” for marker showing the letter r.
vertsA list of (x, y) pairs used for Path vertices. The center of the marker is located at (0, 0) and the size is normalized, such that the created path is encapsulated inside the unit cell.
pathA Path instance
(numsides, style, angle)The marker can also be a tuple (numsides, style, angle), which will create a custom, regular symbol.
A) numsides: the number of sides B) style: the style of the regular symbol,
0: a regular polygon
1: a star-like symbol
2: an asterisk

Illustrations of Matplotlib Marker in Python:

In the following example, we’ll create a custom circular marker on a line graph.

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o')
plt.show()

Output:

Illustrations of Matplotlib Marker in Python

clarification:

In the above code, the keyword argument marker is used to emphasize different points of the plot. The circle marker is used to mark each point in the above plot. matplotlib.pyplot has to be imported at the beginning of all python programs dealing with plots.

Matplotlib Marker Size:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', markersize = 30)
plt.show()

Output:

 Matplotlib Marker size

clarification:

In this specimen, each point is marked using a circle marker. And the keyword argument markersize is used to set the size of the marker to 30.

Matplotlib Marker Edge color:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', markersize = 20, markeredgecolor = 'r')
plt.show()

Output:

Marker Edge color

clarification:

The keyword argument markeredgecolor is used in the above code sample to set the edge color of the circle marker to red.

Matplotlib Marker Color:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r', mfc = 'r')
plt.show()

Output:

Matplotlib Marker Color

clarification:

In this specimen, the circular marker is set to red color. For that, both the keyword argument is set to red color. The markeredgecolor argument can be called as mec for convenience. And the markerfacecolor can be called as mfc. Both mec and mfc are set to red to color the marker.

Matplotlib Empty Circle Marker for Scatter Plot:

import matplotlib.pyplot as plt 
import numpy as np 
x = np.random.randn(60) 
y = np.random.randn(60)
plt.scatter(x, y, s=80, facecolors='none', edgecolors='r')
plt.show()

Output:

Matplotlib Empty Circle Marker for Scatter Plot

clarification Matplotlib Marker:

Empty circle markers of the matplotlib module are used to scatter plot in this tutorial. The facecolors argument is set to none. And the edgecolor argument is set to red to get the empty circle markers. Empty circle markers area is also known as fill none markers. The filling style of the empty circle marker is none, hence known as fill none markers.

Matplotlib Marker Linestyle in Python:

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, 'o:r')
plt.show()

Output:

Matplotlib marker Linestyle in Python

clarification:

The line structure for different line styles in matplotlib markers are:

  • ‘-‘ – solid line
  • ‘:’ – dotted line
  • ‘–‘ – dashed line
  • ‘-.’ – dashed/dotted line.

In the above specimen, ‘o:r’ is used for plotting of red-colored circle-shaped markers with dotted lines. This structure ‘:’ refers to the plotting of the dotted line style in the python program.

Custom Matplotlib Marker in Python:

from matplotlib import pyplot as plt
import numpy as np
import matplotlib

x = np.arange(0.0, 50.0, 2.0)
y = x ** 1.3 + np.random.rand(*x.shape) * 30.0
s = np.random.rand(*x.shape) * 800 + 500

plt.plot(x, y, "ro", alpha=0.5, marker=r'$\clubsuit$', markersize=22)
plt.xlabel("Leprechauns")
plt.ylabel("Gold")
plt.show()

Output:

Custom Matplotlib Markers in Python

clarification:

You can change the point marker type in your line or scatter plot by setting the argument marker equal to the symbol you want to use to identify the plot’s points. In this scatter plot, the marker is customed and is set to clubs.

Must Read

Conclusion: Matplotlib Marker

Matplotlib markers in python have used mark points while line and scatter plots. You have seen how the size, color, and shape of markers can be changed. Markers can also be custom made depending on programmers choice. I hope this article helps you in all aspects related to matplotlib markers in python.

Still have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments