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

Matplotlib plot lines

Matplotlib plot lines

During plotting, if we customize the line style, including the line type, color, and size, etc.

Line types

The line type can be defined using the linestyle parameter, abbreviated as ls.

Type Shorthand Description
'solid' (default) '-' Solid line
'dotted' ':' Dotted line
'dashed' '--' Dashed line
'dashdot' '-.' Dash-dot line
'None' '' or ' ' No line

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

linestyle

=

'dotted'

)

plt.

show

(

)

The display result is as follows:

Using abbreviations:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

ls

=

'-.'

)

plt.

show

(

)

The display result is as follows:

Line colors

The line color can be defined using the color parameter, abbreviated as c.

Color types:

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

Of course, you can also customize color types, for example:SeaGreen、#8FBC8F etc.; for the complete style, you can refer to HTML color values。

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

color

=

'r'

)

plt.

show

(

)

The display result is as follows:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

c

=

'#8FBC8F'

)

plt.

show

(

)

The display result is as follows:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

c

=

'SeaGreen'

)

plt.

show

(

)

The display result is as follows:

Line width

The line width can be defined using the ** linewidth ** parameter, abbreviated as lw, and the value can be a float, such as:1、2.0、5.67 , etc.

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

ypoints

,

linewidth

=

'12.5'

)

plt.

show

(

)

The display result is as follows:

Multiple lines

The plot() method can contain multiple pairs of x, y values to draw multiple lines.

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

y1

=

np.

array

(

[

3

,

7

,

5

,

9

]

)

y2

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

y1

)

plt.

plot

(

y2

)

plt.

show

(

)

As can be seen from the figure above, x is set by default to [0, 1, 2, 3]。

The display result is as follows:

We can also set the x coordinates and other values ourselves:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x1

=

np.

array

(

[

0

,

1

,

2

,

3

]

)

y1

=

np.

array

(

[

3

,

7

,

5

,

9

]

)

x2

=

np.

array

(

[

0

,

1

,

2

,

3

]

)

y2

=

np.

array

(

[

6

,

2

,

13

,

10

]

)

plt.

plot

(

x1

,

y1

,

x2

,

y2

)

plt.

show

(

)

The display result is as follows: