- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Embedding small plots inside subplots in Matplotlib
To embed small plots inside subplots, we can take the following Steps −
Using subplots() method, create a figure and a set of subplots (fig, ax1).
On ax1, plot a line with color red, line width=4, label=”outer plot”.
Using add_axes(), add an axis, i.e., ax2 with l, b, h and w values.
Plot the same points (Step 2) usins the plot() method, with color green, line width=3, label=”inside plot”.
Set the legend on both the plots using the legend() method.
To display the figure, use the show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True fig, ax1 = plt.subplots() ax1.plot([1, 4, 6, 2, 1, 5, 2], c="red", lw=4, label="outer plot") l, b, h, w = .6, .75, .1, .2 ax2 = fig.add_axes([l, b, w, h]) ax2.plot([1, 4, 6, 2, 1, 5, 2], color='green', lw=3, label="inside plot") ax1.legend(loc='upper left') ax2.legend(loc='upper left') plt.show()
Output
- Related Articles
- Manipulation on vertical space in Matplotlib subplots
- Manipulation on horizontal space in Matplotlib subplots
- Draw a border around subplots in Matplotlib
- Embedding a matplotlib animation into a tkinter frame
- How to zoom subplots together in Matplotlib/Pyplot?
- Plotting grids across the subplots in Python Matplotlib
- How to reuse plots in Matplotlib?
- Animation using Matplotlib with subplots and ArtistAnimation
- Fixing color in scatter plots in Matplotlib
- Row and column headers in Matplotlib's subplots
- How to add annotations in Matplotlib Plots?
- Logscale plots with zero values in Matplotlib
- Drawing lines between two plots in Matplotlib
- Adjusting the heights of individual subplots in Matplotlib in Python
- Setting the same axis limits for all subplots in Matplotlib

Advertisements