Matplotlib - Figures



In Matplotlib library a figure is the top-level container that holds all elements of a plot or visualization. It can be thought of as the window or canvas where plots are created. A single figure can contain multiple subplots i.e. axes, titles, labels, legends and other elements. The figure() function in Matplotlib is used to create a new figure.

Syntax

The below is the syntax and parameters of the figure() method.

plt.figure(figsize=(width, height), dpi=resolution)

Where,

  • figsize=(width, height) − Specifies the width and height of the figure in inches. This parameter is optional.

  • dpi=resolution − Sets the resolution or dots per inch for the figure. Optional and by default is 100.

Creating a Figure

To create the Figure by using the figure() method we have to pass the figure size and resolution values as the input parameters.

Example

import matplotlib.pyplot as plt
# Create a figure
plt.figure(figsize=(3,3), dpi=100)
plt.show()

Output

Figure size 300x300 with 0 Axes

Adding Plots to a Figure

After creating a figure we can add plots or subplots (axes) within that figure using various plot() or subplot() functions in Matplotlib.

Example

import matplotlib.pyplot as plt
# Create a figure
plt.figure(figsize=(3, 3), dpi=100)
x = [12,4,56,77]
y = [23,5,7,21]
plt.plot(x,y)
plt.show()

Output

Adding plot

Displaying and Customizing Figures

To display and customize the figures we have the functions plt.show(),plt.title(), plt.xlabel(), plt.ylabel(), plt.legend().

plt.show()

This function displays the figure with all the added plots and elements.

Customization

We can perform customizations such as adding titles, labels, legends and other elements to the figure using functions like plt.title(), plt.xlabel(), plt.ylabel(), plt.legend() etc.

Example

In this example we are using the figure() method of the pyplot module by passing the figsize as (8,6) and dpi as 100 to create a figure with a line plot and includes customization options such as a title, labels and a legend. Figures can contain multiple plots or subplots allowing for complex visualizations within a single window.

import matplotlib.pyplot as plt
# Data
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]

# Create a figure
plt.figure(figsize=(8, 6), dpi=100)

# Add a line plot to the figure
plt.plot(x, y, label='Line Plot')

# Customize the plot
plt.title('Figure with Line Plot')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()

# Display the figure
plt.show()
Output
Figure

Example

Here this is another example of using the figure() method of the pyplot module to create the subplots.

import matplotlib.pyplot as plt
# Create a figure
plt.figure(figsize=(8, 6))

# Add plots or subplots within the figure
plt.plot([1, 2, 3], [2, 4, 6], label='Line 1')
plt.scatter([1, 2, 3], [3, 5, 7], label='Points')

# Customize the figure
plt.title('Example Figure')
plt.xlabel('X-axis')
plt.ylabel('Y-axis')
plt.legend()

# Display the figure
plt.show()
Output
Subplot
Advertisements