Understanding Matplotlib Markers

Matplotlib Markers are the symbols that are used to represent the data points in a plot.

The purpose of this article is to describe Matplotlib markers in depth, including their types, features, and functionality with examples.



Matplotlib Markers Options

Matplotlib provides a wide range of basic marker options that can be used to represent data points in a plot.

Some of the basic marker options available in Matplotlib are:

  • Circle: ‘o
  • Star: ‘*
  • Diamond: ‘d
  • Triangle: ‘^

With the keyword argument marker, you can highlight each point with a particular marker:

In the example below, display all points with a circle as follows:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, marker = 'o') pt.show()

Set marker = ‘o‘ to show the coordinates of the following diagram:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, marker = 'o') pt.show()

In the example below, show all points with a star as follows:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, marker = '*') pt.show()

Assign marker = ‘*‘ to display the coordinates of the following diagram:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, marker = '*') pt.show()
Note: We can change the marker to any of the other basic marker options by simply changing the marker argument.

Here are some Matplotlib markers with examples you can select from:

MarkersOverviewExamples
‘o’Circle Execute
‘*’Star Execute
‘.’Point Execute
‘,’Pixel Execute
‘x’X Execute
‘X’X (filled) Execute
‘+’Plus Execute
‘P’Plus (filled) Execute
‘s’Square Execute
‘D’Diamond Execute
‘d’Diamond (thin) Execute
‘p’Pentagon Execute
‘H’Hexagon Execute
‘h’Hexagon Execute
‘v’Triangle Down Execute
‘^’Triangle Up Execute
‘<‘Triangle Left Execute
‘>’Triangle Right Execute
‘1’Tri Down Execute
‘2’Tri Up Execute
‘3’Tri Left Execute
‘4’Tri Right Execute
‘|’Vline Execute
‘_’Hline Execute

Format Strings fmt

It is also possible to indicate the marker through the shortcut string notation parameter as well.

This parameter is also referred to as fmt, and is represented as follows:

marker|line|color

Display the following diagram with dotted line in blue and also apply the ‘P‘ marker:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, 'P:b') pt.show()

Utlize the ‘>‘ marker and green color in the below example:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, '>:g') pt.show()

The marker type can be selected from any of the markers listed in the Marker Reference section.

There are several possible values for the line:

Line SyntaxOverviewExamples
‘-‘Solid line Execute
‘:’Dotted line Execute
‘–‘Dashed line Execute
‘-.’Dashed/dotted line Execute
Reminder: You will not be able to plot any lines if you do not include the line value in the fmt parameter.

You can select a color from the following short color names:

Color Reference

Color SyntaxOverviewExamples
‘r’Red Execute
‘g’Green Execute
‘b’Blue Execute
‘c’Cyan Execute
‘m’Magenta Execute
‘y’Yellow Execute
‘k’Black Execute
‘w’White Execute

Marker Size

For setting the marker size, you can provide the keyword argument markersize or the simpler alternative, ms:

Implement the ms argument in the following example:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, marker = 'o', ms = 15) pt.show()

Apply the fmt parameter with ms = 20:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, '4-.m', ms = 25) pt.show()

Marker Color

To modify the color of the edge of the markers, you can either provide the keyword argument markeredgecolor or the simpler mec parameter:

Utilize the mec argument in the following example:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, marker = 'o', ms = 15, mec = 'y', mew = 4) pt.show()

First set the mec = ‘b’, then assign marker edge width (mec) = 6:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, '4-.m', ms = 25, mec = 'b', mew = 6) pt.show()

If you want to change the color within the edge of the markers, you can invoke the keyword argument markerfacecolor or the more concise mfc.

Assign face color to the following diagram marker as follows:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, marker = 'o', ms = 15, mec = 'y', mew = 4, mfc = 'r') pt.show()

Insert the magenta color inside the edge of the below diagram marker:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, 'h-.y', ms = 25, mec = 'b', mew = 6, mfc = 'm') pt.show()

To color the entire marker, utilize each of the mec and mfc arguments:

Modify the marker face and edge color to yellow:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, marker = 'P', ms = 15, mec = 'y', mfc = 'y') pt.show()

First assign the marker face and edge color to black, then increase the width of the line:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, 'h-.b', ms = 25, mec = 'k', mfc = 'k', lw = 4) pt.show()

In addition to Hexadecimal color values, you can also utilize:

In the following example, implement the customized hexadecimal color:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([56, 33, 22, 47, 84, 215, 77]) pt.plot(y_plane, 'P–k', ms = 15, mec = '#004080', mew = 4, mfc = '#cce6ff') pt.show()

Apply these two colors ‘#ffcc00‘ and ‘#ff1ac6‘ in the below diagram:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, 'h-.m', ms = 25, mec = '#ffcc00', mew = 6, mfc = '#ff1ac6') pt.show()

You can also utlize 140 supported color names which defined in matplotlib markers document.

Put a mark on each point with a color called “GreenYellow” and “Black”:

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, 'h-.k', ms = 25, mec = 'GreenYellow', mew = 6, mfc = 'Black') pt.show()

Apply ‘PowderBlue’ color to marker edge and ‘Gray’ to marker face:s

Example: 

import matplotlib.pyplot as pt import numpy as npy y_plane = npy.array([265, 33, 63, 47, 10, 265, 77, 13]) pt.plot(y_plane, '*–b', ms = 30, mec = 'PowderBlue', mew = 4, mfc = 'Gray') pt.show()

Example Explanation

The y_plane array is created using NumPy to store some example data.

Then, pt.plot() function is used to create a line plot of the y_plane array. The ‘*–b‘ argument specifies the marker style and color of the plot, where ‘*‘ indicates the marker style as a star, — indicates the line style as dashed, and ‘b‘ indicates the color as blue.

  • The ms parameter sets the size of the marker to 30.
  • mec parameter sets the marker edge color to ‘PowderBlue’.
  • mew parameter sets the width of the marker edge to 4.
  • mfc parameter sets the marker face color to ‘Gray’.

Finally, the pt.show() function is called to display the plot.


Matplotlib Markers Benefits

Matplotlib markers offer various benefits for effective data visualization.

  • Markers help to distinguish data points in a plot, making data more readable and understandable.
  • Matplotlib markers are customizable and allow users to choose shape, size, color, edge color, and line style, enabling them to tailor markers to specific needs.
  • The versatility of Matplotlib markers makes them suitable for various plot types such as scatter plots, line plots, and bar plots, which can lead to diverse visualizations with consistent data point representation.
  • Matplotlib markers are flexible and can be used with other plotting functions and tools to create more complex visualizations.
  • Matplotlib markers ensure compatibility with other Matplotlib functions and tools, which simplifies integrating markers into existing code and workflows.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *