How to Change the Line Width of a Graph Plot in Matplotlib?

Matplotlib one of the libraries of python, which plays an important role in beautifying plots and making the data analysis and data visualization an easier task. You can use Matplotlib for experimenting, by using different options available in it and creating a more appealing, informative plot.

One common customization in Matplotlib is changing the line width of a graph plot. Since, line width controls the thickness of the lines, which are used in the plots at various points such as in connecting the plot points, etc.

In this article, we will be learning how to change the line width of a graph plot in Matplotlib by using different methods such as –

  • Using the linewidth parameter.

  • Using step() function.

  • Using the set_linewidth() method.

Before proceeding further, let us understand how to create line graph using Matplotlib –

Creating different graphs using Matplotlib

Using Matplotlib for creating a graph in Python, you have to import the Matplotlib library and the pyplot module, a sub-module of the matplotlib library.

After importing the module, you can call various functions for different styles and properties. It provides a convenient space for making various types of charts.

Different graphs such as bar graph, line graph, scatter graph, histogram can be created using ‘’, ‘plt.plot’, ‘plt.scatter’, ‘plt.hist’ functions respectively. These functions are included in creating the plots for displaying it you can use imshow() function.

For making changes to your plot there are various functions –

  • xlabel(): Used to set the labels of the x-axis

  • ylabel(): Used to set the labels of the y-axis.

  • title(): Used to set the title for the graph.

  • legend(): adds legend to the plot.

  • xlim() and ylim(): Used to setting up the limit.


Following is an example –

import matplotlib.pyplot as plt
x = [2,4,6,8,10]
y = [10,8,4,6,2]
plt.plot(x, y)



You can add different elements such as label, legend, title, etc. to this graph using different functions such as plt.xlabel, plt.legend(), plt.title, respectively.

import matplotlib.pyplot as plt
x = [2, 4, 6, 8, 10]
y = [3, 5, 7, 9, 1]
plt.plot(x, y)
plt.legend(['Line Chart'])


Now, let us see various ways to set the line width of a change the Line Width of a Graph Plot in Matplotlib.

Using the Linewidth Parameter

When we use the plot function in matplotlib we can change the width of the line in the graph by specifying the line width using the linewidth parameter.


Following is the example –

import matplotlib.pyplot as plt
x = [10, 15, 20, 25]
y = [30, 40, 50, 60]
plt.plot(x, y, label= 'legend example1', linewidth= '11')


Using the setp() Function

This is a general function which deals with different properties of Matplotlib which includes setting up the linewidth as well.


In the following example, we have applied different styles to the graph such as increasing the width, color of the plot.

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100, 1000)
y = np.cos(x)
line, = plt.plot(x, y)
plt.setp(line, linewidth=6, color= 'skyblue' )


Using set_linewidth Parameter

You can use the set_linewidth parameter while creating different types of graphs. Following is the example –


import matplotlib.pyplot as plt
import numpy as np
x1 = np.linspace(0, 10, 100)
y1 = np.sin(x1)
x2 = np.linspace(0, 10, 100)
y2 = np.cos(x2)
fig, ax = plt.subplots()
line, = ax.plot(x1, y1)
line, = ax.plot(x2, y2)
plt.legend(['sin', 'cos'])



In this article, we have briefly discussed about various methods which can be used to change the line width of a graph. We started from the basics of the Matplotlib library which is one of the most popular libraries for creating graphs. We discussed various functions which can be used to create and customize the graph. Due to the wide range of tools which Matplotlib library offers you can create variety of bar graphs, line plots,

Updated on: 11-Oct-2023


Kickstart Your Career

Get certified by completing the course

Get Started