Skip to content
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS
Close
Beyond Knowledge Innovation

Beyond Knowledge Innovation

Where Data Unveils Possibilities

  • Home
  • AI & ML Insights
  • Machine Learning
    • Supervised Learning
      • Introduction
      • Regression
      • Classification
    • Unsupervised Learning
      • Introduction
      • Clustering
      • Association
      • Dimensionality Reduction
    • Reinforcement Learning
    • Generative AI
  • Knowledge Base
    • Introduction To Python
    • Introduction To Data
    • Introduction to EDA
  • References
HomeKnowledge BasePythonHow-to: stack up two plots using the subplot…
Python

How-to: stack up two plots using the subplot function

February 11, 2024February 11, 2024CEO 160 views

You can use the subplot function in Matplotlib to create multiple plots arranged in a grid. To put two plots on top of each other, you can use the following approach:

import matplotlib.pyplot as plt
import numpy as np

# Create data for the plots
x = np.linspace(0, 10, 100)
y1 = np.sin(x)
y2 = np.cos(x)

# Create a subplot with 2 rows and 1 column
# The first plot will go in the first row, and the second plot in the second row
plt.subplot(2, 1, 1)
plt.plot(x, y1, label='Sin(x)')
plt.title('Plot 1')

plt.subplot(2, 1, 2)
plt.plot(x, y2, label='Cos(x)')
plt.title('Plot 2')

# Adjust layout for better spacing
plt.tight_layout()

# Show the plots
plt.show()

In this example, plt.subplot(2, 1, 1) creates the first subplot in a 2-row, 1-column grid, and plt.subplot(2, 1, 2) creates the second subplot beneath the first one. You can adjust the number of rows and columns based on your layout requirements.

The tight_layout() function is used to automatically adjust the subplot parameters for better spacing. Adjust the title and other plot settings as needed for your specific use case.

Since histograms and boxplots are commonly used together, we can create a function that combines them for us:

def hist_and_boxplot(df, label):
  plt.subplots(2, 1, sharex=True, gridspec_kw={'height_ratios': [3,1]})

  plt.subplot(2, 1, 1)
  plt.title(f'Distribution of {label}')
  sns.histplot(data=df, x=label, kde=True)

  plt.subplot(2, 1, 2)
  sns.boxplot(data=df, x=label, vert=False, boxprops=dict(facecolor='lightblue'))

  plt.show()

hist_and_boxplot(df, 'Rating')
matplotlib, pandas, subplot

Post navigation

Previous Post
Previous post: What is Gaussian Distribution?
Next Post
Next post: How-to: save a Google Colab notebook as HTML

You Might Also Like

No image
Quantile-based discretization of continuous variables
April 29, 2024 Comments Off on Quantile-based discretization of continuous variables
No image
Get a random sample from your dataset
March 7, 2024 Comments Off on Get a random sample from your dataset
No image
How-to: give a specific sorting order to…
February 7, 2024 Comments Off on How-to: give a specific sorting order to categorical values
No image
How-to: clean a dataset
February 6, 2024 Comments Off on How-to: clean a dataset
No image
How-to: formatting options for floating-point numbers in…
February 2, 2024 Comments Off on How-to: formatting options for floating-point numbers in Pandas
  • Recent
  • Popular
  • Random
  • No image
    7 months ago Low-Rank Factorization
  • No image
    7 months ago Perturbation Test for a Regression Model
  • No image
    7 months ago Calibration Curve for Classification Models
  • No image
    March 15, 20240Single linkage hierarchical clustering
  • No image
    April 17, 20240XGBoost (eXtreme Gradient Boosting)
  • No image
    April 17, 20240Gradient Boosting
  • No image
    February 6, 2024How-to: cap/clip outliers in a column
  • No image
    March 11, 2024Finding the optimal number of clusters (k)…
  • No image
    March 7, 2024Visualizing the Decision Tree
  • Implementation (55)
    • EDA (4)
    • Neural Networks (10)
    • Supervised Learning (26)
      • Classification (17)
      • Linear Regression (8)
    • Unsupervised Learning (11)
      • Clustering (8)
      • Dimensionality Reduction (3)
  • Knowledge Base (44)
    • Python (27)
    • Statistics (6)
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Oct    

We are on

FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS

Subscribe

© 2025 Beyond Knowledge Innovation
FacebookTwitterLinkedinYouTubeGitHubSubscribeEmailRSS