How to Plot Multiple Graphs in Python?

Python is a programming language with many features. It is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales.

Python is a versatile language when it comes to visualization. There are many libraries in the Python ecosystem that can be used to create static, animated, and interactive visualizations.

In this post, we will see how to use some of these Python visualization libraries in practice. We will take a look at how to create static and animated visualizations using the Matplotlib and seaborn libraries.

GET $15 OF FREE TUTORING WHEN YOU SIGN UP USING THIS LINK

How to Create Multiple Matplotlib plots in one Figure

The purpose of the subplot() function in the Matplotlib library of python is to provide a way for the programmer to display multiple graphs within a single plot. In the following paragraphs, we will take a closer look at this function and how it can be used.

Syntax

matplotlib.pyplot.subplots(numrows, numcols, plot_number)

To plot multiple graphs on one plot, follow these steps.

1. Install and import the matplotlib and NumPy library. Matplotlib library is used to plot graphs in python, and NumPy deals with all mathematical operations.

import matplotlib.pyplot as plt
import NumPy as np

2. Create an array of time using the np.arange() function of NumPy.

t=np.arange(0, 5, 0.2)

Here 0 is the start point, 5 is the endpoint, and 0.2 is the intervention between 0 and 5 (both inclusive).

3. Now plot the graph one as

plt.subplot(121)
plt.plot(t, ‘r- -’)
plt.xlabel(‘Plot 1)

4. Similarly, plot graph 2 as …

plt.subplot(122)
plt.plot(t, ‘r- -’, t**2, ‘b*’, t**3, ‘g-o’)
plt.xlabel(‘Plot 2)

5. Now show both the graph in one plot as…

plt. subtitle("Plotting multiple Graphs")
plt.show()

Full Syntax

import matplotlib.pyplot as plt
import numpy as np
t=np.arange(0, 5, 0.2)
 
plt.subplot(121)
plt.plot(t, "r--")
plt.xlabel("Graph 1")
 
plt.subplot(122)
plt.plot(t, "r--", t**2, "b+", t**3, "g-o")
plt.xlabel("Graph 1")
 
plt.suptitle("Plotting Multiple Graphs")
plt.show()

Output

Source: https://www.oraask.com/

Explanation

  • The purpose of importing matplotlib and NumPy is so that matplotlib can plot the graphs and NumPy can set the time for when the graphs are plotted.
  • Then we utilize the subplot() function. The subplot function assists in plotting two different graphs in one figure.
  • Here in this code example subplot() function has parameters 121 and 122; here, 1 defines the number of rows, and 2 defines the number of columns, as we discussed in the syntax of the subplot function. The 3rd parameter defines which one is which plot number, so 1 defines the 1st plot no, and 2 defines the 2nd plot no.
  • Then we are using the plot function inside which we are passing t, which is defined as time and the plot design.

here ‘r- -’ means:
r = red color
– – (double dash) = these are the patterns that are going to be printed on screen (See the output image)

How to plot multiple graphs in python on the same plot

We have previously learned how to plot multiple graphs using the subplot() function of the matplotlib library. Now, we will explore how to plot multiple graphs in one plot by superimposing them. We can do this by directly plotting the graphs one by one.

For an example, see the code below.

import matplotlib.pyplot as plt
import numpy as np
t=np.arange(0, 5, 0.2)

plt.plot(t, "r--")
plt.plot(t**2, "g*")

plt.xlabel("Time")
plt.suptitle("Superimposing both graphs in one")
plt.show()

Output

Source: https://www.oraask.com/

Explanation
As we can see, we are plotting the graphs one by one, and all the graphs can be seen in a single plot. This is another method in which we are superimposing the other graphs into one.

How to plot multiple graphs in one figure legend in python


What is a legend?

Legends are key in understanding graphs. They provide context for the different areas of the graph. Without a legend, a graph would be difficult to interpret.

A lot of time graph can be self-explanatory but having a title in the graph labels on the axis and a legend that explains more about the graph. This section will learn how to insert a legend into the plot. Basically legend function can be in three forms.

  1. Legend without labels and handles
  2. Legend with labels only
  3. Legend with both labels and handles

Legend without labels and handles

import numpy as np
import matplotlib.pyplot as plt
 
t = np.array([1,2,3,4])

plt.plot(t**2,t, color='red',label='squares')
plt.plot(t**3,t, color='green',label='cubes')

plt.title("Squares and Cubes")
plt.legend()
plt.show()

Output

Source: https://www.oraask.com/

Explanation
The labels included in the plot function will be automatically detected by the legend function if no argument is mentioned. Therefore, if you want the legend to work, you must make sure to include labels in the plot function.

The legend function displays all plots that have been labeled with keyword labels, and they are in the same order as when they were plotted.

Legend with labels only

We can choose not to label our plot as we are plotting by ignoring labels inside the plot function. However, we can add labels by passing them into the legend function after the plots have been plotted.

See the example below:

import numpy as np
import matplotlib.pyplot as plt
 
t = np.array([1,2,3,4])

plt.plot(t**2,t, color='red')
plt.plot(t**3,t, color='green')

plt.title("Squares and Cubes")
plt.legend([“squares”, “cubes”])
plt.show()

Output

Source: https://www.oraask.com/

Note: Make sure this order is crucial while manually writing the labels inside the legend. You must have ordered your label as same as the plots are plotted.

Legend with labels and handles

import numpy as np
import matplotlib.pyplot as plt
 
t = np.array([1,2,3,4])

list1, = plt.plot(t**2,t, color='red')
list2, = plt.plot(t**3,t, color='green')

plt.title("Squares and Cubes")
plt.legend([list1, list2],["squares","cubes"])
plt.show()

Output

Source: https://www.oraask.com/

Explanation
Here plot function will return a list of a single item, and we are taking that list item in variable list1 and list2 to unpack the list we are using comma(,). After that inside legend, we pass handles that are list names and then labels for that handles.