Easy Tutorial
❮ Matplotlib Install Home ❯

Matplotlib Plotting Lines

If we customize the style of the lines, including the type, color, and size, during the plotting process.

Line Types

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

Type Abbreviation Description
'solid' (default) '-' Solid line
'dotted' ':' Dotted line
'dashed' '--' Dashed line
'dashdot' '-.' Dash-dotted 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 result is shown below:

Using abbreviation:

Example

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([6, 2, 13, 10])

plt.plot(ypoints, ls = '-.')
plt.show()

The result is shown below:

Line Color

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

Color types:

Color Marker Description
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White

Custom colors can also be used, such as SeaGreen, #8FBC8F, etc. For a complete list of styles, 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 result is shown below:

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 result is shown below:

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 result is shown below:

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 result is shown below:

Multiple Lines

The plot() method can include 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()

From the above figure, it can be seen that the default values for x are set to [0, 1, 2, 3].

The result is shown below:

We can also set the x-coordinates 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 result is shown below:

❮ Matplotlib Install Home ❯