0
0
Matplotlibdata~5 mins

Figure creation with plt.figure in Matplotlib

Choose your learning style9 modes available
Introduction

We use plt.figure to create a new drawing space for our charts. It helps us organize and control how our plots look.

When you want to make multiple separate charts in one program.
When you need to set the size or resolution of a plot before drawing.
When you want to clear old plots and start fresh with a new figure.
When you want to save a plot with specific dimensions or quality.
When you want to add multiple plots side by side in different figures.
Syntax
Matplotlib
plt.figure(num=None, figsize=None, dpi=None, facecolor=None, edgecolor=None, tight_layout=False)

num gives the figure a number or name to identify it.

figsize sets the width and height in inches (like size of paper).

Examples
Creates a new default figure with default size and settings.
Matplotlib
plt.figure()
Creates a figure 8 inches wide and 6 inches tall.
Matplotlib
plt.figure(figsize=(8, 6))
Creates or selects figure number 2 with 100 dots per inch resolution.
Matplotlib
plt.figure(num=2, dpi=100)
Sample Program

This code creates a new figure with a size of 6 by 4 inches, draws a simple line plot, adds a title, and then shows the plot.

Matplotlib
import matplotlib.pyplot as plt

# Create a figure with size 6x4 inches
plt.figure(figsize=(6, 4))

# Plot a simple line
plt.plot([1, 2, 3], [4, 5, 6])

# Add title
plt.title('Simple Line Plot')

# Show the plot
plt.show()
OutputSuccess
Important Notes

Calling plt.figure() without arguments creates a new figure with default size.

You can create multiple figures by calling plt.figure() multiple times with different num or figsize.

Use plt.show() to display all created figures.

Summary

plt.figure creates a new drawing space for your plots.

You can control size and resolution with figsize and dpi.

Use it to organize multiple plots or customize plot appearance before drawing.