Skip to Content

Change Line Thickness in Matplotlib

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:

the resulting line plot

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:

line plot with thicker line

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:

curve plotted with custom thickness

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:

multiple curves with custom thickness

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 –


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


Author

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.