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

Matplotlib Pyplot

Matplotlib Pyplot

Pyplot is a sub-library of Matplotlib that provides plotting APIs similar to MATLAB.

Pyplot is a commonly used plotting module that makes it easy for users to draw 2D charts.

Pyplot contains a series of plotting functions and related functions; each function makes some modification to the current figure, such as adding markers to the image, generating new images, creating new plotting areas in the image, and so on.

When using it, we can import the pyplot library with import and set an alias plt:

import

matplotlib.

pyplot

as

plt

This way we can use plt to reference the methods of the Pyplot package.

The following are some commonly used pyplot functions:

Besides these basic functions, pyplot also provides many other functions, such as functions for setting chart properties, functions for adding text and annotations, functions for saving charts to files, and so on.

In the following example, through two coordinates (0,0) to (6,100) to draw a line:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

xpoints

=

np.

array

(

[

0

,

6

]

)

ypoints

=

np.

array

(

[

0

,

100

]

)

plt.

plot

(

xpoints

,

ypoints

)

plt.

show

(

)

The output is shown below:

In the example above we used Pyplot's plot() function, which is the most basic function for drawing 2D graphics.

plot() is used for drawing; it can draw points and lines, with the following syntax:


# Draw a single line
plot([x], y, [fmt], *, data=None, **kwargs)
# Draw multiple lines
plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)

Parameter Description:

plot

(

x

,

y

)

       

Create a 2D line plot of the data in y against the corresponding values in x, using the default style

plot

(

x

,

y

,

'bo'

)

 

Create a 2D line plot of the data in y against the corresponding values in x, drawn with blue solid circles

plot

(

y

)

         

The value of x is 0..N-1

plot

(

y

,

'r+'

)

   

Use the red + sign

Color characters:'b' blue, 'm' magenta, 'g' green, 'y' yellow, 'r' red, 'k' black, 'w' white, 'c' cyan, '#008000' RGB color string. When multiple curves do not specify colors, different colors are automatically selected.

Line style parameters:'‐' solid line, '‐‐' dashed line, '‐.' dash-dot line, ':' dotted line.

Marker characters:'.' point marker, ',' pixel marker (very small point), 'o' solid circle marker, 'v' triangle down marker, '^' triangle up marker, '>' triangle right marker, '<' triangle left marker... etc.

If we want to draw a line from coordinates (1, 3) to (8, 10), we need to pass two arrays [1, 8] and [3, 10] to plot functions as follows:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

xpoints

=

np.

array

(

[

1

,

8

]

)

ypoints

=

np.

array

(

[

3

,

10

]

)

plt.

plot

(

xpoints

,

ypoints

)

plt.

show

(

)

The output of the code above is:

If we only want to draw two coordinate points instead of a line, we can use the o parameter, which represents a solid circle marker:

Plot two points at coordinates (1, 3) and (8, 10)

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

xpoints

=

np.

array

(

[

1

,

8

]

)

ypoints

=

np.

array

(

[

3

,

10

]

)

plt.

plot

(

xpoints

,

ypoints

,

'o'

)

plt.

show

(

)

The output of the code above is:

We can also plot any number of points, just make sure the number of points on both axes is the same.

Draw an irregular line with coordinates (1, 3), (2, 8), (6, 1), (8, 10), corresponding to the two arrays: [1, 2, 6, 8] and [3, 8, 1, 10].

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

xpoints

=

np.

array

(

[

1

,

2

,

6

,

8

]

)

ypoints

=

np.

array

(

[

3

,

8

,

1

,

10

]

)

plt.

plot

(

xpoints

,

ypoints

)

plt.

show

(

)

The output of the code above is:

If we do not specify points on the x-axis, x will be set according to the values of y as 0, 1, 2, 3..N-1.

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

3

,

10

]

)

plt.

plot

(

ypoints

)

plt.

show

(

)

The output of the code above is:

As can be seen from the figure above, the value of x is set by default to ** [0, 1]**.

Let's look at another example with more values:

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

ypoints

=

np.

array

(

[

3

,

8

,

1

,

10

,

5

,

7

]

)

plt.

plot

(

ypoints

)

plt.

show

(

)

The output of the code above is:

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

In the following example we draw a sine and cosine graph, with two pairs in the plt.plot() parameters x,y values, the first pair is ** x,y**, which corresponds to the sine function, and the second pair is x,z, which corresponds to the cosine function.

Example

import

matplotlib.

pyplot

as

plt

import

numpy

as

np

x

=

np.

arange

(

0

,

4

*np.

pi

,

0.1

)

 

start,stop,step

y

=

np.

sin

(

x

)

z

=

np.

cos

(

x

)

plt.

plot

(

x

,

y

,

x

,

z

)

plt.

show

(

)

The output of the code above is:

For more information, refer to:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html