TExcelTAD
Home › Matplotlib › Matplotlib Line

Matplotlib Line

Lines carry most of the information in a line chart, so Matplotlib lets you control their style, colour and width in detail.

Line style

The linestyle keyword (short: ls) accepts a name or a symbol:

Name Symbol Looks like
"solid" "-" ────────
"dashed" "--" ‒ ‒ ‒ ‒
"dotted" ":" ········
"dashdot" "-." ‒ · ‒ ·
"None" "" nothing drawn
python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 50)
styles = ["solid", "dashed", "dotted", "dashdot"]

for i, style in enumerate(styles):
    plt.plot(x, x * 0 + i, linestyle=style, label=style)
plt.ylim(-0.5, 4.5)
plt.legend(loc="upper left", ncols=4)
plt.title("Line styles")
plt.show()

print("Styles drawn:", ", ".join(styles))
Styles drawn: solid, dashed, dotted, dashdot

Custom dash patterns are possible too: linestyle=(0, (5, 2, 1, 2)) means "5 points on, 2 off, 1 on, 2 off", starting at offset 0.

Colour and width

color (short: c) sets the line colour and linewidth (short: lw) its thickness in points. The default width is 1.5.

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 2 * np.pi, 200)

plt.plot(x, np.sin(x), color="#d62728", linewidth=4)
plt.plot(x, np.sin(x + 1), color="navy", lw=1)
plt.plot(x, np.sin(x + 2), c="0.6", lw=2.5)   # a grey level as a string
plt.show()

for line in plt.gca().lines:
    print(line.get_color(), line.get_linewidth())
#d62728 4.0
navy 1.0
0.6 2.5

A string holding a number between "0" (black) and "1" (white) is a shade of grey.

Several lines

Each extra plot() call adds a line. When you don't set a colour, Matplotlib walks through its default colour cycle (blue, orange, green, red, purple, …):

python · try it yourself
import numpy as np
import matplotlib.pyplot as plt

years = np.arange(2018, 2025)
north = np.array([40, 44, 47, 45, 52, 58, 61])
south = np.array([35, 33, 38, 42, 41, 45, 50])
west = np.array([20, 26, 30, 36, 39, 40, 46])

plt.plot(years, north, label="North")
plt.plot(years, south, label="South")
plt.plot(years, west, label="West")
plt.legend()
plt.title("Sales by region")
plt.show()

for line in plt.gca().lines:
    print(f"{line.get_label():<6} {line.get_color()}  last value {line.get_ydata()[-1]}")
North  #1f77b4  last value 61
South  #ff7f0e  last value 50
West   #2ca02c  last value 46

The colours #1f77b4, #ff7f0e and #2ca02c are the first three entries of Matplotlib's default cycle, also called "C0", "C1" and "C2". You can use those short names anywhere a colour is expected.

Changing a line after drawing it

plot() returns Line2D objects with setter methods. This is handy in loops or when the style depends on the data:

python · try it yourself
import matplotlib.pyplot as plt

values = [3, 7, 4, 8, 5]
(line,) = plt.plot(values)          # unpack the single line

line.set_linestyle("--")
line.set_linewidth(3)
line.set_color("purple")
line.set_marker("o")
plt.show()

print(line.get_linestyle(), line.get_linewidth(), line.get_color(), line.get_marker())
-- 3.0 purple o

Note: get_linestyle() always returns the short symbol ("--"), even if you set the long name ("dashed").

Line joins and caps

Thick lines show how segments are joined. solid_joinstyle can be "miter", "round" or "bevel", and solid_capstyle controls line ends ("butt", "round", "projecting"). For most charts the defaults are fine; round joins look softer for very thick lines.

Test yourself

Which keyword sets the thickness of a line?

  1. width
  2. linewidth (or lw)
  3. thickness
  4. size

What does color="C1" refer to?

  1. Cyan
  2. The second colour of the default colour cycle (orange)
  3. The first colour of the cycle
  4. Column 1 of the data

Which linestyle symbol produces a dotted line?

  1. "--"
  2. "-."
  3. ":"
  4. "."