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.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
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.
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.