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