Line Plot Styles in Matplotlib

Martin McBride
The Startup
Published in
7 min readFeb 12, 2021

--

Making your plots zing.

Photo by Aswin Anand on Unsplash

Matplotlib allows you to control many aspect of your graphs. In this article we will see how to style line plots. This includes

  • Controlling the colour, thickness and style (solid, dashed, dotted etc) of the lines.
  • Adding markers. A marker is a small square, diamond or other shape that marks a data point.

You can choose to plot data points using lines, or markers, or both.

Matplotlib has a simple notation to set the colour, line style and marker style using a coded text string, for example ‘r:’ creates a red, dottedline. It also supports additional parameters that give more options to control the appearance of the graph.

Line plots

You probably already know how to create a simple function plot in Matplotlib. You can do it something like this:

from matplotlib import pyplot as plt
import numpy as np

xa = np.linspace(0, 12, 100)
ya = np.sin(xa)*np.exp(-xa/4)

plt.plot(xa, ya)
plt.show()

That is nice enough, but you can improve things with a few simple effects.

--

--