Plot Multiple lines in Matplotlib


Python provides a powerful library named Matplotlib that creates visual representations in the form of plots and graphs. One of the many features of this library is the ability to plot multiple lines in a single graph that are useful in comparing data sets or visualizing trends over time. We are going to explore the built-in method named 'plot()' that is used for plotting multiple lines in Python Matplotlib.

Python Program to Plot Multiple lines in Matplotlib

Before jumping to the program directly, let's familiarize ourselves with some basic concepts of Python that will help us better understand the code.

plot() method

This method is used to create various types of plots with different styles and formats. It takes one or more pairs of arguments that represent the x and y coordinates on the plot. By default, it plot line from one data point to another. But, we can draw other kinds of plots such as scatter plots, bar graphs and histograms by changing the format string or using additional arguments like color and line type.

Syntax

plot(x, y)

Here, x and y specifies the data points for x and y coordinates.

Numpy

It is the short form of Numerical Python that is used to perform scientific computation. It provides support for working with large, multi-dimensional arrays and matrices. We can easily integrate it with other Python libraries that use arrays, such as pandas and matplotlib.

Dictionary

It is an unordered and mutable collection of elements. It stores its elements in key-value pair, where each key is associated with a value. We can create a dictionary by placing elements in curly brackets '{ }' as 'key' : 'value' and separate each key-value pair by a comma.

Instance

Details = {"Tutor" : "Tutorialspoint", "Rank" : 01, "country" : "India"}

Example 1

The following example illustrates the use of 'plot()' method in plotting multiple lines.

Approach

  • Initialize three lines with the lists of data points.

  • Use the 'plot()' method to plot the values of x-coordinate against the values of y-coordinate.

  • Then, add some information about plot using 'title', 'legend', 'xlabel' and 'ylabel'.

  • Display the result using 'show()' method and exit.

import matplotlib.pyplot as plt
# Data points of line 1
x1 = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
# Data points of line 2
x2 = [1, 2, 3, 4, 5]
y2 = [1, 3, 5, 7, 9]
# Data points of line 3
x3 = [1, 2, 3, 4, 5]
y3 = [5, 4, 3, 2, 1]
# Plotting all lines with specifying labels
plt.plot(x1, y1, label='Line 1')
plt.plot(x2, y2, label='Line 2')
plt.plot(x3, y3, label='Line 3')
# Adding legend, x and y labels, and titles for the lines
plt.legend()
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Plotting Multiple Lines')
# Displaying the plot
plt.show()

Output

Example 2

In this example, we will plot multiple lines with the help of numpy and plot() method. In the same code of previous example, we will use the numpy array instead of list.

import numpy as np
import matplotlib.pyplot as plt
# Data points of line 1
x1 = np.array([1, 2, 3, 4, 5])
y1 = np.array([2, 4, 6, 8, 10])
# Data points of line 2
x2 = np.array([2, 3, 4, 5, 6])
y2 = np.array([1, 3, 5, 7, 9])
# Data points of line 3
x3 = np.array([1, 2, 3, 4, 5])
y3 = np.array([5, 4, 3, 2, 1])
# Plotting all lines with labels
plt.plot(x1, y1, label='Line 1')
plt.plot(x2, y2, label='Line 2')
plt.plot(x3, y3, label='Line 3')
# Adding legend, x and y labels, and title for the lines
plt.legend()
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Line Plot')
# Displaying the plot
plt.show()

Output

Example 3

In the following example, we will plot multiple lines with the help of dictionary and plot() method.

import matplotlib.pyplot as plt
# Storing the data for line in a Dictionary
lines = {
   'Line 1': ([1, 2, 3, 4, 5], [4, 5, 6, 8, 10]),
   'Line 2': ([1, 2, 3, 4, 5], [1, 3, 5, 7, 9]),
   'Line 3': ([1, 2, 3, 4, 5], [5, 4, 3, 2, 1])
}
# Plotting the lines with labels
for label, (x, y) in lines.items():
   plt.plot(x, y, label = label)
# Adding legend, x and y labels, and title for the lines
plt.legend()
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.title('Multiple Line Plot')
# Displaying the plot
plt.show()

Output

Conclusion

We started this article by introducing Matplotlib library of Python and in later sections, we learned the basic concepts of 'plot()', numpy and dictionary to better understand the example programs. Using these concepts, we discussed how to plot multiple lines in a single graph.

Updated on: 21-Jul-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements