How to change the style of a line with Matplotlib using Python

2 min readMatplotlibData Visualization
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Changing the style of a line in Matplotlib can sometimes help a lot with data visualization.

It makes the line clearer and facilitates greatly the reading of a chart.

There are many different styling available:

  1. solid or "-"
  2. dashed or "--"
  3. dashdot or "-."
  4. dotted or ":"

Here is the code

In order to plot something, let's generate data using NumPy, normally distributed data random generator numpy.random.normal().

Let's generate some sample data:

# To generate some sample data
import numpy as np

# To plot
import matplotlib.pyplot as plt

# We generate some sample data
x = np.random.normal(0,1,50)
Some sample data generation

In order to change the style, we are going to pass the ls argument.

The solid line

fig, axes = plt.subplots(1, 1, figsize=(8,4))
axes.plot(x, ls='-') # By passing the ls parameter
fig.tight_layout()
plt.show()

The result

A solid line plot

The dashed line

fig, axes = plt.subplots(1, 1, figsize=(8,4))
axes.plot(x, ls='--') # By passing the ls parameter
fig.tight_layout()
plt.show()

The result

A dashed line plot

The dashdot line

fig, axes = plt.subplots(1, 1, figsize=(8,4))
axes.plot(x, ls='-.') # By passing the ls parameter
fig.tight_layout()
plt.show()

The result

A dashdot line plot

The dotted line

fig, axes = plt.subplots(1, 1, figsize=(8,4))
axes.plot(x, ls=':') # By passing the ls parameter
fig.tight_layout()
plt.show()

The result

A dotted line plot

More on Matplotlib

If you like what you've just read and want to know more about the Matplotlib library (e.g. how to add labels, plot different types of plots, etc...) check out the other articles I wrote on the topic, just here :

Matplotlib - The Python You Need
We gathered the only Python essentials that you will probably ever need.
7-Day Challenge

Land Your First Data Science Job

A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.

Build portfolios that hiring managers love
Master the Python and SQL essentials to be industry-ready
Practice with real interview questions from tech companies
Access to the $100k/y Data Scientist Cheatsheet

Join thousands of developers who transformed their careers through our challenge. Unsubscribe anytime.

Free Newsletter

Master Data Science in Days, Not Months 🚀

Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚

Weekly simple and practical lessons
Access to ready to use code examples
Skip the math, focus on results
Learn while drinking your coffee

By subscribing, you agree to receive our newsletter. You can unsubscribe at any time.