Plot a dotted line using matplotlib in Python

Hello friends, you can plot a line using matplotlib in Python. In this tutorial, I will tell you how to plot a dotted line using matplotlib in Python.

Plot a dotted line using matplotlib in Python

I have imported the following dependencies in my code to enable me to plot a line.

import matplotlib.pyplot as plt

After importing the library, it’s important to provide some 2-D points. Here I have declared two lists x_axis and y_axis. The list should be of the same length. To plot a line I have used the plot() function. I have passed my two lists as x and y values. Along with these two attributes you need to add another attribute, linestyle. This attribute takes a string value and specifies which kind of line you desire. You can specify any of the kinds, mentioned in this list. In this code snippet, I have given the value as ‘dotted’, as I require a dotted line plot.

Code :

x_axis = [12, 9, 8, 4, 8, 10]
y_axis = [4, 5, 0, -1, 2, -2]

plt.plot(x_axis, y_axis, linestyle = "dotted")

Output :

Plot a dotted line using matplotlib in Python

Now you can plot a dotted line with ease.

Leave a Reply

Your email address will not be published. Required fields are marked *