Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to add different graphs (as an inset) in another Python graph?
To add different graphs (as an inset) in another Python graph, we can use Matplotlib's add_axes() method to create a smaller subplot within the main plot. This technique is useful for showing detailed views or related data alongside the primary visualization.
Steps to Create an Inset Graph
Create x and y data points using NumPy
Using subplots() method, create a figure and a set of subplots
Add a new axis to the existing figure using add_axes()
Plot data on both the main axis and the inset axis
Use show() method to display the figure
Basic Example
Here's how to create a simple inset graph ?
import numpy as np
import matplotlib.pyplot as plt
# Set figure size
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create data
x = np.linspace(-1, 1, 100)
y = np.sin(x)
# Create main plot
fig, ax = plt.subplots()
# Define inset position: [left, bottom, width, height]
left, bottom, width, height = [0.30, 0.6, 0.2, 0.25]
ax_inset = fig.add_axes([left, bottom, width, height])
# Plot on main axis
ax.plot(x, y, color='red', label='Main plot')
ax.set_title('Main Graph with Inset')
ax.set_xlabel('X values')
ax.set_ylabel('Y values')
# Plot on inset axis
ax_inset.plot(x, y, color='green', linewidth=2)
ax_inset.set_title('Inset', fontsize=10)
plt.show()
Advanced Example with Different Data
You can also display different data in the inset, such as a zoomed-in view or a different function ?
import numpy as np
import matplotlib.pyplot as plt
# Create data
x = np.linspace(0, 10, 100)
y_main = np.cos(x)
y_inset = x**2 / 100 # Different function for inset
fig, ax = plt.subplots(figsize=(8, 5))
# Main plot
ax.plot(x, y_main, 'b-', linewidth=2, label='cos(x)')
ax.set_title('Main Plot: Cosine Function')
ax.set_xlabel('X')
ax.set_ylabel('cos(x)')
ax.grid(True, alpha=0.3)
# Create inset at different position
inset_ax = fig.add_axes([0.15, 0.15, 0.3, 0.3]) # Bottom-left corner
inset_ax.plot(x, y_inset, 'r-', linewidth=2)
inset_ax.set_title('Inset: x²/100', fontsize=10)
inset_ax.set_xlabel('X', fontsize=8)
inset_ax.set_ylabel('Y', fontsize=8)
inset_ax.tick_params(labelsize=8)
plt.show()
Understanding add_axes() Parameters
The add_axes() method takes a list of four values: [left, bottom, width, height]. All values are in figure coordinates (0 to 1) ?
| Parameter | Description | Range |
|---|---|---|
left |
Horizontal position of left edge | 0.0 - 1.0 |
bottom |
Vertical position of bottom edge | 0.0 - 1.0 |
width |
Width of the inset | 0.0 - 1.0 |
height |
Height of the inset | 0.0 - 1.0 |
Multiple Insets Example
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-5, 5, 200)
y = np.exp(-x**2) # Gaussian function
fig, ax = plt.subplots(figsize=(10, 6))
# Main plot
ax.plot(x, y, 'purple', linewidth=2)
ax.set_title('Gaussian Function with Multiple Insets')
ax.set_xlabel('X')
ax.set_ylabel('exp(-x²)')
# First inset - top right
inset1 = fig.add_axes([0.65, 0.65, 0.25, 0.25])
inset1.plot(x, y, 'red', linewidth=1.5)
inset1.set_xlim(-1, 1) # Zoom in
inset1.set_title('Zoomed Center', fontsize=9)
# Second inset - bottom left
inset2 = fig.add_axes([0.15, 0.15, 0.25, 0.25])
inset2.plot(x, np.gradient(y), 'green', linewidth=1.5)
inset2.set_title('Derivative', fontsize=9)
plt.show()
Conclusion
Use fig.add_axes() to create inset graphs within your main plot. Specify position and size using figure coordinates (0-1). This technique is perfect for showing detailed views, different data, or supplementary information alongside your primary visualization.
