# Matplotlib – Change Line to Dots

In this tutorial, we will look at how to change the line to dots in a matplotlib plot with the help of some examples.

Depending on what you mean by changing the line to dots, you may be looking for one of the following two tasks at hand –

1. Add dots (markers) to a line plot without altering the line itself.
2. Change the style of the line from a continuous line to a dotted one.

Let’s now check out both use cases with some examples.

First, let’s create a simple line plot and see what it looks like by default.

```import matplotlib.pyplot as plt

# x values - years
x = [2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020]
# y values - curde oil price per barrel in USD
y = [94.88, 94.05, 97.98, 93.17, 48.66, 43.29, 50.80, 65.23, 56.99, 39.68]

# plot x and y on a line plot
plt.plot(x, y)

plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

plt.title("Crude oil prices in 2010s")```

Output:

Here we created a line plot of the crude oil prices in USD per barrel from 2011 to 2020. Note that the above plot does not have any markers.

## Example 1 – Add dots (markers) on a line plot

Let’s plot the above line chart again but this time with dots (markers) for each point on the plot. You can specify the marker to use with the `marker` keyword parameter in the `matplotlib.pyplot.plot()` function. Let’s add a dot, `.` as the marker.

```# plot x and y on a line plot with markers
plt.plot(x, y, marker='.')

plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

plt.title("Crude oil prices in 2010s")```

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

You can use different markers, for example, a star, *, a circle, o, etc. You can also customize other marker properties such as color, size, etc.

Let’s replot the above chart with red markers of the star shape and larger size.

```# plot x and y on a line plot with markers
plt.plot(x, y, marker='*', markerfacecolor='r', markersize='12')

plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

plt.title("Crude oil prices in 2010s")```

Output:

## Example 2 – Change line style to dotted

If, on the other hand, you want to change the line from a continuous line to a dotted one, use the `linestyle` parameter when plotting the line with `matplotlib.pyplot.plot()` function.

```# plot x and y on a line plot
plt.plot(x, y, linestyle='dotted')

plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

plt.title("Crude oil prices in 2010s")```

Output:

The line is now dotted.

You can also customize the line with additional parameters. For example, use the `linewidth` parameter to change the width of the line.

```# plot x and y on a line plot
plt.plot(x, y, linestyle='dotted', linewidth=4)

plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

plt.title("Crude oil prices in 2010s")```

Output:

You might also be interested in –