Figure Class

<aside> 💡 class matplotlib.figure.Figure(figsize=None, dpi=None, facecolor=None, edgecolor=None, linewidth=0.0, frameon=None, subplotpars=None, tight_layout=None, constrained_layout=None)

</aside>

import matplotlib.pyplot as plt

# Initializing the data
x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

# Creating a new figure with width = 7 inches
# and height = 5 inches with face color as
# green, edgecolor as red and the line width
# of the edge as 7
fig = plt.figure(figsize =(7, 5), facecolor='g',
				edgecolor='b', linewidth=7)

# Creating a new axes for the figure
ax = fig.add_axes([1, 1, 1, 1])

# Adding the data to be plotted
ax.plot(x, y)

# Adding title to the plot
plt.title("Graphic Linear", fontsize=25, color="yellow")

# Adding label on the y-axis
plt.ylabel('Axis Y')

# Adding label on the x-axis
plt.xlabel('Axis X')

# Setting the limit of y-axis
plt.ylim(0, 80)

# Setting the labels of x-axis
plt.xticks(x, labels=["one", "two", "three", "four"])

# Adding legends
plt.legend(["GFG"])

plt.show()

Untitled

Axes Class

Axes class is the most fundamental unit and flexible to create sub-plot. Certain picture may contain a lot of axes, but certain axes only can be in a picture. Function axes() create axes object.

Syntax:

<aside> 💡 axes([left, bottom, width, height])

</aside>

import matplotlib.pyplot as plt
from matplotlib.figure import Figure

# Initializing the data
x = [10, 20, 30, 40]
y = [20, 25, 35, 55]

fig = plt.figure(figsize = (5, 4))

# Adding the axes to the figure
ax = fig.add_axes([1, 1, 1, 1])

# Plotting 1st dataset to the figure
ax1 = ax.plot(x, y)

# Plotting 2nd dataset to the figure
ax2 = ax.plot(y, x)

# Setting Title
ax.set_title("Graphic Linear")

# Setting Label
ax.set_xlabel("Axes X")
ax.set_ylabel("Axes Y")

# Adding Legends
ax.legend(labels = ('line 1', 'line 2'))

plt.show()

Untitled

Multiple Plot (Plot more than 1)

We have learned about fundamental graph component that can be added so we can convey the information further. One of the methods is by calling the plot function multiple times with set of values that is different as shown on the previous example above. Now let’s see how we are plotting a lot of graphics using some of function and also plotting the subplot.

Example 1

Example 2

Example 3