Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Matplotlib Subplot



Creating multiple subplots in a single figure is a common requirement in data visualization. Matplotlib provides the plt.subplots() function, which makes it easy to create a grid of subplots. Here’s a comprehensive guide on how to create and customize subplots using Matplotlib.

Basic Usage of plt.subplots()

The plt.subplots() function creates a figure and a set of subplots. Here’s a basic example:



        import matplotlib.pyplot as plt
        import numpy as np
        
        # Data
        x = np.linspace(0, 10, 100)
        y1 = np.sin(x)
        y2 = np.cos(x)
        
        # Create a figure and a set of subplots
        fig, axs = plt.subplots(2)
        
        # First subplot
        axs[0].plot(x, y1)
        axs[0].set_title('Sine Function')
        
        # Second subplot
        axs[1].plot(x, y2)
        axs[1].set_title('Cosine Function')
        
        # Show the plot
        plt.tight_layout()  # Adjusts spacing to prevent clipping of titles
        plt.show()
      

Specifying the Layout

You can specify the number of rows and columns in the subplot grid:



        # Create a 2x2 grid of subplots
        fig, axs = plt.subplots(2, 2)
        
        # Data
        x = np.linspace(0, 10, 100)
        y1 = np.sin(x)
        y2 = np.cos(x)
        y3 = np.tan(x)
        y4 = np.exp(x)
        
        # First subplot
        axs[0, 0].plot(x, y1)
        axs[0, 0].set_title('Sine')
        
        # Second subplot
        axs[0, 1].plot(x, y2)
        axs[0, 1].set_title('Cosine')
        
        # Third subplot
        axs[1, 0].plot(x, y3)
        axs[1, 0].set_title('Tangent')
        
        # Fourth subplot
        axs[1, 1].plot(x, y4)
        axs[1, 1].set_title('Exponential')
        
        # Show the plot
        plt.tight_layout()
        plt.show()
      

Sharing Axes

You can share the x or y axis across multiple subplots using the sharex or sharey parameter:



        # Create a figure with shared x-axis
        fig, axs = plt.subplots(2, sharex=True)
        
        # Data
        x = np.linspace(0, 10, 100)
        y1 = np.sin(x)
        y2 = np.cos(x)
        
        # First subplot
        axs[0].plot(x, y1)
        axs[0].set_title('Sine')
        
        # Second subplot
        axs[1].plot(x, y2)
        axs[1].set_title('Cosine')
        
        # Set labels for the shared x-axis
        plt.xlabel('X-axis')
        plt.tight_layout()
        plt.show()
      

Customizing Subplots

You can customize individual subplots by accessing them through the array of axes returned by plt.subplots()



        # Create a figure and subplots
        fig, axs = plt.subplots(2, 2)
        
        # Data
        x = np.linspace(0, 10, 100)
        y1 = np.sin(x)
        y2 = np.cos(x)
        y3 = np.tan(x)
        y4 = np.exp(x)
        
        # Customize each subplot
        axs[0, 0].plot(x, y1, color='red')
        axs[0, 0].set_title('Sine')
        
        axs[0, 1].plot(x, y2, color='blue')
        axs[0, 1].set_title('Cosine')
        
        axs[1, 0].plot(x, y3, color='green')
        axs[1, 0].set_title('Tangent')
        
        axs[1, 1].plot(x, y4, color='purple')
        axs[1, 1].set_title('Exponential')
        
        # Adjust layout
        plt.tight_layout()
        plt.show()
      

Advanced Example

Here's a more comprehensive example that includes various customizations:



        import matplotlib.pyplot as plt
        import numpy as np
        
        # Data
        x = np.linspace(0, 10, 100)
        y1 = np.sin(x)
        y2 = np.cos(x)
        y3 = np.tan(x)
        y4 = np.exp(x)
        
        # Create a figure and subplots
        fig, axs = plt.subplots(2, 2, figsize=(10, 8))
        
        # First subplot
        axs[0, 0].plot(x, y1, 'r-', label='sin(x)')
        axs[0, 0].set_title('Sine')
        axs[0, 0].set_xlabel('x')
        axs[0, 0].set_ylabel('y')
        axs[0, 0].legend()
        axs[0, 0].grid(True)
        
        # Second subplot
        axs[0, 1].plot(x, y2, 'b-', label='cos(x)')
        axs[0, 1].set_title('Cosine')
        axs[0, 1].set_xlabel('x')
        axs[0, 1].set_ylabel('y')
        axs[0, 1].legend()
        axs[0, 1].grid(True)
        
        # Third subplot
        axs[1, 0].plot(x, y3, 'g-', label='tan(x)')
        axs[1, 0].set_title('Tangent')
        axs[1, 0].set_xlabel('x')
        axs[1, 0].set_ylabel('y')
        axs[1, 0].legend()
        axs[1, 0].grid(True)
        
        # Fourth subplot
        axs[1, 1].plot(x, y4, 'm-', label='exp(x)')
        axs[1, 1].set_title('Exponential')
        axs[1, 1].set_xlabel('x')
        axs[1, 1].set_ylabel('y')
        axs[1, 1].legend()
        axs[1, 1].grid(True)
        
        # Adjust layout
        plt.tight_layout()
        plt.show()
      

Using subplot2grid

For more complex layouts, you can use subplot2grid to place subplots in specific grid positions.



        # Create a figure
        fig = plt.figure(figsize=(10, 8))
        
        # Create subplots using subplot2grid
        ax1 = plt.subplot2grid((3, 3), (0, 0), colspan=3)
        ax2 = plt.subplot2grid((3, 3), (1, 0), colspan=2)
        ax3 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)
        ax4 = plt.subplot2grid((3, 3), (2, 0))
        ax5 = plt.subplot2grid((3, 3), (2, 1))
        
        # Data
        x = np.linspace(0, 10, 100)
        y = np.sin(x)
        
        # Plot data
        ax1.plot(x, y)
        ax1.set_title('Plot 1')
        
        ax2.plot(x, y)
        ax2.set_title('Plot 2')
        
        ax3.plot(x, y)
        ax3.set_title('Plot 3')
        
        ax4.plot(x, y)
        ax4.set_title('Plot 4')
        
        ax5.plot(x, y)
        ax5.set_title('Plot 5')
        
        # Adjust layout
        plt.tight_layout()
        plt.show()
      




Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

Zava

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

Zava