- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Plot a rectangle with an edgecolor in Matplotlib
To put edgecolor of a rectangle in matplotlib, we can take the following Steps −
Create a new figure or activate an existing figure using figure() method.
Add a subplot method to the current axis.
Create a rectangle instance using Rectangle() class with an edgecolor and linewidth of the edge.
Add a rectangle path on the plot.
To place the text in the rectangle, we can use text() method.
Scale x and y axes using xlim() and ylim() methods.
To display the figure, use show() method.
Example
import matplotlib from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True fig = plt.figure() ax = fig.add_subplot(111) rect = matplotlib.patches.Rectangle((-1, -1), 3, 3, edgecolor='orange', facecolor="green", linewidth=5) ax.add_patch(rect) plt.text(0, 0.5, "Rectangle") plt.xlim([-4, 4]) plt.ylim([-4, 4]) plt.show()
Output
- Related Articles
- Plot a circle with an edgecolor in Matplotlib
- Plot a circle with an edgecolor and hatch in Matplotlib
- Plot a circle inside a rectangle in Matplotlib
- How to plot a rectangle inside a circle in Matplotlib?
- How to rotate the rectangle patch in a plot using Matplotlib?
- How to plot a rectangle on a datetime axis using Matplotlib?
- Drawing a rectangle with only border in Matplotlib
- Line plot with arrows in Matplotlib
- How to set different opacity of edgecolor and facecolor of a patch in Matplotlib?
- Plot numpy datetime64 with Matplotlib
- Plot Matplotlib 3D plot_surface with contour plot projection
- How to plot a line in Matplotlib with an interval at each data point?
- How to create a Swarm Plot with Matplotlib?
- How to plot a Pandas Dataframe with Matplotlib?
- Matplotlib savefig with a legend outside the plot

Advertisements