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 create an empty Figure with Matplotlib in Python?
Matplotlib is a powerful Python library used for data visualization and creating 2D plots. It provides various tools for creating static, animated, and interactive plots, including line plots, scatter plots, bar plots, histograms, etc. Creating an empty figure is often the first step when building custom visualizations or when you need a blank canvas to work with.
An empty figure in Matplotlib is essentially a blank plot window without any data, axes, or visual elements. This can be useful for creating custom layouts or when you want to add elements programmatically.
Basic Syntax
To create an empty figure in Matplotlib, you use the following syntax ?
import matplotlib.pyplot as plt plt.figure(figsize=(width, height)) plt.show()
The figsize parameter is optional and specifies the width and height of the figure in inches. The default size is (6.4, 4.8).
Creating a Simple Empty Figure
Here's how to create a basic empty figure using the plt.figure() function ?
import matplotlib.pyplot as plt # Create an empty figure fig = plt.figure() plt.show()
This creates an empty figure with default dimensions. The figure appears as a blank white canvas without any axes or data.
Creating an Empty Figure with Custom Size
You can specify custom dimensions for your empty figure using the figsize parameter ?
import matplotlib.pyplot as plt # Create an empty figure with custom size (width=8, height=6) fig = plt.figure(figsize=(8, 6)) plt.show()
The figsize parameter takes a tuple of (width, height) in inches, allowing you to control the aspect ratio and size of your figure.
Multiple Empty Figures
You can create multiple empty figures by calling plt.figure() multiple times ?
import matplotlib.pyplot as plt
# Create first empty figure
fig1 = plt.figure(figsize=(5, 4))
plt.title("Empty Figure 1")
plt.show()
# Create second empty figure
fig2 = plt.figure(figsize=(7, 5))
plt.title("Empty Figure 2")
plt.show()
Each call to plt.figure() creates a new figure window. Adding titles helps distinguish between multiple figures.
Using Figure Objects
You can work with figure objects directly for more control ?
import matplotlib.pyplot as plt
# Create figure object
fig = plt.figure(figsize=(6, 4))
# Set figure properties
fig.suptitle("My Empty Figure")
fig.patch.set_facecolor('lightgray')
plt.show()
This approach gives you more control over figure properties like background color and titles.
Common Parameters
| Parameter | Description | Default |
|---|---|---|
figsize |
Width and height in inches | (6.4, 4.8) |
dpi |
Resolution in dots per inch | 100 |
facecolor |
Background color | 'white' |
Conclusion
Creating empty figures in Matplotlib is straightforward using plt.figure(). Use the figsize parameter to control dimensions and figure objects for advanced customization. This foundation is essential for building more complex visualizations.
