Matplotlib line thickness

In this article, we are going to discuss how to use Python to modify the thickness of a line of the chart in matplotlib. Whenever we construct a line plot, matplotlib will use the default line width value, which is 1. But this value can be changed. The linewidth parameter in Matplotlib enables us to change the thickness of a line in the chart.

When we want to specify a chart’s line width narrower, we will always use a value less than 1. And when we want the plot’s line width to be wider, we could define linewidth as larger than 1.

Change the Thickness of several lines by using for loop:

We utilize for loop to modify the line width of many lines. The subsequent code demonstrates how to change the width of numerous lines at the same time:

import matplotlib.pyplot as plt
import numpy as np
fig= plt.figure()
axes= fig.add_axes([1,1,2,2])
x= np.arange(0,15)
axes.plot(x,x**2, color='green', linewidth=50)

Here we have to integrate matplotlib.pyplot as plt library for graphic visualizations, and also we integrate Numpy as np for mathematical analysis of the data. Now we create the data sets by using the NumPy library.

To store the data sets of the x-axis and y-axis, we declare two variables. For defining the data points of the x-axis, we call the np.linspace() method. And similarly, for defining the data points of the y-axis, we apply the np.sin() function. We declare for loop here.

A new variable is created and initialized to specify the range. In addition to this, we apply plt.plot() to draw the plot. The data sets of the x-axis and y-axis are provided as an argument for this function. After this, we also specified the line width for the lines.

Here we utilize the short form ‘lw’ of the line-width. At the end of the program, we call function to represent the graph.

Adjust the line width of one line:

Let’s see a simple example of how to draw a graph and specify the thickness of one line of any graph.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(10, 100, 2000)
y = np.sin(x)

for i in range(30):
    plt.plot(x, y + i*1.5, lw=i*0.7)

In this program, firstly, we have to import the packages, including matplotlib.pyplot as plt and NumPy as np. In the subsequent step, we state a variable termed ‘fig.’ The function plt.figure() is specified to the ‘fig’ variables. This function generates an item, and that item is initially empty since we are not providing any parameter to it.

Further, we insert the x and y-axis to this graph by calling the function fig.add_axes() function. We define x dimensions, which range from 0 to 15. For this purpose we call the function np.arrange(). We now draw the square of x dimensions by the use of axes.plot() function. This technique lets us adjust the graph’s line width. This can be done by providing the ‘linewidth’ argument to the plot() method.

In this scenario, we defined the width of the line as 50, providing the graph’s line with a thickness that is 50 times that of the usual line width. We also specified the color scheme of the thickness of the line by passing the parameter ‘color’ to the plot () function. Here we specify the color of the line to ‘green.’ We display the plot by using the () function.

We obtain this type of graph after executing the above code. The thickness of the line is set to be 50, as displayed in the above output.

Specify the line thickness and apply Legends:

This step demonstrates how to construct numerous lines of varying thicknesses, as well as a label that indicates the width of every line.

import matplotlib.pyplot as plt
import numpy as np
l = np.linspace(10, 20, 200)
m = np.sin(l)*np.exp(-l/8)
n = np.cos(l)*np.exp(-l/10)
plt.plot(l, m, linewidth=10, label='first line')
plt.plot(l, n, linewidth=5, label='second line')

After including the matplotib and NumPy library, we create three variables ‘l’, ‘m’, and ‘n.’ Then; we declare the values for both the x-axis and y-axis by applying the NumPy package. Those three variables are used to store data sets. We have to draw two lines, so we call plt.plot() function respectively.

The plot() function holds four parameters. The values for the x-axis, y-axis, line width, and labels are provided for this function. Hence we create different lines and then specify the thickness of these lines. We defined the line width as 10 for the first line. And define the line width to 5 for the second line.

We also defined the labels that show the lines. To insert the label to every line, we have to call plt.legend() method. Similarly, we apply the function to show the plot.

Floating-point number of line width:

We can set any floating-point number to the line width. We would utilize the parameter ‘linewidth’ to adjust the thickness of the line.,

import matplotlib.pyplot as plt
import numpy as np
a = np.array([21, 11, 10, 17])
plt.plot(a, linewidth = '32.6')

In this instance, we introduce the matplotlib and NumPy modules. We defined the data set for only the y-axis here using the np.array() function. We declare the array containing different values. This array is stored in the ‘a’ variable.

Now we apply the plot() function. Here we provided a dataset of the y-axis as a function parameter. Similarly, we specify the floating-point value to the ‘linewidth’ argument of the function. Now we get the figure by calling the method.

By running the above code, we get a figure with a 32.6 wide line.


In this article, we examined the method of changing the line width of the plot with several examples. We can set floating-point numbers to the line width of the graphs. We utilize for loop to adjust the line width of different lines. We modify the line width as well as apply labels to the lines.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content