- 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
Draw a border around subplots in Matplotlib
To draw a border around subplots in matplotlib, we can use a Rectangle patch on the subplots.
Steps
Set the figure size and adjust the padding between and around the subplots.
Add a subplot to the current figure using subplot(121).
Get the subplot axes.
Add a rectangle defined via an anchor point *xy* and its *width* and *height*.
Add a rectangle patch to the current subplot based on axis (Step 4).
Set whether the artist uses clipping.
Add a subplot to the current figure using subplot(122).
Set the title of the current subplot.
To display the figure, use show() method.
Example
from matplotlib import pyplot as plt plt.rcParams["figure.figsize"] = [7.50, 3.50] plt.rcParams["figure.autolayout"] = True sub = plt.subplot(121) ax = sub.axis() rec = plt.Rectangle((ax[0] - 0.7, ax[2] - 0.2), (ax[1] - ax[0]) + 1, (ax[3] - ax[2]) + 0.4, fill=False, lw=2, linestyle="dotted") rec = sub.add_patch(rec) rec.set_clip_on(False) plt.title("with border") sub = plt.subplot(122) plt.title("without border") plt.show()
Output
- Related Articles
- Draw a border around an undecorated JFrame in Java
- Embedding small plots inside subplots in Matplotlib
- Manipulation on vertical space in Matplotlib subplots
- Manipulation on horizontal space in Matplotlib subplots
- Set a border around navbar with CSS
- Populating Matplotlib subplots through a loop and a function
- Annotate Subplots in a Figure with A, B, C using Matplotlib
- Plotting grids across the subplots in Python Matplotlib
- How to zoom subplots together in Matplotlib/Pyplot?
- Animation using Matplotlib with subplots and ArtistAnimation
- How to put a border around a Frame in Python Tkinter?
- Row and column headers in Matplotlib's subplots
- Adjusting the heights of individual subplots in Matplotlib in Python
- How to make more than 10 subplots in a figure using Matplotlib?
- How to share secondary Y-axis between subplots in Matplotlib?

Advertisements