Open In App

How to Create Subplots in Seaborn?

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisites: Matplotlib, Seaborn

In this article, we will explore how to create a subplot or multi-dimensional plot in seaborn, It is a useful approach to draw subplot instances of the same plot on different subsets of your dataset. It allows a viewer to quickly extract a large amount of data about complex information. 

 Create Subplots in Seaborn

Example 1: Here, we are Initializing the grid without arguments returns a Figure and a single Axes.

Python3




import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
 
figure, axes = plt.subplots()
figure.suptitle('Geeksforgeeks - one axes with no data')


Output:

Example 2: In this example we create a plot with 1 row and 2 columns, still no data passed i.e. nrows and ncols. If given in this order, we don’t need to type the arg names, just its values. 

  • figsize set the total dimension of our figure.
  • sharex and sharey are used to share one or both axes between the charts.

Python3




figure, axes = plt.subplots(1, 2, sharex=True,
                            figsize=(10, 5))
figure.suptitle('Geeksforgeeks')
axes[0].set_title('first chart with no data')
axes[1].set_title('second chart with no data')


Output:

Example 3: Creating subplot with many levels 

Python3




figure, axes = plt.subplots(3, 4, sharex=True,
                            figsize=(16, 8))
figure.suptitle('Geeksforgeeks - 3 x 4 axes with no data')


Output:

Example 4: A gridspec() is for a grid of rows and columns with some specified width and height space. The plt.GridSpec object does not create a plot by itself but it is simply a convenient interface that is recognized by the subplot() command.

Python3




import matplotlib.pyplot as plt
 
Grid_plot = plt.GridSpec(2, 3, wspace = 0.8,
                        hspace = 0.6)
 
plt.subplot(Grid_plot[0, 0])
plt.subplot(Grid_plot[0, 1:])
plt.subplot(Grid_plot[1, :2])
plt.subplot(Grid_plot[1, 2])


Output:

Example 4: Here we’ll create a 3×4 grid of subplot using subplots(), where all axes in the same row share their y-axis scale, and all axes in the same column share their x-axis scale

Python3




import matplotlib.pyplot as plt
 
figure, axes = plt.subplots(3, 4,
                            figsize=(15, 10))
 
figure.suptitle('Geeksforgeeks - 2 x 3 axes\
grid plot using subplots')


Output:



Last Updated : 16 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads