Back to Python Notes
Topic #121

Matplotlib Line

Line styles and colours turn basic lines into clear comparisons between datasets. Small style choices carry the story of the data.


Linestyle

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

Example

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

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

image

Example

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

image


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

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

image


Line Styles

You can choose any of these styles:

Style Or
'solid' (default) '-'
'dotted' ':'
'dashed' '--'
'dashdot' '-.'
'None' '' or ' '

Line Color

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

Example

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

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

image

You can also use Hexadecimal color values:

Example

...

plt.plot(ypoints, c = '#4CAF50')
...

image

Or any of the 140 supported color names.

Example

...

plt.plot(ypoints, c = 'hotpink')
...

image


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:

Example

import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linewidth = '20.5')
plt.show()

image


Multiple Lines

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

Example

import matplotlib.pyplot as plt
import numpy as np

y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])

plt.plot(y1)
plt.plot(y2)

plt.show()

image

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:

Example

import matplotlib.pyplot as plt
import numpy as np

x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1, x2, y2)
plt.show()

image

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Matplotlib Line – FAQs

Quick answers about learning Matplotlib Line in Python.

This free note from Coding Now Tech Institute explains Matplotlib Line in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Matplotlib Line, is 100% free with no signup required.
With focused practice, most students grasp Matplotlib Line in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now