Matplotlib é uma biblioteca em Python e é uma extensão numérica - matemática da biblioteca NumPy. A classe Axes contém a maioria dos elementos da figura: Axis, Tick, Line2D, Text, Polygon, etc., e define o sistema de coordenadas. E as instâncias de Axes suportam callbacks por meio de um atributo callbacks.

Função matplotlib.axes.Axes.get_figure()

A função Axes.get_figure() no módulo axes da biblioteca matplotlib é usada para obter a instância de Figura à qual o artista pertence.

Sintaxe: Axes.get_figure (self)

Parâmetros: este método não aceita nenhum parâmetro.

Retorna: Este método retorna a instância da Figura à qual o artista pertence.



Os exemplos abaixo ilustram a função matplotlib.axes.Axes.get_figure() em matplotlib.axes:

Exemplo 1:

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): 
      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) 
   
ax.text(0.2, 0.8, "Value Return : "
        +str(ax.get_figure()),  
        fontweight ="bold") 
  
fig.suptitle('matplotlib.axes.Axes.get_figure()\ 
 function Example\n\n', fontweight ="bold") 
  
plt.show() 

Resultado:

Exemplo 2:

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) 
  
ax.text(0.2, 0.8, "Value Return : "
        +str(ax.get_figure()), 
        fontweight ="bold") 
   
fig.suptitle('matplotlib.axes.Axes.get_figure()\ 
function Example\n\n', fontweight ="bold") 
  
plt.show() 

Resultado: