Matplotlib.pyplot.gca() in Python - GeeksforGeeks

01 Jul, 2021
Matplotlib is a library in Python and it is a numerical – mathematical extension for the NumPy library. Pyplot is a state-based interface to a Matplotlib module that provides a MATLAB-like interface.  
The gca() function in pyplot module of matplotlib library is used to get the current Axes instance on the current figure matching the given keyword args, or create one.  
Syntax: matplotlib.pyplot.gca(\*\*kwargs)Parameters: This method does not accepts any parameter. Returns: This method does not return any value.  
Below examples illustrate the matplotlib.pyplot.gca() function in matplotlib.pyplot: 
Example #1: 
Python3
# Implementation of matplotlib functionimport matplotlib.pyplot as pltimport numpy as npimport matplotlib.gridspec as gridspecfrom mpl_toolkits.axes_grid1 import make_axes_locatable  plt.close('all')arr = np.arange(100).reshape((10, 10))fig = plt.figure(figsize =(4, 4))im = plt.imshow(arr,                interpolation ="none",                cmap ="plasma") divider = make_axes_locatable(plt.gca())cax = divider.append_axes("left",                          "15 %",                          pad ="30 %") plt.colorbar(im, cax = cax) fig.suptitle('matplotlib.pyplot.gca() function\Example\n\n', fontweight ="bold")plt.show()
Output:  
Example #2: 
Python3
# Implementation of matplotlib functionimport matplotlib.pyplot as pltfrom matplotlib.tri import Triangulationfrom matplotlib.patches import Polygonimport numpy as np   def update_polygon(tri):         if tri == -1:        points = [0, 0, 0]    else:        points = triang.triangles[tri]             xs = triang.x[points]    ys = triang.y[points]    polygon.set_xy(np.column_stack([xs, ys]))    def motion_notify(event):         if event.inaxes is None:        tri = -1    else:        tri = trifinder(event.xdata, event.ydata)             update_polygon(tri)    plt.title('matplotlib.pyplot.gca() function \    Example\n\n Potion number : % i' % tri,              fontweight ="bold")         event.canvas.draw() ang = 3radi = 8radii = np.linspace(0.25, 0.95, radi)res = np.linspace(0, 4 * np.pi, ang)res = np.repeat(res[..., np.newaxis], radi, axis = 1)res[:, 1::2] += np.pi / ang x = (radii * np.cos(2 * res)).flatten()y = (radii * np.sin(2 * res)).flatten()triang = Triangulation(x, y)triang.set_mask(np.hypot(x[triang.triangles].mean(axis = 1),                         y[triang.triangles].mean(axis = 1))                < 0.25)  trifinder = triang.get_trifinder()  plt.subplot(111, aspect ='equal')plt.triplot(triang, 'go-')polygon = Polygon([[0, 0], [0, 0]],                  facecolor ='r')update_polygon(-1) plt.gca().add_patch(polygon)plt.gcf().canvas.mpl_connect('motion_notify_event',                             motion_notify)  plt.show()
Output:  
 
codingyash
Python-matplotlib
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Iterate over a list in Python
Python String | replace()
*args and **kwargs in Python
Reading and Writing to text files in Python
Create a Pandas DataFrame from Lists