Python Tutorials · Python Matplotlib

Matplotlib Line

Learn all about Matplotlib Line in this comprehensive tutorial.

5 min read intermediate
  • You can use the keyword argument linestyle, or shorter ls, to change the style of the plotted line:
  • The line style can be written in a shorter syntax:
  • You can choose any of these styles:
  • You can use the keyword argument color or the shorter c to set the color of the line:
  • You can use the keyword argument linewidth or the shorter lw to change the width of the line.
  • You can plot as many lines as you like by simply adding more plt.

Linestyle

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

python
python

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 --.

python

Line Styles

You can choose any of these styles:

StyleOr
'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:

python

You can also use Hexadecimal color values:

python

Or any of the 140 supported color names.

python

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:

python

Multiple Lines

You can plot as many lines as you like by simply adding more plt.plot() functions:

python

You can also plot many lines by adding the points for the x- and y-axis for each line in the same plt.plot() function.

(In the examples above we only specified the points on the y-axis, meaning that the points on the x-axis got the the default values (0, 1, 2, 3).)

The x- and y- values come in pairs:

python

Module quiz

2 questions
1

Which of the following is true about Matplotlib Line?

2

What is the most common pitfall when working with Matplotlib Line?

Answer all questions to submit.