The Wayback Machine - http://web.archive.org/web/20240616163007/https://www.geeksforgeeks.org/matplotlib-pyplot-gca-in-python/
Open In App

Matplotlib.pyplot.gca() in Python

Last Updated : 01 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. 
 

matplotlib.pyplot.gca() Function

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 function
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
from 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: 
 

Image

Example #2: 

Python3




# Implementation of matplotlib function
import matplotlib.pyplot as plt
from matplotlib.tri import Triangulation
from matplotlib.patches import Polygon
import 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 = 3
radi = 8
radii = 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: 
 

Image

 



Similar Reads

Matplotlib.figure.Figure.gca() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The figure module provides the top-level Artist, the Figure, which contains all the plot elements. This module is used to control the default spacing of the subplots and top level container for all plot elements. matplotlib.figure.Figure.gca() method T
2 min read
Matplotlib.pyplot.broken_barh() in Python
Matplotlib is one of the most popular Python packages used for data visualization. It is a cross-platform library for making 2D plots from data in arrays. Pyplot is a collection of command style functions that make matplotlib work like MATLAB. matplotlib.pyplot.broken_barh() The function broken_barh() is used to Plot a horizontal sequence of rectan
2 min read
Matplotlib.pyplot.axis() in Python
Matplotlib is a plotting library for creating static, animated, and interactive visualizations in Python. Pyplot is a Matplotlib module which provides a MATLAB-like interface. Matplotlib is designed to be as usable as MATLAB, with the ability to use Python and the advantage of being free and open-source. matplotlib.pyplot.axis() This function is us
1 min read
Matplotlib.pyplot.get_fignums() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.get_fignums() method The ge
2 min read
Python | Matplotlib.pyplot ticks
Matplotlib is an amazing visualization library in Python for 2D plots of arrays. Matplotlib is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. It was introduced by John Hunter in the year 2003.One of the greatest benefits of visualization is that it allows us visual access to huge
3 min read
Matplotlib.pyplot.waitforbuttonpress() in Python
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. There are various plots which can be used in Pyplot are Line Plot, Contour, Histogram, Scatter, 3D Plot, etc. matplotlib.pyplot.waitforbuttonpress() method
2 min read
Matplotlib.pyplot.streamplot() in Python
Stream plot is basically a type of 2D plot used majorly by physicists to show fluid flow and 2D field gradients .The basic function to create a stream plot in Matplotlib is: ax.streamplot(x_grid, y_grid, x_vec, y_vec, density=spacing) Here x_grid and y_grid are arrays of the x and y points.The x_vec and y_vec represent the stream velocity of each p
7 min read
Matplotlib.pyplot.ylim() in Python
Matplotlib is a library in Python and it is numerical - mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.ylim() Function The ylim() function in pyplot module of matplotlib library is used to get or set the y-limits of the current axes. Syntax
2 min read
Matplotlib.pyplot.figimage() function in Python
Matplotlib is a widely used library in Python for plotting various graphs, as it provides very efficient ways and easy to understand methods for complex plots also. pyplot is a collection of command style functions that make matplotlib work like MATLAB. figimage() function matplotlib.pyplot.figimage() is a method for adding a non-resampled image to
2 min read
matplotlib.pyplot.arrow() in Python
Matplotlib is a very powerful plotting library useful for those working with Python and NumPy. And for making statistical interference, it becomes very necessary to visualize our data and Matplotlib is the tool that can be very helpful for this purpose. It provides MATLAB like interface only difference is that it uses Python and is open source. mat
2 min read
Article Tags :
Practice Tags :