As a developer, I’ve worked extensively with Matplotlib for data visualization. One of the most common yet useful ways to enhance your plots is by using dashed lines.
In this article, I’ll walk you through everything you need to know about creating and customizing dashed lines in Matplotlib. I’ll share practical tips and different methods that I’ve found effective in real-world projects, especially when working with datasets relevant to the USA, such as economic trends, stock prices, or weather data.
Let’s get in!
What Are Dashed Lines in Matplotlib?
Dashed lines are line styles where the line is broken into segments of dashes and spaces. In Matplotlib, you can easily apply dashed lines to your plots to improve visual distinction between data series.
By default, Matplotlib offers several line styles, including solid, dashed, dotted, and dash-dot. Among these, dashed lines ('--') are widely used to represent secondary or comparative data.
Read Matplotlib Time Series Plot
How to plot a Dashed Line in Matplotlib
Now, I will explain methods to plot a dashed line in Matplotlib.
Method 1: Use the Basic Line Style Parameter
The simplest way to create a dashed line in Matplotlib is by using the linestyle parameter in the plot() function in Python.
Here’s a quick example plotting average monthly temperatures for New York City:
import matplotlib.pyplot as plt
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun']
avg_temps = [31, 35, 42, 55, 65, 75]
plt.plot(months, avg_temps, linestyle='--', color='blue', label='Avg Temp')
plt.title('Average Monthly Temperatures in NYC')
plt.xlabel('Month')
plt.ylabel('Temperature (°F)')
plt.legend()
plt.show()You can see the output in the screenshot below.

In this example, the linestyle='--' creates a dashed line. It’s straightforward and perfect for quick visual differentiation.
Check out Module ‘matplotlib’ has no attribute ‘artist’
Method 2: Use the Shortcut Line Style String
Matplotlib allows you to specify line style shorthand directly in the format string. For instance, 'b--' means a blue dashed line.
Here’s how you can use it:
plt.plot(months, avg_temps, 'b--', label='Avg Temp')This method is concise and useful for simple plots, especially when you want to quickly combine color and line style.
Method 3: Customize Dash Patterns with dashes
Sometimes, the default dash pattern doesn’t fit your needs. Matplotlib lets you customize dash patterns using the dashes attribute, which defines the length of dashes and spaces in points.
For example, you might want longer dashes with shorter spaces to emphasize a trend line:
line, = plt.plot(months, avg_temps, color='green', label='Custom Dash')
line.set_dashes([10, 5]) # 10 points dash, 5 points space
plt.title('Custom Dashed Line Example')
plt.xlabel('Month')
plt.ylabel('Temperature (°F)')
plt.legend()
plt.show()You can see the output in the screenshot below.

This flexibility is great when you want to match styling guidelines or improve readability, especially in presentations or reports for US-based audiences.
Read Matplotlib Set y Axis Range
Method 4: Use set_linestyle() for Dynamic Styling
In interactive applications or scripts where you need to change line styles dynamically, you can use the set_linestyle() method on a line object.
Here’s an example:
line, = plt.plot(months, avg_temps, color='red', label='Dynamic Style')
line.set_linestyle((0, (3, 5, 1, 5))) # Complex dash sequence
plt.title('Dynamic Dashed Line Style')
plt.xlabel('Month')
plt.ylabel('Temperature (°F)')
plt.legend()
plt.show()You can see the output in the screenshot below.

The tuple (0, (3, 5, 1, 5)) specifies a dash pattern with 3 3-point dash, 5 points space, 1 point dash, and 5 points space. This level of customization can help when plotting multiple lines with distinct dash patterns.
Read Module ‘matplotlib’ has no attribute ‘plot’
Method 5: Combine Line Styles with Markers
To make your plots even more informative, you can combine dashed lines with markers. This is especially helpful when you want to highlight specific data points, such as quarterly sales figures or election polling results.
Example:
sales = [5000, 7000, 6500, 8000, 9000, 8500]
plt.plot(months, sales, linestyle='--', marker='o', color='purple', label='Sales')
plt.title('Quarterly Sales with Dashed Lines and Markers')
plt.xlabel('Month')
plt.ylabel('Sales (USD)')
plt.legend()
plt.show()Using markers along with dashed lines improves data readability, which is crucial when presenting to stakeholders.
Check out Matplotlib Update Plot in Loop
Tips for Using Dashed Lines Effectively
- Contrast: Make sure your dashed lines contrast well against the background and other plot elements. Use colors that are easy on the eyes, especially for presentations.
- Line Width: Adjust
linewidthto make dashed lines more visible. Thin dashed lines can sometimes be hard to see. - Legend: Always include a legend to explain what each line represents, especially when using multiple dashed patterns.
- Consistency: Use consistent dash patterns across related plots to maintain a professional look.
Dashed lines are a simple yet useful tool in Matplotlib that can significantly enhance your data visualizations. Whether you’re plotting economic indicators, weather trends, or sales data relevant to the USA, knowing how to customize dashed lines helps you communicate your message more effectively.
I’ve shared multiple methods, from basic line styles to advanced dash pattern customization, that you can use depending on your project needs. Experiment with these techniques to find the style that best suits your data story.
Other Python tutorials you may like:
- Matplotlib is currently using agg, a non-GUI backend
- Matplotlib 2d Surface Plot
- Matplotlib Unknown Projection ‘3d’
- How to Rotate Date Tick Labels in Matplotlib

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.