Saturday, October 11, 2025
HomeLanguagesMatplotlib.axes.Axes.set_figure() in Python

Matplotlib.axes.Axes.set_figure() in Python

Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.

matplotlib.axes.Axes.set_figure() Function

The Axes.set_figure() function in axes module of matplotlib library is used to set the Figure for this Axes.

Syntax: Axes.set_figure(self, fig)

Parameters: This method accepts only one parameter.

  • fig : This parameter is the Figure instance.

Returns: This method does not return any value.

Below examples illustrate the matplotlib.axes.Axes.set_figure() function in matplotlib.axes:

Example 1:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as lines
import matplotlib.transforms as mtransforms
import matplotlib.text as mtext
   
   
class GFGfun(lines.Line2D):
    def __init__(self, *args, **kwargs):
        self.text = mtext.Text(0, 0, '')
        lines.Line2D.__init__(self, *args, **kwargs)
        self.text.set_text(self.get_label())
   
    def set_figure(self, figure):
        self.text.set_figure(figure)
        lines.Line2D.set_figure(self, figure)
   
    def set_axes(self, axes):
        self.text.set_axes(axes)
        lines.Line2D.set_axes(self, axes)
   
    def set_transform(self, transform):
        # 2 pixel offset
        texttrans = transform + mtransforms.Affine2D().translate(2, 2)
        self.text.set_transform(texttrans)
        lines.Line2D.set_transform(self, transform)
   
    def set_data(self, x, y):
        if len(x):
            self.text.set_position((x[-1], y[-1]))
   
        lines.Line2D.set_data(self, x, y)
   
    def draw(self, renderer):
        lines.Line2D.draw(self, renderer)
        self.text.draw(renderer)
   
   
np.random.seed(10**7)
   
   
fig, ax = plt.subplots()
x, y = np.random.rand(2, 20)
line = GFGfun(x, y, mfc ='green',
              ms = 12
              label ='Label')
   
line.text.set_color('green')
line.text.set_fontsize(16)
   
ax.add_line(line)
   
fig.suptitle('matplotlib.axes.Axes.set_figure() \
function Example\n\n', fontweight ="bold")
  
plt.show()


Output:

Example 2:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as lines
import matplotlib.transforms as mtransforms
import matplotlib.text as mtext
   
   
class GFGfun(lines.Line2D):
    def __init__(self, *args, **kwargs):
        self.text = mtext.Text(0, 0, '')
        lines.Line2D.__init__(self, *args, **kwargs)
        self.text.set_text(self.get_label())
   
    def set_figure(self, figure):
        self.text.set_figure(figure)
        lines.Line2D.set_figure(self, figure)
   
   
np.random.seed(10**7)
   
   
fig, ax = plt.subplots()
x, y = np.random.rand(2, 10)
line = GFGfun(x, y, mfc ='green',
              ms = 12,
              label ='Label')
   
line.text.set_color('green')
line.text.set_fontsize(16)
   
ax.add_line(line)
   
fig.suptitle('matplotlib.axes.Axes.set_figure() \
function Example\n\n', fontweight ="bold")
  
plt.show()


Output:

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS