Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Matplotlib plot markers

Matplotlib plot markers

During plotting, if we want to customize some different markers for the coordinates, we can use the plot() marker parameter of the method to define them.

The following example defines a solid circle marker:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

1

,

3

,

4

,

5

,

8

,

9

,

6

,

1

,

3

,

4

,

5

,

2

,

4

]

)

plt.

plot

(

ypoints

,

marker

=

'o'

)

plt.

show

(

)

The display result is as follows:

The symbols that marker can define are as follows:

Markup sign before positive numbers Description
"." m00 Point
"," m01 Pixel point
"o" m02 Solid circle
"v" m03 Triangle down
"^" m04 Triangle up
"<" m05 Triangle left
">" m06 Triangle right
"1" m07 Tri-down
"2" m08 Tri-up
"3" m09 Tri-left
"4" m10 Tri-right
"8" m11 Octagon
"s" m12 Square
"p" m13 Pentagon
"P" m23 Plus (filled)
"*" m14 Asterisk
"h" m15 Hexagon 1
"H" m16 Hexagon 2
"+" m17 Plus sign
"x" m18 Multiplication x
"X" m24 Multiplication x (filled)
"D" m19 Diamond
"d" m20 Thin diamond
"|" m21 Vertical line
"_" m22 Horizontal line
0 (TICKLEFT) m25 Left horizontal line
1 (TICKRIGHT) m26 Right horizontal line
2 (TICKUP) m27 Up vertical line
3 (TICKDOWN) m28 Down vertical line
4 (CARETLEFT) m29 Left arrow
5 (CARETRIGHT) m30 Right arrow
6 (CARETUP) m31 Up arrow
7 (CARETDOWN) m32 Arrow down
8 (CARETLEFTBASE) m33 Arrow left (centered on the midpoint)
9 (CARETRIGHTBASE) m34 Arrow right (centered on the midpoint)
10 (CARETUPBASE) m35 Arrow up (centered on the midpoint)
11 (CARETDOWNBASE) m36 Arrow down (centered on the midpoint)
"None", " " or ""   No marker
'$...$' m37 Render the specified character. For example, "$f$" uses the letter f as the marker.

The following example defines the * marker:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

1

,

3

,

4

,

5

,

8

,

9

,

6

,

1

,

3

,

4

,

5

,

2

,

4

]

)

plt.

plot

(

ypoints

,

marker

=

'*'

)

plt.

show

(

)

The display result is as follows:

The following example defines a down arrow:

Example

import

matplotlib.

pyplot

as

plt

import

matplotlib.

markers

plt.

plot

(

[

1

,

2

,

3

]

,

marker

=

matplotlib.

markers

.

CARETDOWNBASE

)

plt.

show

(

)

The display result is as follows:

fmt parameter

The fmt parameter defines the basic format, such as markers, line styles, and colors.

fmt = '[marker][line][color]'

For example, o:r: o represents a solid circle marker, : represents a dotted line, and r represents the color red.

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

'o:r'

)

plt.

show

(

)

The display result is as follows:

Line types:

Line type markers Description
'-' Solid line
':' Dotted line
'--' Dashed line
'-.' Dash-dot line

Color types:

Color markers Description
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' black
'w' white

Marker size and color

We can customize the marker size and color using the following parameters respectively:

Set the marker size:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

marker

=

'o'

,

ms

=

20

)

plt.

show

(

)

The display result is as follows:

Set the marker edge color:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

marker

=

'o'

,

ms

=

20

,

mec

=

'r'

)

plt.

show

(

)

The display result is as follows:

Set the marker inner color:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

marker

=

'o'

,

ms

=

20

,

mfc

=

'r'

)

plt.

show

(

)

The display result is as follows:

Customize the marker inner and edge colors:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

marker

=

'o'

,

ms

=

20

,

mec

=

'#4CAF50'

,

mfc

=

'#4CAF50'

)

plt.

show

(

)

The display result is as follows: