How can I hide axes in matplotlib using examples? 2

How can I hide axes in matplotlib using examples?

The process of hiding axes in matplotlib involves using the “ax.set_axis_off()” function to remove the axes from a plot. This function can be used in various ways, such as hiding specific axes or all axes in a plot. For example, to hide the x-axis, the command “ax.set_axis_off(xaxis=True)” can be used. Similarly, to hide both axes, the command “ax.set_axis_off()” can be used. This functionality can be useful in creating cleaner and more visually appealing plots in matplotlib.

Hide Axes in Matplotlib (With Examples)


You can use the following syntax to hide axes in Matplotlib plots:

import matplotlib.pyplotas plt

#get current axes
ax = plt.gca()

#hide x-axis
ax.get_xaxis().set_visible(False)

#hide y-axis 
ax.get_yaxis().set_visible(False)

The following examples show how to use this syntax in practice.

Example 1: Hide X-Axis

The following code shows how to create a scatterplot and hide the x-axis:

import matplotlib.pyplotas plt

#define data
x = [3, 4, 4, 6, 7, 8, 8, 12]
y = [11, 12, 12, 14, 17, 15, 14, 19]

#create scatterplot
plt.scatter(x, y)

#get current axes
ax = plt.gca()

#hide x-axis
ax.get_xaxis().set_visible(False)

Hide

Example 2: Hide Y-Axis

The following code shows how to create a scatterplot and hide the y-axis:

import matplotlib.pyplotas plt

#define data
x = [3, 4, 4, 6, 7, 8, 8, 12]
y = [11, 12, 12, 14, 17, 15, 14, 19]

#create scatterplot
plt.scatter(x, y)

#get current axes
ax = plt.gca()

#hide y-axis
ax.get_yaxis().set_visible(False)

Example 3: Hide Both Axes

The following code shows how to create a scatterplot and hide both axes:

import matplotlib.pyplotas plt

#define data
x = [3, 4, 4, 6, 7, 8, 8, 12]
y = [11, 12, 12, 14, 17, 15, 14, 19]

#create scatterplot
plt.scatter(x, y)

#get current axes
ax = plt.gca()

#hide x-axis
ax.get_xaxis().set_visible(False)

#hide y-axis
ax.get_yaxis().set_visible(False)

Example 4: Remove Axes & Borders Completely

The following code shows how to remove the axes and the plot borders completely:

import matplotlib.pyplotas plt

#define data
x = [3, 4, 4, 6, 7, 8, 8, 12]
y = [11, 12, 12, 14, 17, 15, 14, 19]

#create scatterplot
plt.scatter(x, y)

#get current axes
ax = plt.gca()

#hide axes and borders
plt.axis('off')

Cite this article

stats writer (2024). How can I hide axes in matplotlib using examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-hide-axes-in-matplotlib-using-examples/

stats writer. "How can I hide axes in matplotlib using examples?." PSYCHOLOGICAL SCALES, 3 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-hide-axes-in-matplotlib-using-examples/.

stats writer. "How can I hide axes in matplotlib using examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-hide-axes-in-matplotlib-using-examples/.

stats writer (2024) 'How can I hide axes in matplotlib using examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-hide-axes-in-matplotlib-using-examples/.

[1] stats writer, "How can I hide axes in matplotlib using examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I hide axes in matplotlib using examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top