- 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
How can I set the background color on specific areas of a Pyplot figure using Matplotlib?
To set the background color on specific areas of a pyplot, we can take the following steps −
Using subplots() method, create a figure and a set of subplots, where nrows=1.
Using rectangle, we can create a rectangle, defined via an anchor point and its width and height. Where, edgecolor=orange, linewidth=7, and facecolor=green.
To plot a diagram over the axis, we can create a line using plot() method, where line color is red.
To color a specific portion of the plot, add a rectangle patch on the diagram using add_patch() method.
To display the figure, use show() method.
Example
from matplotlib import pyplot as plt, patches plt.rcParams["figure.figsize"] = [7.00, 3.50] plt.rcParams["figure.autolayout"] = True figure, ax = plt.subplots(1) rectangle = patches.Rectangle((2, 4), 3, 3, edgecolor='orange', facecolor="green", linewidth=7) ax.plot([2, 4, 5, 1, 4, 8, 0], c='red') ax.add_patch(rectangle) plt.show()
Output
- Related Articles
- How can I attach a pyplot function to a figure instance? (Matplotlib)
- How can I get the color of the last figure in Matplotlib?
- How can I get pyplot images to show on a console app? (Matplotlib)
- How to set the background color of a column in a matplotlib table?
- How can we set the background color to a JPanel in Java?
- How to change background-color on a specific wider viewport in CSS ?
- How to set the background color of selection of Ellipse using FabricJS?
- How can we set a background color to JSplitPane in Java?
- How do I set color to Rectangle in Matplotlib?
- How do I close all the open pyplot windows (Matplotlib)?
- How to set the background color of a ttk.Combobox in tkinter?
- How can we set the background/foreground color for individual column of a JTable in Java?
- How to set the margins of a Matplotlib figure?
- How can I programmatically select a specific subplot in Matplotlib?
- Set Max value for color bar on Seaborn heatmap using Matplotlib

Advertisements