Change the width of the line in Matplotlib Python

In this tutorial, we will learn to change the width of the line in Matplotlib Python.

To install matplotlib library on your local machine, fire up the command prompt/terminal and write:

pip install matplotlib

Lets Code

Import matplotlib library. We use as keyword to make aliases for the library. This will make our code look clean and readable.

# Importing library
import matplotlib.pyplot as plt

Define data values

# Define data values
x = [8, 14, 22, 29, 35, 49, 52]
y = [5, 13, 19, 24, 33, 39, 41]
z = [2, 6 , 15, 22, 26, 34, 43]

plt.plot() function will take up coordinates (x,y,z coordinates) to plot lines. linewidth parameter is used to adjust width (thickness) of the line.

# Plot a simple line chart with linewidth of 2
plt.plot(x, y, 'black', linewidth=2)

# Plot another line on the same chart/graph with linewidth of 6
plt.plot(x, z, 'red', linewidth=6)

plt.show() will plot the lines

# Plot the lines
plt.show()

Python: Change the width of the line in Matplotlib

# Importing library
import matplotlib.pyplot as plt

# Define data values
x = [8, 14, 22, 29, 35, 49, 52]
y = [5, 13, 19, 24, 33, 39, 41]
z = [2, 6 , 15, 22, 26, 34, 43]

# Plot a simple line chart with linewidth of 2
plt.plot(x, y, 'black', linewidth=2)

# Plot another line on the same chart/graph with linewidth of 6
plt.plot(x, z, 'red', linewidth=6)

# Plot the lines
plt.show()

 

Output:

Change the width of the line in Matplotlib Python

 

We have changed the thickness of the lines using Matplotlib.

Leave a Reply

Your email address will not be published.