How Can We Help?
In this article, we will learn to plot multiple graphs in python. It is often needed to plot multiple graphs when you want to visualize the data. Graphs are handy for a clear understanding of the data you have. What are we going to see in this post? We want to plot multiple graphs in a single plot. We can do this using the matplotlib python library. There are two effective ways to plot multiple graphs in a single plot by using the matplotlib library.
- Using subplot() function of Matplotlib library.
- Superimposing one graph to another graph will help us visualize both the graph in a single plot.
We will look at some crucial ways to plot multiple graphs in python, like
- How to create Multiple Matplotlib plots in one figure.
- How to plot multiple graphs in one figure legend in python.
- How to plot two graphs with different scales in python.
- How to plot multiple graphs in python on the same plot.
How to Create Multiple Matplotlib plots in one Figure
In the Matplotlib library of python, the subplot() function is a wrapper function that enables the programmer to plot multiple graphs in a single plot. Let us see about this function more.
Syntax
matplotlib.pyplot.subplots(numrows, numcols, plot_number)
To plot multiple graphs in a single plot, follow the steps mentioned below.
- 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
- 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).
- Now plot the graph one as
plt.subplot(121)
plt.plot(t, ‘r- -’)
plt.xlabel(‘Plot 1)
- Similarly, plot graph 2 as …
plt.subplot(122)
plt.plot(t, ‘r- -’, t**2, ‘b*’, t**3, ‘g-o’)
plt.xlabel(‘Plot 2)
- 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
Explanation
- Here we have imported matplotlib and NumPy. Matplotlib plots the graphs, and NumPy sets the time for plotting the graphs.
- Then we are using the subplot() function. The subplot function helps plot 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 Plot 1).
Check This: in case you want to know more about how to change Matplotlib Plot colors and what are different colors available in python matplotlib library that you can use.
How to plot multiple graphs in python on the same plot
We have understood plotting multiple graphs using the subplot() function of the matplotlib library. Now let us know how to plot multiple graphs in one plot by superimposing them. We can achieve this by directly plotting the graphs one by one. Look at the example code below.
Example Syntax
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
Explanation
So here, we can see that we are plotting the graphs one by one, and all the graphs can be seen in a single plot. So this is one other 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? A Legend is an area of a graph describing each of the parts of that graph. So the legend is nothing but the part of the graph which explains each part of that graph. 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
- Legend without labels and handles
- Legend with labels only
- Legend with both labels and handles
Let us look at all three types of legends using examples.
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
Explanation
So if you don’t mention any argument in the legend function, it will automatically detect the labels from the plot function. So you must include labels in your plot function to get detected by the legend() function; if you don’t mention labels in the plot, the legend will not work.
The legend function displays all plots that have been labeled with keyword labels. The order of the lines in the legend is the same as the order you plot them.
Legend with labels only
Suppose we don’t want to label our plot while plotting. We can ignore writing labels inside the plot function, and we can pass labels in the legend function respectively the plots are 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
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
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.
How to plot two graphs with different scales in python
We can also plot two graphs on different scales in python easily. To get more understanding, look at the example code below:
Example Syntax
import numpy as np
import matplotlib.pyplot as plt
t = np.array([1,2,3,4])
ld = [5, 4, 3, 2, 1]
rd = [0.1, 0.2, 0.4, 0.8, 1.6]
f, al = plt.subplots()
ar = al.twinx()
al.plot(ld, color='black')
ar.plot(rd, color='red')
plt.title("Plotting in different scales")
plt.show()
Output
Conclusion
In this article, we have seen different ways to plot multiple graphs in python. Most of the methods are quick and very simple to understand. Make sure to end your code with the show() function to get the output graph on your screen.