Saturday, November 16, 2024
Google search engine
HomeLanguagesMatplotlib.axis.Axis.set_ticks() function in Python

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

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.set_ticks() Function

The Axis.set_ticks() function in axis module of matplotlib library is used to set the locations of the tick marks from sequence ticks. 
 

Syntax: Axis.set_ticks(self, ticks, \*, minor=False) 
 

Parameters: This method accepts the following parameters. 

  • ticks : This parameter is the sequence of floats.
  • minor : This parameter contains the bool value.

Return value: This method does not return any value. 

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

Example 1:

Python3




# Implementation of matplotlib function
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import numpy as np 
  
fig = plt.figure()
  
x = np.linspace(0,2*np.pi,100)
y = 2*np.sin(x)
  
ax = fig.add_subplot()
ax.plot(x,y)
ax.yaxis.set_ticks([1,2,3,4,5,6,7])  
  
ax.grid() 
  
fig.suptitle("""matplotlib.axis.Axis.set_ticks()
function Example\n""", fontweight ="bold")  
    
plt.show()


Output: 
 

Example 2:

Python3




# Implementation of matplotlib function
from matplotlib.axis import Axis
import matplotlib.pyplot as plt
import numpy as np 
  
fig = plt.figure()
x = np.linspace(-np.pi, np.pi, 100)
y = 2 * np.sin(x)
  
ax = fig.add_subplot()
ax.set_title('centered spines')
ax.plot(x, y)
  
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
  
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks([-3, -2.5, -2, -1.5, -1,
                    -0.5, 0, 0.5, 1, 1.5,
                    2, 2.5, 3])
  
ax.grid() 
  
fig.suptitle("""matplotlib.axis.Axis.set_ticks()
function Example\n""", fontweight ="bold")  
    
plt.show()


Output: 
 

 

RELATED ARTICLES

Most Popular

Recent Comments