In this tutorial, we’ll try to understand how to change the thickness of a line in a matplotlib plot with the help of some examples.
Using the linewidth
parameter
We can easily adjust the thickness of the line in matplotlib plots using the linewidth
argument to the matplotlib.pyplot.plot()
function.
Basic syntax:
matplotlib.pyplot.plot(x, y, linewidth=1.5)
When we generate a plot in matplotlib, it has a default linewidth of 1.
So, If we want to increase the line thickness we need to adjust the linewidth
parameter value to be greater than 1, and to decrease the line thickness we need to adjust the linewidth
parameter value to be less than 1.
Examples
Now let us see some examples to demonstrate the above argument function.
Example 1 – Changing the linewidth of a straight line
Let’s plot a line plot and see its width by default.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y1 = 1*x + 4 plt.plot(x, y1) plt.show()
Output:
We first import the required modules in the above code (numpy and matplotlib). Then, we create a set of x values using the numpy.linspace
method(more about the method here). And finally, we create the y values of the line y = (1*x) + 4
which forms a straight line.
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.
Let’s now replot the above chart but make the line thicker in the plot by using the linewidth
parameter.
# make the line thicker plt.plot(x, y1, linewidth=5) plt.show()
Output:
The line is now much thicker.
Example 2 – Changing the linewidth of a curve
We can similarly change the thickness of a curve in matplotlib using the linewidth
parameter.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y = np.sin(x)*np.exp(-x/3) plt.plot(x, y, linewidth=10) plt.show()
Output:
We first import the required modules in the above code (numpy and matplotlib). Then we create a set of x values using numpy.linspace
method(more about the method here). Then, we create the y values of the curve y = sinx*e^(-x/3)
which forms a curve. Note that we are specifying a custom linewidth for the curve here.
You can see that the resulting curve is thick.
Example 3 – Changing the linewidth of multiple curves
You can modify the linewidth of more than one line or curve. For this, specify the linewidth
when plotting the respective line/curve.
import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 10, 100) y1 = np.sin(x)*np.exp(-x/3) y2 = np.sin(x)*np.exp(-x/7) plt.plot(x, y1, linewidth=10) plt.plot(x, y2, linewidth=0.5) plt.show()
Output:
We first import the required modules in the above code (numpy and matplotlib). Then we create a set of x values using numpy.linspace
method. Then, we create the y1 values of the curve y = sinx*e^(-x/3) which forms a curve, and y2 values of the curve y = sinx*e^(-x/7)
which also forms a curve. Note that we specify different line widths for both curves.
You can see that the curves have the specified line widths.
You might also be interested in –
- How to remove the legend border (frame) in Matplotlib?
- How to change the legend position in Matplotlib?
- Matplotlib – Change Line to Dots
- Matplotlib – Add an Average Line to the Plot
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.