# Matplotlib GCA in Python Explained with Examples

Hello coders!! In this post, we will learn about Matplotlib GCA in Python. We will see some examples to make our concept clear and see its uses and applications.

Contents

## Matplotlib.pyplot.gca() Function in Python:

Matplotlib is an in-built library available in Python. It is essentially a numerical and mathematical extension of Python’s NumPy library. Pyplot is a MATLAB like interface provided by the matplotlib module. The GCA() function is used to get the current Axes instance on the current figure matching the given keyword args or create one.

## Synatx:

` matplotlib.pyplot.gca(\*\*kwargs)`

## Parameters:

• No parameters.

## Return Value:

• No return value.

## How does Matplotlib GCA work internally?

`GCA` stands for “get current axes”.

Here, “Current” means that it provides a handle to the last active axes. If there are no axes yet, axes will be created. If you create two subplots, then the subplot that is created last is the current one.

## Illustrated Examples:

```import matplotlib.pyplot as plt

plt.figure()
ax = plt.gca()
print (ax)
```

#### Output:

`AxesSubplot(0.125,0.125;0.775x0.755)`

As we can see, in this example, we have used the matplotlib gca() method to get the instance of the current axis.

```import matplotlib.pyplot as plt
import numpy as np
import matplotlib.gridspec as gridspec
from mpl_toolkits.axes_grid1 import make_axes_locatable

arr = np.arange(100).reshape((10, 10))
fig = plt.figure()
im = plt.imshow(arr,interpolation ="none",cmap ="Blues")

divider = make_axes_locatable(plt.gca())
cax = divider.append_axes("left", "20 %", pad ="40 %")

plt.colorbar(im, cax = cax)

fig.suptitle('matplotlib.pyplot.gca()', fontweight ="bold")
plt.show()

```

#### Output:

Let us first understand the different modules that we have imported for our code. We already know about the matplotlib and the NumPy module. The gridspec module is used to customize the figure layouts. The make_axes_locatable() method takes existing axes, creates a divider for them, and returns an instance of the AxesLocator class.

We have created the data using the np.arange() method. Then we used the imshow() method to display our data as images. We then used the make_axes_locatable() to customize the layout and the gca() method to get the instance of the current axes.  The append_axes() method is used to create new axes on a given side of the original axes.

## Matplotlib gca 3d Projection:

```import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

plt.figure()
x = np.arange(-2, 2, 0.25)
y = np.arange(-2, 2, 0.25)
x, y = np.meshgrid(x, y)
z = np.sin(x * np.pi / 2) + np.cos(y * np.pi / 3)

fig = plt.figure()
ax = fig.gca(projection="3d")
surf = ax.plot_surface(x, y, z, cmap='Blues')
plt.show()
```

#### Output:

As you can see, here we have made a 3D projection of our data using the matplotlib GCA. We created our data using the NumPy module. We then used the gca() method with 3D projection to get the desired result.

## Conclusion: Matplotlib GCA

With this, we come to an end with this article. We learned about the GCA() method, how it works and we also saw illustrated examples to make our concept clear.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of 