Matplotlib.axes.Axes.set_axis_on() in Python

Last Updated : 28 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Axes.set_axis_on() function in Matplotlib is used to turn on the axis lines, ticks, tick labels, and axis labels on a given Axes object. It is a simple way to make the axis visible if it was previously turned off. It's key features include:

  • Turns on the axis lines, ticks, tick labels and axis labels.
  • Useful when you want to restore the visibility of the axis after it was hidden.
  • Modifies the Axes object in-place.

Syntax

Axes.set_axis_on(self)

Parameters: This method does not take any parameters.

Returns: It returns the modified Axes object.

Examples

Example 1: In this example, we plot the cross-correlation between two identical arrays using ax.xcorr(). We then demonstrate toggling the visibility of the axes using ax.set_axis_off() and ax.set_axis_on().

Python
import matplotlib.pyplot as plt
import numpy as np
   
x = np.array([24.40, 110.25, 20.05,22.00, 61.90, 7.80, 15.00])
y = np.array([24.40, 110.25, 20.05,22.00, 61.90, 7.80, 15.00])
   
fig, ax = plt.subplots()
ax.xcorr(x, y, maxlags = 6, 
         color ="green")
 
ax.set_axis_off()
ax.set_axis_on()
plt.show()

Output

Output
Using Matplotllib.axes.Axes.set_axis_on()

Explanation: ax.xcorr() visualizes how two sequences are correlated over a range of lags. After plotting, the code toggles the visibility of the plot axes by first hiding them with ax.set_axis_off() and then showing them again with ax.set_axis_on().

Example 2: This demonstrates the effect of set_axis_off() and set_axis_on() by toggling axis visibility in two side-by-side subplots.

Python
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import numpy as np
   
x = np.asarray([0, 1, 2, 3, 0.5, 1.5, 2.5, 1, 2, 1.5])
y = np.asarray([0, 0, 0, 0, 1.0, 1.0, 1.0, 2, 2, 3.0])

triangles = [[0, 1, 4], [1, 5, 4], 
             [2, 6, 5], [4, 5, 7],
             [5, 6, 8], [5, 8, 7], 
             [7, 8, 9], [1, 2, 5], 
             [2, 3, 6]]

triang = mtri.Triangulation(x, y, triangles)
z = np.cos(1.5 * x) * np.cos(1.5 * y)
   
fig, [axs, axs1] = plt.subplots(1, 2)
axs.tricontourf(triang, z)
axs.triplot(triang, 'go-', color ='white')
axs.set_axis_off()
axs.set_title('Without set_axis_on', 
              fontsize = 10, 
              fontweight ='bold')

axs1.tricontourf(triang, z)
axs1.triplot(triang, 'go-', color ='white')
axs1.set_xlabel("X-axis")
axs1.set_ylabel("Y-axis")
axs1.set_axis_off()

axs1.set_axis_on()
axs1.set_title('With set_axis_on ',
               fontsize = 10,
               fontweight ='bold')

plt.show()

Output

Output
Using Matplotllib.axes.Axes.set_axis_on()

Explanation: The data arrays define the points and triangles, with z setting contour colors. The left subplot hides the axes using axs.set_axis_off(), while the right subplot restores axis visibility by calling axs1.set_axis_on() after initially turning them off.



Next Article
Article Tags :
Practice Tags :

Similar Reads