Thursday, July 4, 2024
HomeLanguagesPythonMatplotlib.axes.Axes.get_gid() in Python

Matplotlib.axes.Axes.get_gid() 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.get_gid() Function

The Axes.get_gid() function in axes module of matplotlib library is used to get the group id.

Syntax: Axes.get_gid(self)

Parameters: This method does not accepts any parameter.

Returns: This method return the group id.

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

Example 1:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
Ā Ā 
Ā Ā 
y, x = np.mgrid[:5, 1:6]
Ā Ā 
poly_coords = [
Ā Ā Ā Ā (0.25, 2.75), (3.25, 2.75),
Ā Ā Ā Ā (2.25, 0.75), (0.25, 0.75)
]
fig, ax = plt.subplots()
Ā Ā Ā Ā 
cells = ax.plot(x, y, x + y, color ='green')
Ā Ā 
ax.add_patch(
Ā Ā Ā Ā plt.Polygon(poly_coords, color ='forestgreen',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā alpha = 0.5)
Ā Ā Ā Ā )
ax.margins(x = 0.1, y = 0.05)
ax.set_aspect('equal')
Ā Ā 
for i, t in enumerate(ax.patches):
Ā Ā Ā Ā t.set_gid('patch_% d' % i)
Ā Ā Ā Ā print("Value Return :", t.get_gid())
Ā Ā Ā Ā 
fig.suptitle('matplotlib.axes.Axes.get_gid() function\
Ā Example\n\n', fontweight ="bold")
Ā Ā 
plt.show()


Output:

Value Return : patch_0

Example 2:




# Implementation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
Ā Ā 
Ā Ā 
fig, ax = plt.subplots()
Ā Ā Ā 
circle = plt.Circle((0, 0), 5, fc ='blue')
rect = plt.Rectangle((-5, 10), 10, 5, fc ='green')
Ā Ā Ā 
ax.add_patch(circle)
ax.add_patch(rect)
Ā Ā Ā 
circle_tip = ax.annotate('This is a blue circle.',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā xy =(0, 0),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā xytext =(30, -30),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā textcoords ='offset points',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color ='w',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ha ='left',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā bbox = dict(boxstyle ='round, pad =.5',Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fc =(.1, .1, .1, .92),Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ec =(1., 1., 1.),Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā lw = 1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zorder = 1),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā )
Ā Ā Ā 
rect_tip = ax.annotate('This is a green rectangle.',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā xy =(-5, 10),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā xytext =(30, 40),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā textcoords ='offset points',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā color ='w',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ha ='left',
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā bbox = dict(boxstyle ='round, pad =.5',Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā fc =(.1, .1, .1, .92),Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā ec =(1., 1., 1.),Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā lw = 1,
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā zorder = 1),
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā )
print("Value Return")
for i, t in enumerate(ax.patches):
Ā Ā Ā Ā t.set_gid('patch_% d'% i)
Ā Ā Ā Ā print(t.get_gid())
Ā Ā 
for i, t in enumerate(ax.texts):
Ā Ā Ā Ā t.set_gid('tooltip_% d'% i)
Ā Ā Ā Ā print(t.get_gid())
Ā Ā 
ax.set_xlim(-30, 30)
ax.set_ylim(-30, 30)
ax.set_aspect('equal')
Ā Ā Ā Ā 
fig.suptitle('matplotlib.axes.Axes.get_gid() function\
Example\n\n', fontweight ="bold")
Ā Ā 
plt.show()


Output:

Value Return
patch_0
patch_1
tooltip_0
tooltip_1

Thapelo Manthata
Iā€™m a desktop support specialist transitioning into a SharePoint developer role by day and Software Engineering student by night. My superpowers include customer service, coding, the Microsoft office 365 suite including SharePoint and power platform.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments