Understanding Matplotlib Line Styles

Someone
3 min readSep 29, 2024

--

In this blog, I continue my exploration of Matplotlib by learning how to apply various line styles in a plot. This fourth blog in my series demonstrates how to plot multiple lines with different styles and save the figure as a PNG file. Let’s break down the code used for this purpose.

My Program — linestyles

import matplotlib.pyplot as plt

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

plt.plot(x, y, linestyle="-")

plt.plot(x, [y_i-1 for y_i in y], linestyle="--")
plt.plot(x, [y_i-2 for y_i in y], linestyle="-.")
plt.plot(x, [y_i-3 for y_i in y], linestyle=":")

plt.savefig("Plots/fig_with_linestyles.png")

Importing `matplotlib.pyplot`

import matplotlib.pyplot as plt

The first step in any Matplotlib script is importing matplotlib.pyplot. We commonly abbreviate it as `plt` to simplify the syntax later in the code.

Defining Data Points for `x` and `y`

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

Here, I’ve defined two lists, x and y, which serve as the coordinates for the data points on the plot. The x list increases linearly, while the y list decreases linearly, creating an inverted line when plotted.

Plotting the First Line with a Solid Style

plt.plot(x, y, linestyle="-")

This is the first line of the plot. It uses a solid line style, specified by linestyle=”-”. The plot will connect the (x, y) pairs (1,5), (2,4), (3,3), (4,2), and (5,1) with a continuous line.

Plotting a Dashed Line

plt.plot(x, [y_i - 1 for y_i in y], linestyle=" - ")

This line follows the same x values but uses modified y values where each y is reduced by 1. This creates a new line that is parallel and below the first line. The linestyle=” — “ argument specifies that the line will appear dashed.

Plotting a Dash-Dot Line

plt.plot(x, [y_i - 2 for y_i in y], linestyle="-.")

For the third line, I use a dash-dot (-.) pattern. Here, each y value is reduced by 2, resulting in another parallel line further below the first two. The line style alternates between dashes and dots.

Plotting a Dotted Line

plt.plot(x, [y_i - 3 for y_i in y], linestyle=":")

This fourth and final line uses a dotted style (linestyle=”:”). As with the previous lines, I reduce each y value by 3 to shift the line further down.

Saving the Figure

plt.savefig("Plots/fig_with_linestyles.png")

After plotting all four lines with different styles, the plt.savefig() function is used to save the figure as a PNG file. The plot is saved inside a folder named Plots with the file name fig_with_linestyles.png.

The Output Plot

The final plot consists of four lines, each representing different line styles: solid, dashed, dash-dot, and dotted.

Figure with linestyles in matplotlib

Note: The colors were applied automatically by matplotlib

Conclusion

In this post, I demonstrated how to work with different line styles in Matplotlib. By plotting multiple lines with various linestyle options, it’s easy to create clear and visually distinct plots.

These line styles are useful when you want to differentiate between different data sets or add a stylistic element to your visualizations. Saving the figure using plt.savefig() allows you to store and use these plots for reports, articles, or presentations.

Stay tuned for the next blog where I’ll dive deeper into coloring the plots!

--

--

Someone
Someone

Written by Someone

Using medium as a progress tracker

No responses yet