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.
Before starting with different matplotlib illustrations, here is the list of all possible markers in the matplotlib module along with their symbol and description :
|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.|
|verts||A 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.|
|path||A 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
Contents of Tutorial
Illustrations of Matplotlib Marker in Python:
import matplotlib.pyplot as plt import numpy as np ypoints = np.array([3, 8, 1, 10]) plt.plot(ypoints, marker = 'o') plt.show()
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()
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()
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()
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()
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()
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()
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.
- Introduction to Python Super With Examples
- Python Help Function
- Why is Python sys.exit better than other exit functions?
- Python Bitstring: Classes and Other Examples | Module
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.