Skip to Content

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)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

the resulting line chart which is a continuous line without any markers or dots

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='.')

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

line chart with dots as markers

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')

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

line chart with customized markers

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')

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

line chart with dotted line

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)

# add axes labels
plt.xlabel('Year')
plt.ylabel('Crude oil price USD/barrel')

# add plot title
plt.title("Crude oil prices in 2010s")

Output:

line chart with dotted line which is thicker

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush

    Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.