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 –
- Add dots (markers) to a line plot without altering the line itself.
- 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:
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University

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:

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:

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:

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:

You might also be interested in –
- Matplotlib – Add an Average Line to the Plot
- How to make text italic in a Matplotlib plot?
- How to bold text in a Matplotlib plot?
- Matplotlib – Remove the frame without altering the ticks and the tick labels
- Add Title to Each Subplot in Matplotlib
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.