Matplotlib Marker in Python With Examples and Illustrations

Matplotlib 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

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

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

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

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

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

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.

Matplotlib Marker Alpha

You can set the marker and alpha yourself in matplotlib. Choose a number, in int format. This will imply the number of data points you want on your plot. Post this, set the marker. For example, you can use it as *

marker=’*’

And then set alpha also. You can use a float value.

Alpha decides the transparency of the line of your plot. You need to choose a value between 0 and 1 only. Values close to 0 will be more transparent whereas values like 0.9 or 1 will be more opaque.

Its syntax is:

alpha=0.9

Matplotlib Marker Border

One can set the border specifications of a marker in matplotlib. Some of these are:

s, marker, edgecolor,linewidth, facecolor, etc.

Otherwise, you can simply go for the given syntax:

import matplotlib.pyplot as plt1 

plt1.scatter(10,11,s=800,edgecolors=’black’,linewidths=3)

This will provide a border to the marker in scatter plot. 

Matplotlib Marker Color

To set the marker color,the following keywords are available in matplotlib. These are:

‘r’ for Red, ‘g’ for Green, ‘b’ for Blue , ‘c’ for Cyan, ‘m’ for Magenta, ‘y’ for Yellow, ‘k’ for Black and  ‘w’ for White.

Example: 

plt.plot(mypts, 'o:k')

Will plot all the markers with black color.

Matplotlib Marker Jitter

Matplotlib may not give you results with a largely populated dataset. Jitter is used with seaborn library. It will represent data well.

Example:

sns.stripplot(data =df, x="x dimension", y="y dimension", jitter=0.3, size=2)
plt.title('Jitter Plot', loc='left')

plt.show()

matplotlib x marker thickness

To specify thickness of marker in a scatter plot, use linewidths. It will increase or decrease the width of your marker. 

ax.scatter(x,y,&nbsp; s=200, marker='x',color='g',linewidths=2)

The value of linewidths can be 1,2.. depending on desired thickness. Also, markeredgewidth keyword works with the plot function. 

FAQs

What are the different markers in matplotlib?

Different types of markers exist in matplotlib. Some of them are:
‘o’ for Circle, ‘*’ for Star, ‘.’ For Point , ‘,’ for Pixel, ‘x’ for X , ‘X’ for X (filled), ‘+’ for Plus, ‘P’ for Plus (filled), ‘s’ for Square, ‘D’ for Diamond, ‘d’ for Diamond (thin), ‘p’ for Pentagon, ‘H’ for Hexagon, ‘h’ for Hexagon, ‘v’ for Triangle Down, ‘^’ for Triangle Up, ‘<‘ for Triangle Left, ‘>’ for Triangle Right, ‘|’ for Vline, etc. This will different value to all data points.

How do I change the marker shape in matplotlib?

You can specify the type of marker. It can be star, filled star, triangle, hexagon,etc. The following shapes exist in matplotlib: '.','o','v','^','>','<','s','p','*','h','H','D','d' .

Must Read

Conclusion: Matplotlib Marker

Matplotlib markers in python have used mark points while line and scatter plots. You have seen how markers’ size, color, and shape can be changed. Markers can also be custom-made depending on the programmer’s 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