Open In App

How to plot a dashed line in matplotlib?

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Matplotlib is used to create visualizations and plotting dashed lines is used to enhance the style and readability of graphs. A dashed line can represent trends, relationships or boundaries in data. Below we will explore how to plot and customize dashed lines using Matplotlib. To plot dashed line:

Syntax: matplotlib.pyplot.plot(x, y, linestyle='dashed')

where:

  • x:  X-axis points on the line.
  • y:  Y-axis points on the line.
  • linestyle: Change the style of the line.

1. Plotting a Basic Dashed Line

The simplest way to plot a dashed line in Matplotlib is by setting the linestyle parameter to 'dashed' or using the shorthand '--'

Python
import matplotlib.pyplot as plt
x = [1.5, 2.6, 3.5, 4, 9]
y = [3.25, 6.3, 4.23, 1.35, 3]

plt.plot(x, y, linestyle='dashed')  # or linestyle='--'
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output

Output
Dashed line plot

2. Customizing the Color of the Dashed Line

You can easily change the color of your dashed line to match your design or to make different lines distinguishable.

Python
import matplotlib.pyplot as plt
x = [1.5, 2.6, 3.5, 4, 9]
y = [3.25, 6.3, 4.23, 1.35, 3]

plt.plot(x, y, linestyle='--', color='gold')
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.show()

Output

Output
Golden dashed line

3. Adjusting the Width (Thickness) of the Dashed Line

To make the dashed line thicker or thinner, use the linewidth or lw parameter.

Python
import matplotlib.pyplot as plt
x = [1.5, 5.6, 3.5, 4, 9]
y1 = [1, 4, 3, 4, 5]
y2 = [6, 7, 4, 9, 10]

plt.plot(x, y1, linestyle='--', color='black', linewidth=1)  # thinner line
plt.plot(x, y2, linestyle='--', color='red', linewidth=4)    # thicker line
plt.show()

Output

Output
Thin & thick dashes

4. Creating Custom Dash Patterns

In addition to predefined styles, Matplotlib lets you create custom dash patterns using set_dashes() which takes a list of dash and gap lengths (in points) for greater control over line appearance.

Python
import matplotlib.pyplot as plt
x = [1.5, 2.6, 3.5, 4, 9]
y = [3.25, 6.3, 4.23, 1.35, 3]
line, = plt.plot(x, y, color='blue')

line.set_dashes([5, 10, 15, 5]) 
plt.show()

Output

Output
Patterned dashed line
With these customizations we can easily plot dashed lines in our graphs.

Next Article
Article Tags :
Practice Tags :

Similar Reads