Step Guide to Plot Multiple Lines in Matplotlib – Matplotlib Tutorial

By | March 31, 2022

It is easy to plot a line in matplotlib, here is the tutorial:

Python Matplotlib Implement a Line Chart: A Completed Guide – Matplotlib Tutorial

How to draw multiple lines in a chart? In this tutorial, we will introduce you how to do.

Preliminary

We import some libraries first.

import numpy as np
import matplotlib.pyplot as plt

Plot some lines

We will use numpy to plot some line data.

lenx = 30
x = np.arange(lenx)
lines = 5
for i in range(lines):
    y = [10*np.random.rand()+t for t in x]
    plt.plot(x, y, label = 'line '+str(i+1))

plt.title("Plot Mutiple lines in Matplotlib", fontsize=13)
plt.xlabel("X", fontsize=13)
plt.ylabel("Y", fontsize=13)
plt.legend()
plt.show()

In this code, we will draw 5 lines in a chat. Run this code, you will see:

Step Guide to Plot Multiple Lines in Matplotlib - Matplotlib Tutorial

If you want to change line style, you can read this tutorial:

A Full List of Matplotlib Line Style: A Beginner Guide – Matplotlib Tutorial

Leave a Reply