I still remember the first time I plotted a line chart in Python using Matplotlib. It looked fine, but when I tried to add a dashed line, the spacing didn’t look the way I wanted.
In this tutorial, I’ll walk you through everything I’ve learned about controlling dashed line spacing in Matplotlib. I’ll show you different methods, share real Python code, and explain how you can use them in your own projects.
You’ve probably used line charts to show trends like stock prices, sales growth, or weather patterns.
Dashed lines are a great way to highlight comparisons, and with the right spacing, they can make your charts much easier to read.
What is Dashed Line Spacing in Matplotlib?
Dashed line spacing in Matplotlib refers to the pattern of dashes and gaps along a line.
Instead of a continuous solid line, you can break it into repeating dash-gap sequences.
For example, you can create a line that looks like — — — this with equal spacing.
Or you can design a custom pattern like — —- — with different dash and gap lengths.
This gives you flexibility to:
- Differentiate multiple lines in the same chart.
- Emphasize specific data series.
- Create professional, publication-ready plots.
Method 1 – Use Predefined Line Styles in Python Matplotlib
The easiest way to create dashed lines in Matplotlib is by using predefined line styles. Python makes this very convenient because you can simply pass a string argument to the linestyle parameter.
Here’s an example.
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 10, 100)
y = np.sin(x)
# Plot with predefined dashed line
plt.plot(x, y, linestyle='--', color='blue', label='Dashed Line')
plt.title("Predefined Dashed Line in Matplotlib")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend()
plt.show()You can refer to the screenshot below to see the output.

This code will create a simple sine wave with a dashed line style. You don’t control the spacing here, but it’s the quickest option when you just need a standard dashed line.
Method 2 – Use the dashes Parameter for Custom Spacing in Python
When I wanted more control, I discovered the dashes parameter. It allows you to define a sequence of numbers representing dash length and gap length.
Here’s how it works in Python.
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 20, 200)
y = np.cos(x)
# Plot with custom dash pattern
plt.plot(x, y, color='red', linestyle='--', dashes=[5, 2, 10, 3], label='Custom Dashes')
plt.title("Custom Dashed Line Spacing in Matplotlib")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend()
plt.show()You can refer to the screenshot below to see the output.

In this example, [5, 2, 10, 3], it means:
- Draw a dash of length 5 points.
- Leave a gap of 2 points.
- Draw a dash of 10 points.
- Leave a gap of 3 points.
The sequence then repeats along the line. This method is perfect when you want unique dash patterns for different lines in the same chart.
Method 3 – Use Python’s set_dashes() with Line2D Objects
Sometimes, I prefer working directly with Line2D objects because it gives me more flexibility. With this method, you first create a line and then apply a custom dash sequence.
Here’s the Python code.
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 15, 150)
y = np.tan(x)
# Create line object
line, = plt.plot(x, y, color='green', label='Line2D Dashed')
# Apply custom dash pattern
line.set_dashes([8, 4, 2, 4])
plt.title("Dashed Line with Line2D set_dashes() in Matplotlib")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.ylim(-10, 10) # limit y-axis for better view
plt.legend()
plt.show()You can refer to the screenshot below to see the output.

In this case, the sequence [8, 4, 2, 4] creates a long dash, a gap, a short dash, and another gap. This approach is useful when you need to modify line styles after plotting.
Method 4 – Combine Multiple Custom Dashed Lines
In real-world Python projects, I often plot multiple datasets together. To make them easy to read, I assign different dash patterns to each line.
Here’s a practical example.
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)
y3 = np.sin(x) + np.cos(x)
# Plot multiple lines with different dash styles
plt.plot(x, y1, color='blue', dashes=[5, 2], label='Sine Wave')
plt.plot(x, y2, color='red', dashes=[2, 2], label='Cosine Wave')
plt.plot(x, y3, color='purple', dashes=[10, 5, 2, 5], label='Sine + Cosine')
plt.title("Multiple Dashed Line Spacing in Python Matplotlib")
plt.xlabel("X Axis")
plt.ylabel("Y Axis")
plt.legend()
plt.show()You can refer to the screenshot below to see the output.

This example shows how you can clearly distinguish between three different mathematical functions. Each line has its own dash spacing, making the visualization easy to interpret.
Method 5 – Style Dashed Lines for Presentations in Python
When I present data to clients in the U.S., I often need clear, professional visuals. Dashed lines with good spacing can make charts look polished and easy to follow.
Here’s an example of how I use thicker lines and custom spacing for presentations.
import matplotlib.pyplot as plt
import numpy as np
# Create sample data
x = np.linspace(0, 12, 120)
y = np.exp(-x/3) * np.sin(2*x)
# Plot with bold dashed line
plt.plot(x, y, color='black', linewidth=2.5, dashes=[12, 6], label='Exponential Decay')
plt.title("Professional Dashed Line Styling in Python Matplotlib")
plt.xlabel("Time (Months)")
plt.ylabel("Value")
plt.legend()
plt.show()The [12, 6] pattern creates a long dash with a clear gap, making it perfect for business reports. This is one of my favorite tricks when I need to highlight trends in financial or sales data.
Tips for Using Dashed Line Spacing in Python
Here are some quick tips I’ve learned over the years:
- Keep it simple: Don’t use overly complex dash patterns unless necessary.
- Use color + spacing together: Combine different colors with unique dash spacing for maximum clarity.
- Think about printing: If your chart will be printed in black and white, varying dash spacing is more effective than just color.
- Test readability: Always check if the dashed lines are easy to distinguish when viewed on different screens.
Conclusion
Dashed line spacing in Matplotlib might seem like a small detail, but it makes a big difference in readability. By using predefined styles, the dashes parameter, or the set_dashes() method, you can fully control how your lines appear.
I’ve used these techniques in real-world Python projects for over a decade, from stock analysis to sales reports. And every time, customizing dashed lines has helped me create charts that are both professional and easy to understand.
So the next time you’re plotting in Python, try experimenting with different dash patterns.
You’ll be surprised how much better your visualizations can look.
You may also read:
- What is Matplotlib Inline in Python
- Matplotlib Best Fit Line
- Matplotlib Dashed Line
- Matplotlib Plot Bar Chart

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.