Easy Tutorial
❮ Matplotlib Bar Matplotlib Scatter ❯

Matplotlib Pyplot

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

Pyplot is a commonly used plotting module that allows users to easily create 2D charts.

Pyplot includes a series of functions related to plotting, each of which makes some modifications to the current figure, such as adding markers, generating new figures, or creating new plotting areas.

To use it, we can import the pyplot library with an alias plt:

import matplotlib.pyplot as plt

This allows us to reference the Pyplot package methods using plt.

The following example plots a line from coordinates (0,0) to (0,100):

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 as follows:

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

plot() is used to draw points (markers) or lines, and its syntax is as follows:

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

Parameter descriptions:

Color characters: 'b' for blue, 'm' for magenta, 'g' for green, 'y' for yellow, 'r' for red, 'k' for black, 'w' for white, 'c' for cyan, '#008000' for RGB color strings. Multiple curves without specified colors will automatically choose different colors.

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

Marker characters: '.' for point marker, ',' for pixel marker (very small point), 'o' for filled circle marker, 'v' for inverted triangle marker, '^' for triangle marker, '>' for right triangle marker, '<' for left triangle marker, and so on.

To draw a line from (1, 3) to (8, 10), we need to pass the two arrays [1, 8] and [3, 10] to the plot function:

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 is as follows:

If we only want to plot two points instead of a line, we can use the o parameter to indicate filled circle markers:

Plotting points (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 is as follows:

We can also plot any number of points, as long as the number of points on both axes is the same.

To draw an irregular line with coordinates (1, 3), (2, 8), (6, 1), (8, 10), the corresponding arrays are [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 is as follows:

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 is as follows:

From the above figure, we can see that the x values are set to [0, 1] by default.

Here is an 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 is as follows:

From the above figure, we can see that the x values are set to [0, 1, 2, 3, 4, 5] by default.

In the following example, we plot a sine and cosine graph. In plt.plot() parameters, we include two pairs of x,y values. The first pair is x,y, corresponding to the sine function, and the second pair is x,z, corresponding 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 is as follows:

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

❮ Matplotlib Bar Matplotlib Scatter ❯