Matplotlib Line

Line style, color, and width control how the connecting line in a Matplotlib plot actually looks.

Choosing a line style

The linestyle argument (or its shorthand ls) controls the dash pattern used to draw the line connecting your data points. The default is a solid line, but Matplotlib supports dashed, dash-dot, and dotted styles out of the box, plus the option to hide the line entirely.

StyleShorthandDescription
'solid''-'A continuous unbroken line
'dashed''--'Evenly spaced dashes
'dashdot''-.'Alternating dashes and dots
'dotted'':'A line of small dots
'none''' or ' 'No line drawn at all

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [4, 6, 5, 8, 7]

plt.plot(x, y, ls="--")
plt.show()

Choosing a line color

The color argument (or the shorthand c) sets the line's color. You can use a full name such as "red" or "green", a hex code such as "#3366cc", or one of the built-in single-letter shortcuts: r, g, b, c, m, y, k, and w for red, green, blue, cyan, magenta, yellow, black, and white.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [3, 5, 4, 7, 6]

plt.plot(x, y, color="#3366cc")
plt.show()

Controlling line width

The linewidth argument (shorthand lw) accepts a number specifying the thickness of the line in points. Larger numbers draw a bolder line, which can help a chart read well when it is shrunk down or projected on a screen.

Example

import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y = [2, 4, 3, 6, 5]

plt.plot(x, y, color="green", linestyle="-.", linewidth=3)
plt.show()
Note: linestyle, color, and marker can all be set on the same call, and can also be combined into one fmt shorthand string such as "o-.g" for a green dash-dot line with circular markers.
  • linestyle / ls sets the dash pattern
  • color / c sets the line color
  • linewidth / lw sets the thickness in points
  • marker adds a symbol at each data point, independent of the line style