0

Matplotlib.axis.Axis.get_majorticklabels() function in Python

 1 year ago
source link: https://www.geeksforgeeks.org/matplotlib-axis-axis-get_majorticklabels-function-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Matplotlib.axis.Axis.get_majorticklabels() function in Python

  • Last Updated : 03 Jun, 2020

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_majorticklabels() Function

The Axis.get_majorticklabels() function in axis module of matplotlib library is used to get a list of Text instances for the major ticklabels.

Syntax: Axis.get_majorticklabels(self) 

Parameters: This method does not accepts any parameter. 

Return value: This method returns the list of Text instances for the major ticklabels. 

Below examples illustrate the matplotlib.axis.Axis.get_majorticklabels() function in matplotlib.axis:

Example 1:

  • Python3
# Implementation of matplotlib function 
import numpy as np
from matplotlib.axis import Axis  
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
np.random.seed(19680801)
fig, ax = plt.subplots()
ax.plot(100*np.random.rand(20))
formatter = ticker.FormatStrFormatter('%1.2f')
Axis.set_major_formatter(ax.yaxis, formatter)
print("Value of get_major_ticks() :")
for i in ax.xaxis.get_majorticklabels():
print(i)
plt.title("Matplotlib.axis.Axis.get_majorticklabels()\n\
Function Example", fontsize = 12, fontweight ='bold'
plt.show()

Output: 

getmal1.jpg
Value of get_majorticklabels() :
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')
Text(0, 0, '')

Example 2:

  • Python3
# Implementation of matplotlib function 
from matplotlib.axis import Axis  
from matplotlib.artist import Artist  
import matplotlib.pyplot as plt  
import numpy as np  
np.random.seed(19680801)  
n = 100000
x = np.random.standard_normal(n)  
y = 2 * np.random.standard_normal(n)  
z =[1, 2, 3, 4]  
xmin = x.min()  
xmax = x.max()  
ymin = y.min()  
ymax = y.max()  
fig, ax = plt.subplots()  
hb = ax.hexbin(x, y, 
gridsize = 50
bins ='log'
cmap ='BuGn')  
ax.set(xlim =(xmin, xmax), ylim =(ymin, ymax)) 
ax.xaxis.set_ticklabels(["A","B","C","D","E","F","G","H","I","J","K"])
print("Value of get_majorticklabels() :")
for i in ax.xaxis.get_majorticklabels():
print(i)
plt.title("Matplotlib.axis.Axis.get_majorticklabels()\n\
Function Example", fontsize = 12, fontweight ='bold'
plt.show()

Output: 

ge2.jpg
Value of get_majorticklabels() :
Text(0, 0, 'A')
Text(0, 0, 'B')
Text(0, 0, 'C')
Text(0, 0, 'D')
Text(0, 0, 'E')
Text(0, 0, 'F')
Text(0, 0, 'G')
Text(0, 0, 'H')
Text(0, 0, 'I')
Text(0, 0, 'J')
Text(0, 0, 'K')

2022-05-25-10-57-57-PythonProgrammingInArticleAd.webp

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK