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:
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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') # 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.