bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Python/Data Science and Scientific Python
Python•Data Science and Scientific Python

Matplotlib Line

Linestyle

You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line:

Example

Use a dotted line:

Formula

import matplotlib.pyplot as plt import numpy as np ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle = 'dotted') plt.show()

Result:

Example

Use a dashed line:

plt.plot(ypoints, linestyle = 'dashed')

Result:

Shorter Syntax

The line style can be written in a shorter syntax: linestyle can be written as ls. dotted can be written as . dashed can be written as --.

Example

Shorter syntax:

plt.plot(ypoints, ls = ':')

Result:

Line Styles

You can choose any of these styles:

Style

Or 'solid' (default) '-' Try it » 'dotted' ':' Try it » 'dashed' '--' Try it » 'dashdot' '-.' Try it » 'None' '' or ' ' Try it »

Line Color

You can use the keyword argument color or the shorter c to set the color of the line:

Example

Set the line color to red:

Formula

import matplotlib.pyplot as plt import numpy as np ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, color = 'r') plt.show()

Result:

You can also use

Hexadecimal color values

Example

Plot with a beautiful green line:... plt.plot(ypoints, c = '#4CAF50')...

Result:

Or any of the

140 supported color names.

Example

Plot with the color named "hotpink":... plt.plot(ypoints, c = 'hotpink')...

Result:

Line Width

You can use the keyword argument linewidth or the shorter lw to change the width of the line. The value is a floating number, in points:

Previous

Machine Learning - Data Distribution

Next

Machine Learning - Normal Data Distribution