Wednesday, July 3, 2024
HomeLanguagesPythonMatplotlib.axis.Axis.get_figure() function in Python

Matplotlib.axis.Axis.get_figure() function in Python

Matplotlib is a library in Python and it is numerical ā€“ mathematical extension for NumPy library. It is an amazing visualization library in Python for 2D plots of arrays and used for working with the broader SciPy stack.
Ā 

Matplotlib.axis.Axis.get_figure() Function

The Axis.get_figure() function in axis module of matplotlib library is used to get the Figure instance the artist belongs to.Ā 
Ā 

Syntax: Axis.get_figure(self)Ā 
Ā 

Parameters: This method does not accepts any parameter.Ā 
Ā 

Return value: This method return the Figure instance the artist belongs to.Ā 

Below examples illustrate the matplotlib.axis.Axis.get_figure() function in matplotlib.axis:

Example 1:

Python3




# Implementation of matplotlib function
from matplotlib.axis import Axis
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)Ā 
Ā Ā Ā Ā Ā 
ax.text(0.2, 0.8, "Value Return : "
Ā Ā Ā Ā Ā Ā Ā Ā +str(Axis.get_figure(ax)),Ā Ā 
Ā Ā Ā Ā Ā Ā Ā Ā fontweight ="bold")
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 
fig.suptitle("""matplotlib.axis.Axis.get_figure()
function Example\n""", fontweight ="bold")Ā Ā 
Ā Ā Ā Ā 
plt.show()


Output:Ā 
Ā 

Example 2:

Python3




# Implementation of matplotlib function
from matplotlib.axis import Axis
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(Axis.get_figure(ax)),Ā 
Ā Ā Ā Ā Ā Ā Ā Ā fontweight ="bold")
Ā Ā 
fig.suptitle("""matplotlib.axis.Axis.get_figure()
function Example\n""", fontweight ="bold")Ā Ā 
Ā Ā Ā Ā 
plt.show()


Output:Ā 
Ā 

Ā 

Ted Musemwa
As a software developer Iā€™m interested in the intersection of computational thinking and design thinking when solving human problems. As a professional I am guided by the principles of experiential learning; experience, reflect, conceptualise and experiment.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments