Multiple Plots
We have learned about the basic components of a graph that can be added so that it can convey more information. One method can be by calling the plot function again and again with a different set of values as shown in the above example. Now let’s see how to plot multiple graphs using some functions and also how to plot subplots.
Method 1: Using the add_axes() method
The add_axes() method is used to add axes to the figure. This is a method of figure class
Syntax:
add_axes(self, *args, **kwargs)
Example:
Python3
# Python program to show pyplot module import matplotlib.pyplot as plt from matplotlib.figure import Figure # initializing the data x = [ 10 , 20 , 30 , 40 ] y = [ 20 , 25 , 35 , 55 ] # Creating a new figure with width = 5 inches # and height = 4 inches fig = plt.figure(figsize = ( 5 , 4 )) # Creating first axes for the figure ax1 = fig.add_axes([ 0.1 , 0.1 , 0.8 , 0.8 ]) # Creating second axes for the figure ax2 = fig.add_axes([ 1 , 0.1 , 0.8 , 0.8 ]) # Adding the data to be plotted ax1.plot(x, y) ax2.plot(y, x) plt.show() |
Output:
Method 2: Using subplot() method.
This method adds another plot at the specified grid position in the current figure.
Syntax:
subplot(nrows, ncols, index, **kwargs)
subplot(pos, **kwargs)
subplot(ax)
Example:
Python3
import matplotlib.pyplot as plt # initializing the data x = [ 10 , 20 , 30 , 40 ] y = [ 20 , 25 , 35 , 55 ] # Creating figure object plt.figure() # adding first subplot plt.subplot( 121 ) plt.plot(x, y) # adding second subplot plt.subplot( 122 ) plt.plot(y, x) |
Output:
Method 3: Using subplots() method
This function is used to create figures and multiple subplots at the same time.
Syntax:
matplotlib.pyplot.subplots(nrows=1, ncols=1, sharex=False, sharey=False, squeeze=True, subplot_kw=None, gridspec_kw=None, **fig_kw)
Example:
Python3
import matplotlib.pyplot as plt # initializing the data x = [ 10 , 20 , 30 , 40 ] y = [ 20 , 25 , 35 , 55 ] # Creating the figure and subplots # according the argument passed fig, axes = plt.subplots( 1 , 2 ) # plotting the data in the # 1st subplot axes[ 0 ].plot(x, y) # plotting the data in the 1st # subplot only axes[ 0 ].plot(y, x) # plotting the data in the 2nd # subplot only axes[ 1 ].plot(x, y) |
Output:
Method 4: Using subplot2grid() method
This function creates axes object at a specified location inside a grid and also helps in spanning the axes object across multiple rows or columns. In simpler words, this function is used to create multiple charts within the same figure.
Syntax:
Plt.subplot2grid(shape, location, rowspan, colspan)
Example:
Python3
import matplotlib.pyplot as plt # initializing the data x = [ 10 , 20 , 30 , 40 ] y = [ 20 , 25 , 35 , 55 ] # adding the subplots axes1 = plt.subplot2grid ( ( 7 , 1 ), ( 0 , 0 ), rowspan = 2 , colspan = 1 ) axes2 = plt.subplot2grid ( ( 7 , 1 ), ( 2 , 0 ), rowspan = 2 , colspan = 1 ) # plotting the data axes1.plot(x, y) axes2.plot(y, x) |
Output:
Data Visualization using Matplotlib
Data Visualization is the process of presenting data in the form of graphs or charts. It helps to understand large and complex amounts of data very easily. It allows the decision-makers to make decisions very efficiently and also allows them in identifying new trends and patterns very easily. It is also used in high-level data analysis for Machine Learning and Exploratory Data Analysis (EDA). Data visualization can be done with various tools like Tableau, Power BI, Python.
In this article, we will discuss how to visualize data with the help of the Matplotlib library of Python.