In this Python tutorial, we will discuss the **Matplotlib dashed line** in python to plot the graph with the dashed line style, and we will also cover the following topics:

- Matplotlib dashed line
- Matplotlib dashed line how to plot
- Matplotlib dashed line example
- Matplotlib dashed line styles
- Matplotlib dashed line colors
- Matplotlib dashed line with markers
- Matplotlib dashed line horizontal line
- Matplotlib dashed line vertical line
- Matplotlib dashed line spacing
- Matplotlib dashed line legend
- Matplotlib dashed line contour
- Matplotlib dashed line width
- Matplotlib dashed line errorbar

## Matplotlib dashed line

- In Python, Matplotlib is the widely used library for data visualization.
- By using this library, we can create a
**line chart**in python using the**pyplot**submodule or a method. - Line chart visualizes the relationship between the two quantities on X-axis and Y-axis on the X-Y plane or the coordinate plane.

Matplotlib dashed line is a **special styled line chart** that represents the relationship between the X-axis and Y-axis with** linestyle- dashed**.

Also, Read: Matplotlib subplot

## Matplotlib dashed line how to plot

The following steps are used to create a matplotlib dashed line chart which is outlined below:

**Defining Libraries:**Import the important libraries which are required for the creation of the dashed line chart ( For visualization: pyplot from matplotlib, For data creation and manipulation: numpy)**Define X and Y:**Define the data values on X-axis and Y-axis.**Plot a line:**By using**pyplot()**method with special parameter linestyled as dashed.**Display:**At last display the plot.

**The syntax to create a matplotlib dashed line chart is as below**:

`matplotlib.pyplt.plot(x, y, linestyle='dashed')`

**The above-used parameters are outlined as below:**

**x:**X-axis coordinates of the points on the line.**y:**Y-axis coordinates of the points on the line.**linestyle:**special feature used to change the style of the line. We mention it as dashed because our main objective is to create a dashed line graph.

Read: Matplotlib plot bar chart

## Matplotlib dashed line example

In the above sections, we discussed what a dashed line graph exactly means and what are the various steps used to create or plot a matplotlib dashed line graph.

**Let’s understand the concept with the help of an example as below:**

**# Import libraries**
import matplotlib.pyplot as plt
**# Define Ax**es
x_points = [1.5, 2.6, 3.5, 4, 9]
y_points = [3.25, 6.3, 4.23, 1.35, 3]
**# Plot a graph**
plt.plot(x_points, y_points, linestyle = 'dashed')
**# Display graph**
plt.show()

- In the above, example we import the
**matplotlib.pyplot**library. - Then we define the
**X-axis and Y-axis**of the cartesian plane. **plt.plot()**method is used with parameter linestyle “dashed” to plot a graph with a dashed line.- Then we finally use the method
**plt.show()**to display the plotted graph.

Read: What is matplotlib inline

## Matplotlib dashed line styles

To add some extra features or styles to graphs or charts we use different styles. Basically, styles are used to change the overall look of the plot or graph, or chart.

To achieve more control on the style we have to provide a dash tuple.

**# Create dash tuple as:
**dash tuple (offset, (on_off_seq))

**Example:** (‘loosely dashed’, (0,(5,20))) where 5 means 5pt line, and 20 means 20pt space.

**In the matplotlib dashed line the following styles are available which are listed below:**

- Loosely dashed
- Dashed
- Densely dashed

Let’s discuss the different styles one by one with the help of examples for clear understanding:

### Matplotlib dashed line – style *“loosely dashed* “

Now we will add more features to the dashed line.

**Let’s understand the concept with help of an example:**

**# Import libraries**
import matplotlib.pyplot as plt
from collections import OrderedDict
**# Define dash tuple**
linestyles = OrderedDict(
[
('loosely dashed', (0, (5, 20))),
('dashed', (0, (10, 4))),
('densely dashed', (0, (1, 3))),
])
**# Plot the graph**
plt.plot([0,100], [0,1], linestyle=linestyles['loosely dashed'])
**# Display the plot**
plt.show()

- In the above example, firstly we import the
**matplotlib.pyplot**library and then we**import OrderedDict from the collections**. - Next, we define the
**dash tuple**with all the available styles. - Then we plot use
**plt.plot()**method to plot the graph with the loosely dashed features with 1pt line and 3pt space. - Finally, we show the graph by using the method
**plt.show()**.

Read Matplotlib default figure size

### Matplotlib dashed line – style *” dashed “*

Add some extra features to the dashed line.

**Let’s understand the concept with help of an example:**

**# Import libraries**
import matplotlib.pyplot as plt
from collections import OrderedDict
**# Define dash tuple**
linestyles = OrderedDict(
[
('loosely dashed', (0, (5, 20))),
('dashed', (0, (10, 4))),
('densely dashed', (0, (1, 3))),
])
**# Plot the graph**
plt.plot([0,100], [0,1], linestyle=linestyles['dashed'])
**# Display the plot**
plt.show()

- In the above example, firstly we import the
**matplotlib.pyplot**library and then we**import OrderedDict from the collections**. - Next, we define the dash tuple with all the available styles.
- Then we plot use
**plt.plot()**method to plot the graph with the dashed features with 10pt line and 4pt space. - Finally, we show the graph by using the method
**plt.show()**.

### Matplotlib dashed line – style *” densely dashed “*

Add some extra features to the dashed line.

**Let’s understand the concept with help of an example: **

**# Import libraries**
import matplotlib.pyplot as plt
from collections import OrderedDict
**# Define dash tuple**
linestyles = OrderedDict(
[
('loosely dashed', (0, (5, 20))),
('dashed', (0, (10, 4))),
('densely dashed', (0, (1, 3))),
])
**# Plot the graph**
plt.plot([0,100], [0,1], linestyle=linestyles['densely dashed'])
**# Display the plot**
plt.show()

- In the above example, firstly we import the
**matplotlib.pyplot**library and then we**import OrderedDict from the collections**. - Next, we define the dash tuple with all the available styles.
- Then we plot use
**plt.plot()**method to plot the graph with the dashed features with 1pt line and 3pt space. - Finally, we show the graph by using the method
**plt.show()**.

Read: Python plot multiple lines using Matplotlib

## Matplotlib dashed line colors

**plt.plot()** method is basically used to plot the relationship between data on X-axis and Y-axis and the parameter **color** specifies the different colors which we use to highlight different plot lines.

**The syntax for this is given below:**

`matplotlib.pyplot.plot(x, y, linestyle='dashed', color=None)`

**Colors available in matplotlib are given below:**

**Blue color:**Write it as ‘blue’ or ‘b’.**Red color:**Write it as ‘red’ or ‘r’.**Green color:**Write it as ‘green’ or ‘g’ .**Cyan color:**Write it as ‘cyan’ or ‘c’ .**Magenta color:**Write it as ‘magenta’ or ‘m’ .**Yellow color:**Write it as ‘yellow’ or ‘y’ .**Black color:**Write it as ‘black’ or ‘k’ .**White color:**Write it as ‘white’ or ‘w’ .

**Let’s understand the concept of changing the color of plot lines with help of the below example:**

**# Import libraries**
import matplotlib.pyplot as plt
**# Define Ax**es
x_points = [1.5, 2.6, 3.5, 4, 9]
y1_points = [1, 2, 3, 4, 5]
y2_points = [6, 7, 8, 9, 10]
y3_points = [3, 4.3, 4.23, 6, 3]
**# Plot a graph**
plt.plot(x_points, y1_points, linestyle = 'dashed', color = 'cyan')
plt.plot(x_points, y2_points, linestyle = 'dashed',color = 'm')
plt.plot(x_points, y3_points, linestyle = 'dashed', color = 'green')
**# Display graph**
plt.show()

- In the above example, we use the
**plt.plot()**method for plotting data by using linestyle dashed. - To easily differentiate between the plotted lines we use the parameter color.
- Here we plot three different dashed line plot and we use three different colors as
**cyan, magenta, green**.

Read: Matplotlib plot a line

## Matplotlib dashed line with markers

Markers show us the data points. Basically, they are used to highlight the highest and lowest data points.

We can set the different patterns, colors, sizes, etc of the markers.

Even, we can also set **markeredgecolor**, **facecolor**, etc.

**The syntax for this is given below:**

`matplotlib.pyplot.plot(x, y, linestyle='dashed', marker=None, markersize=None,markeredgecolor=None ... )`

**Let’s understand the concept with the help of an example:**

**# Import libraries**
import matplotlib.pyplot as plt
**# Define Ax**es
x_points = [1.5, 2.6, 3.5, 4, 9]
y_points = [1, 2, 3, 4, 5]
**# Plot a graph**
plt.plot(x_points, y1_points, linestyle = 'dashed', marker = '*' )
**# OR**
plt.plot(x_points, y1_points, linestyle = 'dashed', marker = 'd', ms= '20')
**# OR**
plt.plot(x_points, y1_points, linestyle = 'dashed', marker = '>', ms= '20', mec= 'r')
**# OR**
plt.plot(x_points, y1_points, linestyle = 'dashed', marker = '>', ms= '20', mec= 'r', mfc='y')
**# Display graph**
plt.show()

- In this above example, we use the markers in dashed line graph by using markers as argument and we also tune the marker according to your need.
- First case: We use
**marker****Star ‘ * ‘**. - Second case: We use
**marker****Diamond ‘d’**and set the**markersize 20**. - Third case: We use
**marker****Circle ‘o’**and set the**markersize 10**and set the**markeredgecolor red**. - Fourth case: We use
**marker****Triangle Right ‘>’**and set the**markersize 20**and set the**markeredgecolor red**and**makercolorface yellow**.

Read: How to install matplotlib python

## Matplotlib dashed line horizontal line

In some cases, we need to draw a horizontal line in a graph or plot.

For plotting a horizontal line **axhline()** method is used.

**Syntax to plot horizontal line at specified date:**

`matplotlib.pyplot,axhline(x=0, ymin=0, ymax=1, **kwargs)`

**In the above syntax, the following parameters are used which are outlined below:**

**x:**To place the horizontal line in data coordinates.**ymin:**a horizontal line starting position on the y-axis. It takes values between 0 and 1. 0 stands for the bottom of the axis and 1 stands for the top of the axis.**ymax**: a horizontal line ending position on the y-axis. It takes values between 0 and 1. 0 stands for the bottom of the axis and 1 stands for the top of the axis.**kwargs:**specify properties to change the style, color, the linewidth of line.

**Let’s understand the concept with the help of an example:**

**# Import libraries**
import matplotlib.pyplot as plt
**# Plot the graph**
plt.plot([0,10], [0,6])
**# Plot horizontal line**
plt.axhline(y=1.5, linestyle='dashed', color='black')
**# Display the plot**
plt.show()

- In the above example, firstly we import the
**matplotlib.pyplot**library and then we**import OrderedDict from the collections**. - Then we plot use
**plt.plot()**method to plot the graph with the dashed line argument. - Then we use
**axhline()**method to draw a horizontal line. - Finally, we show the graph by using the method
**plt.show()**.

Read: Matplotlib plot_date

## Matplotlib dashed line vertical line

In some cases, we need to draw a vertical line in a graph or plot.

For plotting a horizontal line **axvline()** method is used.

**Syntax to plot horizontal line at specified date:**

`matplotlib.pyplot,axvline(x=0, ymin=0, ymax=1, **kwargs)`

**In the above syntax, the following parameters are used which are outlined below:**

**x:**To place the vertical line in data coordinates.**ymin:**a vertical line starting position on the y-axis. It takes values between 0 and 1. 0 stands for the bottom of the axis and 1 stands for the top of the axis.**ymax**: a vertical line ending position on the y-axis. It takes values between 0 and 1. 0 stands for the bottom of the axis and 1 stands for the top of the axis.**kwargs:**specify properties to change the style, color, the linewidth of line.

**Let’s understand the concept with the help of an example:**

**# Import libraries**
import matplotlib.pyplot as plt
**# Plot the graph**
plt.plot([0,10], [0,6])
**# Plot vertical line**
plt.axvline(x=1.5, linestyle='dashed', color='black')
**# Display the plot**
plt.show()

Read: Matplotlib log log plot

## Matplotlib dashed line spacing

Spacing specifies the length of space between the dashes. You can increase or decrease the space according to your requirement.

**The syntax for changing the space is described as below:**

`matplotlib.plt.plot(x,y,linestyle='dashed',dashes= ( length of line , space of line ) )`

**Let’s understand the concept with the help of an example:**

**# Import libraries**
import matplotlib.pyplot as plt
**# Plot the graph**
plt.plot([0,10], [0,1], linestyle='dashed', dashes=(1,3))
plt.plot([0,10], [0,2], linestyle='dashed', dashes=(3,2))
plt.plot([0,10], [0,3], linestyle='dashed', dashes=(5,10))
**# Display the plot**
plt.show()

- In the above example we plot a graph by using
**plt.plot()**method - We pass
**dashes**as argument in**plt.plot()**method to change the spacing between dashed line. - From the output, you observed that the spacing between the dashes are different in all three cases.

Read: Matplotlib subplots_adjust

## Matplotlib dashed line legend

- A legend is used to express the coordinates of the graph.
- In matplotlib, we have a method
**legend()**. **legend()**is used to place a legend on the axes.

**The syntax for the above is given below:**

`matplotlib.pyplot.legend()`

**The legend function has the following arguments which are outlined below:**

**loc:**specify the location of the legend. By default value of loc is “best” or upper left. Other locations are defined as ‘upper right’, ‘lower right’, ‘lower left’.**bbox_to_anchor((x,y) , ncol) :**x and y specify the coordinates and ncol represent the number of columns. By default value of ncol is 1.**markerscale :**specify the size of the legend marker.**facecolor:**specify the legend’s background color.**edgecolor:**specify the legend’s edge color.

**Let’s understand the concept with the help of an example:**

**#Import Libraries**
import numpy as np
import matplotlib.pyplot as plt
**# Data to be plotted**
x = np.arange(5)
y1 = [1, 2, 3, 4, 5]
y2 = [1, 8, 27, 64, 125]
**# Function to plot**
plt.plot(x, y1, '--', label ='Numbers')
plt.plot(x, y2, label ='Cube of numbers')
**# Function add a legend**
plt.legend(bbox_to_anchor =(0.85, 1.12), ncol = 3)
**#OR**
plt.legend( loc='upper center')
**# Function to show the plot**
plt.show()

- In the above example, firstly we import the libraries
**matplotlib.pylot**and**NumPy**. - Next, by using NumPy
**arrange()**method we define data. - Then we plot the data by using
**plt.plot()**method and set their labels also. **plt.legend ()**method is used to define the legend- Case 1: We use
**bbox_to_anchor()**method - Case 2: We use
**loc**to specify the position. - Finally, we use
**show()**to display the graph.

Read: Matplotlib best fit line

## Matplotlib dashed line contour

Contour is used to plot three-dimensional data in two dimensions.

**plt.contour()** and **plt.contourf()** method is used to create 3D data plot.

**The syntax for the above is given below:**

`matplotlib.pyplot.contour([X,Y]Z,[levels],**kwargs)`

**The followings are the argument of the above describe below:**

**X:**specify the 2 D array, where len(X) is rows of Z.**Y:**specify the 2 D array, where len(Y) is columns of Z.**Z:**It draws the shape of (X, Y) and specifies the height values.**levels:**specify the number and positions of the contour lines.

**Let’s understand the** **concept with the help of an example:**

**# Import Libraries**
import numpy as np
import matplotlib.cm as cm
import matplotlib.pyplot as plt
**# Define data**
delta = 0.035
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z = np.exp(-X**2 - Y**2)
**# contour() method**
fig,axis = plt.subplots()
Counter_plot = axis.contour(X, Y, Z, 6, colors='k', linestyles='dashed')
**# Plot the graph
**
plt.show()

- Import necessary libraries such as
**matplotlib.pyplot**,**matplotlib.cm**, and**numpy**. - Define the data or 2 D data by using
**arrange()**method. - Create a mesh grid by using
**meshgrid()**method and pass the value of x and y. - Define Z as the
**function**of X and Y. - Then use the
**contour()**method to plot the 3D plot and also pass the different parameters x, y and metion the linestyle as dashed. - Finally, use the
**show()**method to display the graph.

Read: Matplotlib scatter marker

## Matplotlib dashed line width

We can easily customize the linestyle dashed in matplotlib. By defining the line width we can modify the thickness of the line. Or we can say that we can increase or decrease the width of the line. By default, we specify the line width size in pixels.

**The syntax for the above is given below:**

**#Import Libraries**
import numpy as np
import matplotlib.pyplot as plt
**# Data to be plotted**
x = np.arange(5)
y1 = [1, 2, 3, 4, 5]
y2 = [1, 8, 27, 64, 125]
**# Function to plot**
plt.plot(x, y1, '--', linewidth = 8, label ='Numbers')
plt.plot(x, y2, '--', linewidth = 4, label ='Cube of numbers')
**# Function add a legend **
plt.legend(loc='upper center')
**# Function to show the plot**
plt.show()

In the above example, by using the **plt.plot()** method, we plot the graph. And by adding additional parameter **linewidth**, we can change the thickness of the dashed lines.

Also, Read: Matplotlib remove tick labels

## Matplotlib dashed line errorbar

Matplotlib dashed lines have error bars To plot the confidence or precision in calculated values we use error bars.

**errorbar()** method is used to plot the error bars in the graph.

**The syntax for plotting the error bars are given below:**

`matplotlib.pyplot.errorbar(x, y, xerr=None, yeer=None, ...)`

**The following used parameters used above are described below:**

**x:**specifies the horizontal coordinates**y:**specifies the vertical coordinates**xeer:**array-like structure and specify error bar size in the horizontal direction.**yeer:**array-like structure and specify error bar size in the vertical direction.

**Let’s understand the concept with an example:**

**#Import Libraries**
import numpy as np
import matplotlib.pyplot as plt
**# Data to be plotted
**
x = np.arange(5)
y = [1, 2, 3, 4, 5]
**# Function to plot errorbars**
plt.errorbar(x, y, xerr = 0.3, linestyle='dashed')
**# Function to show the plot**
plt.show()

In the above example, we use **plt.errorbar()** method and plot the error bars in horizontal direction have a size of 0.3 and we use the line style as a dashed line.

Also, check: Draw vertical line matplotlib

In this Python tutorial, we have discussed the “**Matplotlib dashed line**” and we have also covered some examples related to it. There are the following topics that we have discussed in this tutorial.

- Matplotlib dashed line
- Matplotlib dashed line how to plot
- Matplotlib dashed line example
- Matplotlib dashed line styles
- Matplotlib dashed line colors
- Matplotlib dashed line with markers
- Matplotlib dashed line horizontal line
- Matplotlib dashed line vertical line
- Matplotlib dashed line spacing
- Matplotlib dashed line legend
- Matplotlib dashed line contour
- Matplotlib dashed line width
- Matplotlib dashed line errorbar

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.