Lesson 05
Line Styles and Colours
Control how a line looks with colour, line style, width, and transparency, and learn the short aliases that appear in most Matplotlib code.
Once a chart has more than one line, style stops being decoration and starts carrying meaning. A thicker line says this is the one that matters. A dashed line says this is a target, not a measurement. Four arguments cover nearly everything: color, linestyle, linewidth, and alpha.
Colour
color takes a named colour such as "teal", a single-letter code such as "g", or a hex string such as "#7C3AED". The alias c means the same thing. Hex is worth the extra characters when a chart has to match a house style.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
target = [50] * 7
plt.plot(days, signups, color="#7C3AED")
plt.plot(days, target, c="g")
plt.show()
print("one-letter codes:", ", ".join(["b", "g", "r", "c", "m", "y", "k", "w"]))Output:
one-letter codes: b, g, r, c, m, y, k, wk is black, because b was already taken by blue.
Line style
linestyle (or ls) chooses solid, dashed, dotted, or dash-dot. Use it to separate a measurement from a forecast or a target, and you have a chart that still reads correctly when printed in black and white.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
target = [55] * 7
plt.plot(days, signups, color="#7C3AED", linestyle="solid", label="actual")
plt.plot(days, target, color="#94A3B8", ls="--", label="target")
plt.legend()
plt.show()
above = [day for day, value in zip(days, signups) if value > 55]
print("days above target:", ", ".join(above))Output:
days above target: Tue, Thu, Fri, SatWidth and transparency
linewidth (or lw) is measured in points, and the default is 1.5. Raising it on one line is the simplest way to say which line the chart is about. alpha runs from 0 to 1 and fades a line, which helps when several lines overlap.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
last_week = [38, 44, 49, 52, 61, 55, 43]
plt.plot(days, last_week, color="#94A3B8", lw=1.5, alpha=0.5, label="last week")
plt.plot(days, signups, color="#7C3AED", lw=3.5, label="this week")
plt.legend()
plt.title("This week stands out")
plt.show()
growth = sum(signups) - sum(last_week)
print("signups gained:", growth)Output:
signups gained: 56The format string shortcut
A third positional argument packs colour, marker, and line style into one small string, in any order: "g--" is a green dashed line, "ro" is red circles with no line. It is quick for a throwaway look, but the named arguments are clearer in code that someone else will read.
import matplotlib.pyplot as plt
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
signups = [42, 58, 51, 66, 73, 61, 47]
plt.plot(days, signups, "g--")
plt.show()
plt.plot(days, signups, color="green", linestyle="--")
plt.show()
print("both lines above are the same")Output:
both lines above are the sameKey takeaways
-
color takes a name, a one-letter code, or a hex string; c is the alias
-
linestyle picks solid, dashed, dotted, or dashdot; ls is the alias
-
linewidth is in points and makes one line the main one
-
alpha fades a line from 1 down to 0 so overlaps stay readable
-
Next: Markers on a Line
-
Previous: Multiple Lines and a Legend
Loading playground…
Check your understanding
Line Styles and Colours Quiz
Which argument sets the colour of a line?
1 / 10