codingstreets
Search
Close this search box.

Introduction to Python Matplotlib Markers

In This Article, You Will Learn About Python Matplotlib Markers.

Python Matplotlib Pyplot – Before moving ahead, let’s take a look at Python Matplotlib Pyplot & Plotting

Table of Contents

Example: Marking each point with square.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])

plt.plot(y_axis, marker = 's')
plt.show()

				
			
python-matplotlib-pyplot

As shown above, it returned a graph after setting each point’s shape as Square.

Marker Reference

You can choose any of these markers:

 

Marker

Description

‘o’

Circle

‘*’

Star

‘.’

Point

‘,’

Pixel

‘x’

X

‘X’

X (filled)

‘+’

Plus

‘P’

Plus (filled)

‘s’

Square

‘D’

Diamond

‘d’

Diamond (thin)

‘p’

Pentagon

‘H’

Hexagon

‘h’

Hexagon

‘v’

Triangle Down

‘^’

Triangle Up

‘<‘

Triangle Left

‘>’

Triangle Right

‘1’

Tri Down

‘2’

Tri Up

‘3’

Tri Left

‘4’

Tri Right

‘|’

Vline

‘_’

Hline

Let’s take a look at another example –

Example: Marking each point with Triangle Up.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])

plt.plot(y_axis, marker = '^')
plt.show()

				
			
python-matplotlib-pyplot

As has been noted, it returned a graph after setting each point’s shape as Triangle Up.

Format Strings fmt

There is another parameter that you can use to specify the marker i.e., fmt.

Syntax: marker|line|color

Example: Marking each point with Triangle Up.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])

plt.plot(y_axis,'^--g')
plt.show()

				
			
python-matplotlib-pyplot

As can be seen, it returned a graph after setting each point’s shape as Triangle Up with a dotted line.

Note: The marker value can be anyone from Marker Reference Table.

The Line value can be anyone from the table given below.

Line Reference

Line Syntax

Description

‘-‘

Solid line

‘:’

Dotted line

‘–‘

Dashed line

‘-.’

Dashed/dotted line

 

Note: If line value is not provided in the parameter, then no line will be plotted.

The color value can be anyone from the table given below.

Color Reference

Color Syntax

Description

‘r’

Red

‘g’

Green

‘b’

Blue

‘c’

Cyan

‘m’

Magenta

‘y’

Yellow

‘k’

Black

‘w’

White

Marker Size

The keyword argument markersize or ms (short version) allows us to set the size of points.   

Example: Set the markers size 12.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])

plt.plot(y_axis, marker = 's', markersize = 12)
plt.show()

				
			
python-matplotlib-pyplot

As can be seen, it returned a graph after setting each point’s size as 12.

Marker Color

The keyword argument markeredgecolor or mec (short version) allows us to set the color of the edge of the points.

Example: Set the edge colour of each point as green.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])
plt.plot(y_axis, marker = 's', markersize = 12, markeredgecolor = 'g')

plt.show()

				
			
python-matplotlib-pyplot

As a result, it returned a graph after setting each point’s edge as green.

To set the color inside the edge of the points, we can use keyword argument markerfacecolor or mfc (short version).

Example: Set the color inside the edge of each point as green.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])
plt.plot(y_axis, marker = 's', ms = 12, mfc = 'g')

plt.show()

				
			

As shown above, it returned a graph after setting each point’s inside as green.

Now, use both keyword arguments mec and mfc together to set the color of all points.

Example: Set the color of all points entirely as green.

				
					import matplotlib.pyplot as plt
import numpy as np

y_axis = np.array([5, 6, 7, 8])
plt.plot(y_axis, marker = 's', ms = 12, mec = 'g', mfc = 'g')

plt.show()

				
			

As shown above, it returned a graph after setting each point entirely as green.

If you find anything incorrect in the above-discussed topic and have any further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on