How to Hide Axes in Matplotlib (With Examples)

How to Easily Hide Axes in Matplotlib for Cleaner Visualizations

In modern data visualization using Matplotlib, customization is key to creating figures that are both informative and aesthetically pleasing. Often, depending on the context of the visualization—such as embedding a chart into a larger dashboard or focusing purely on the spatial relationship of points—the default x-axis and y-axis elements become unnecessary visual noise. Fortunately, Matplotlib provides straightforward methods for precisely controlling the visibility of these graphical components.

The primary and most flexible approach involves utilizing the set_visible() method on the specific axes object. By setting this property to False, you can effectively suppress the display of labels, ticks, and the axis line itself. This article will guide you through the mechanisms for hiding individual axis components and demonstrate techniques for removing all visual framing elements from a figure.


Understanding the Axes Object Hierarchy

To manipulate the visibility of plotting elements in Matplotlib, it is essential to first understand the hierarchy of objects. Every plot resides within a Figure, and the actual plotting area is defined by one or more Axes objects. When we talk about the x-axis or y-axis, we are actually referring to specific components managed by the overall Axes object. Direct manipulation of these components allows for granular control over the plot’s appearance.

The core mechanism we employ is accessing the current Axes object using the plt.gca() function (Get Current Axes). Once we have the active Axes object, we can retrieve the specific axis element (like the X-axis) and invoke the crucial set_visible() method. This method controls whether the element is drawn on the canvas.

This process ensures that while the data itself remains plotted, the surrounding context provided by the axis labels, tick marks, and spine lines are suppressed. This technique is particularly useful when creating subplots where only the outermost plots require visible axes for context, or when preparing data visualizations for publication where minimalist design is preferred.

Implementing set_visible() for Axis Control

The most reliable and specific way to hide an axis component is by retrieving the axis object itself and toggling its visibility status. The syntax is highly explicit, ensuring that you target precisely the element you wish to remove. Below illustrates the standardized syntax used for managing individual axis visibility in Matplotlib environments.

You can use the following syntax to hide individual x-axis and y-axis components in Matplotlib plots:

import matplotlib.pyplot as plt

#get current axes using plt.gca()
ax = plt.gca()

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

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

In the code snippet above, plt.gca() retrieves the active Axes object, which is necessary before manipulating any plot characteristics. Subsequently, we call get_xaxis() or get_yaxis() to retrieve the specific axis instance. Finally, the set_visible() method is executed with the argument False, commanding Matplotlib not to render that axis component. This approach provides fine-grained control, allowing you to hide one axis while leaving the other fully displayed.

The following practical examples demonstrate how to apply this syntax effectively in various plotting scenarios.

Example 1: Hiding Only the X-Axis

In many analytical situations, especially time series analysis or visualizations where the y-axis (representing magnitude or frequency) is crucial but the x-axis (often representing index or time stamps) might be inferred or redundant, hiding the x-axis enhances clarity. This first example shows the exact implementation required to suppress only the bottom horizontal axis elements of a standard scatterplot.

We begin by defining simple data points for our x and y coordinates. We then generate the scatterplot using the standard plt.scatter() function. The critical step follows: accessing the current axes using plt.gca() and chaining the get_xaxis() and set_visible() method to False. Notice that the y-axis remains fully intact, including its tick marks and axis line.

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

import matplotlib.pyplot as 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)

The resulting figure clearly demonstrates the removal of the horizontal axis. This technique is often preferable to simply removing the axis labels, as Matplotlib also eliminates the reserved space for the axis, sometimes allowing the plot to utilize more vertical space efficiently, or to align better with adjacent subplots that share an X reference.

Hide

Example 2: Hiding Only the Y-Axis

Conversely, there are times when the vertical scale is inferred or irrelevant, but the relative position along the horizontal x-axis remains critical. For instance, if you are plotting normalized data or relative change where the absolute Y values are less informative than the trends across the X domain. This example reverses the process shown previously, targeting and hiding only the vertical y-axis.

The implementation is nearly identical to Example 1, except that we substitute get_xaxis() with get_yaxis(). This targeted approach ensures minimal impact on the overall plot aesthetics, preserving the spatial context provided by the horizontal axis while removing the vertical scale information. This method is frequently used in complex dashboards where tooltips or supplementary textual annotations provide the necessary value context for the vertical placement.

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

import matplotlib.pyplot as 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)

After running this script, you will observe that the numbers, tick marks, and the vertical spine of the plot have vanished, leaving the plot area fully dedicated to the data points and the horizontal axis. This confirms the efficacy of targeting axis components independently using the Axes object access methods.

Example 3: Hiding Both Axes Simultaneously

If the goal is to present the data purely as a visual representation—perhaps as a map layer, a stylized graphic, or a plot where all contextual information is provided externally—you may need to hide both the x-axis and y-axis. This is achieved simply by combining the techniques demonstrated in the previous two examples, calling set_visible() method on both the retrieved X and Y axis objects.

While this involves two separate lines of code targeting the axis components, it is the most robust way to ensure that only the axis elements are removed, while still allowing other potential plot features (like titles, gridlines, or legends) to remain visible, should you choose to include them. The approach reinforces the idea of working with specific, independent graphical components within the Axes object.

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

import matplotlib.pyplot as 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)

The resultant graph is purely a visualization of the data points within the defined plot area, stripped of all traditional scaling markers. This level of customization allows developers and analysts to integrate plots seamlessly into various user interface designs without clashing with surrounding layout elements.

Example 4: Removing Axes & Borders Completely

While using get_xaxis().set_visible(False) provides surgical precision in removing individual axes, Matplotlib offers a shortcut for completely removing all surrounding frame elements—including axes, ticks, labels, and the bounding box (or spine) of the plot. This is achieved using the plt.axis() function with the argument ‘off’.

The plt.axis('off') command is extremely powerful because it modifies the visibility properties of the current Axes object comprehensively. Instead of targeting the X and Y axes individually, this single command ensures that the visual frame is entirely suppressed. This is the preferred method when you want a minimalist display, such as for drawing simple markers or complex spatial relationships without any distracting contextual lines.

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

import matplotlib.pyplot as 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 using the built-in helper function
plt.axis('off')

It is important to understand the difference between this method and the set_visible(False) approach. While set_visible(False) only removes the axis (labels, ticks, line), plt.axis('off') also targets the four boundary lines of the plot area (known as the spines or borders). Therefore, plt.axis('off') results in a cleaner, completely unframed output, which is generally desired when integrating charts into graphic design layouts.

Summary of Axis Control Techniques

Matplotlib provides flexibility in controlling the visual components of a plot, allowing users to tailor outputs for specific consumption needs. The choice between using the targeted set_visible() method and the comprehensive plt.axis('off') helper function depends entirely on the desired outcome regarding the plot boundaries.

Here is a quick summary of when to use each technique:

  • Targeted Removal (.get_xaxis().set_visible(False)): Use this when you need to remove only the labels, ticks, and line for one axis (X or Y) but wish to keep the other axis and the plot boundary box visible. This is ideal for subplots that share an axis reference.
  • Complete Removal (plt.axis('off')): Use this when you require a minimalist rendering of the data points only, completely suppressing all axis elements, tick marks, labels, and the four surrounding plot borders (spines). This is best for standalone graphics or embedded visualization fragments.

Mastering these techniques ensures that your visualizations are not only accurate in their data representation but also optimized for their intended display environment, leading to more professional and impactful data storytelling.

 

Cite this article

stats writer (2025). How to Easily Hide Axes in Matplotlib for Cleaner Visualizations. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-to-hide-axes-in-matplotlib-with-examples/

stats writer. "How to Easily Hide Axes in Matplotlib for Cleaner Visualizations." PSYCHOLOGICAL SCALES, 4 Dec. 2025, https://scales.arabpsychology.com/stats/how-to-hide-axes-in-matplotlib-with-examples/.

stats writer. "How to Easily Hide Axes in Matplotlib for Cleaner Visualizations." PSYCHOLOGICAL SCALES, 2025. https://scales.arabpsychology.com/stats/how-to-hide-axes-in-matplotlib-with-examples/.

stats writer (2025) 'How to Easily Hide Axes in Matplotlib for Cleaner Visualizations', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-to-hide-axes-in-matplotlib-with-examples/.

[1] stats writer, "How to Easily Hide Axes in Matplotlib for Cleaner Visualizations," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, December, 2025.

stats writer. How to Easily Hide Axes in Matplotlib for Cleaner Visualizations. PSYCHOLOGICAL SCALES. 2025;vol(issue):pages.

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