Matplotlib.axis.Axis.get_gridlines() function in Python
Last Updated :
03 Jun, 2020
Improve
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_gridlines() Function
The Axis.get_gridlines() function in axis module of matplotlib library is used to get the grid lines as a list of Line2D instance.
Syntax: Axis.get_gridlines(self)
Parameters: This method does not accepts any parameters.
Return value: This method returns the grid lines as a list of Line2D instance.
Below examples illustrate the matplotlib.axis.Axis.get_gridlines() function in matplotlib.axis:
Example 1:
# Implementation of matplotlib function
from matplotlib.axis import Axis
import numpy as np
import matplotlib.pyplot as plt
import random
fig, ax2 = plt.subplots()
x = []
for i in range(1,11):
j = random.randint(1,10)
x.append(i*j)
ax2.plot(x)
print("Value of get_gridlines() :")
for i in ax2.xaxis.get_gridlines():
print(i)
fig.suptitle("Matplotlib.axis.Axis.get_gridlines()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Output:

Value of get_gridlines() : Line2D((0,0),(0,1)) Line2D((0,0),(0,1)) Line2D((0,0),(0,1)) Line2D((0,0),(0,1)) Line2D((0,0),(0,1)) Line2D((0,0),(0,1)) Line2D((0,0),(0,1))
Example 2:
# Implementation of matplotlib function
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import matplotlib.text
fig, ax = plt.subplots()
ax.plot([5,1], label="Label 1")
ax.plot([3,0], label="Label 2")
legend = ax.legend(loc="upper right")
offset = matplotlib.text.OffsetFrom(legend, (1.0, 0.0))
ax.annotate("String - Info",
xy = (0,0),
size = 14,
xycoords = 'figure fraction',
xytext = (0,-20),
textcoords = offset,
horizontalalignment = 'right',
verticalalignment = 'top')
fig.canvas.draw()
ax.grid()
print("Value of get_gridlines() :")
for i in ax.yaxis.get_gridlines():
print(i)
fig.suptitle("Matplotlib.axis.Axis.get_gridlines()\
Function Example", fontsize = 12, fontweight ='bold')
plt.show()
Output:

Value of get_gridlines() : Line2D((0,0),(1,0)) Line2D((0,0),(1,0)) Line2D((0,0),(1,0)) Line2D((0,0),(1,0)) Line2D((0,0),(1,0)) Line2D((0,0),(1,0)) Line2D((0,0),(1,0)) Line2D((0,0),(1,0))