Saturday, November 16, 2024
Google search engine
HomeLanguagesMatplotlib.pyplot.xticks() in Python

Matplotlib.pyplot.xticks() 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.xticks() Function

The annotate() function in pyplot module of matplotlib library is used to get and set the current tick locations and labels of the x-axis.

Syntax:

matplotlib.pyplot.xticks(ticks=None, labels=None, **kwargs)

Parameters: This method accept the following parameters that are described below:

  • ticks: This parameter is the list of xtick locations. and an optional parameter. If an empty list is passed as an argument then it will removes all xticks
  • labels: This parameter contains labels to place at the given ticks locations. And it is an optional parameter.
  • **kwargs: This parameter is Text properties that is used to control the appearance of the labels.

Returns: This returns the following:

  • locs :This returns the list of ytick locations.
  • labels :This returns the list of ylabel Text objects.

The resultant is (locs, labels)

Below examples illustrate the matplotlib.pyplot.xticks() function in matplotlib.pyplot:

Example #1:




# Implementation of matplotlib.pyplot.xticks()
# function
  
import numpy as np
import matplotlib.pyplot as plt
    
x = [1, 2, 3, 4]
y = [95, 38, 54, 35]
labels = ['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4']
  
plt.plot(x, y)
  
# You can specify a rotation for the tick
# labels in degrees or with keywords.
plt.xticks(x, labels, rotation ='vertical')
  
# Pad margins so that markers don't get 
# clipped by the axes
plt.margins(0.2)
  
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom = 0.15)
plt.show()


Output:

Example #2:




# Implementation of matplotlib.pyplot.xticks()
# function
  
import matplotlib.pyplot as plt
  
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, zoomed_inset_axes
  
def get_demo_image():
    from matplotlib.cbook import get_sample_data
    import numpy as np
    f = get_sample_data("axes_grid / bivariate_normal.npy"
                        asfileobj = False)
    z = np.load(f)
      
    # z is a numpy array of 15x15
    return z, (3, 19, 4, 13)
  
  
fig, ax = plt.subplots(figsize =[5, 4])
  
Z, extent = get_demo_image()
  
ax.set(aspect = 1,
       xlim =(0, 65),
       ylim =(0, 50))
  
  
axins = zoomed_inset_axes(ax, zoom = 2
                          loc ='upper right')
  
im = axins.imshow(Z, extent = extent, 
                  interpolation ="nearest",
                  origin ="upper")
  
plt.xlabel('X-axis'
plt.ylabel('Y-axis')
  
plt.xticks(visible = False)
plt.show() 


Output:

RELATED ARTICLES

Most Popular

Recent Comments