How to turn off the axes for subplots in Matplotlib?
×


How to turn off the axes for subplots in Matplotlib?

1607

How to Hide Axes for Subplots in Matplotlib

When working with multiple subplots in Matplotlib, sometimes you want to hide the axes to create a cleaner look—especially useful when showing images or decorative plots. This article explains different ways to turn off the axes for subplots efficiently.

Using set_axis_off() to Disable Axes

The set_axis_off() method hides everything related to the axes including ticks, labels, and borders. It’s a straightforward way to completely remove the axis visuals from your subplot.

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(2, 2)

for ax in axs.flat:
    ax.plot(np.random.rand(10))
    ax.set_axis_off()  # Hide axes completely

plt.tight_layout()
plt.show()

This code creates a 2x2 grid of plots with no visible axes.

Hiding Axes Using axis('off')

You can also use the axis('off') command on each subplot’s axis to hide the axes. This method is concise and achieves a similar result to set_axis_off().

for ax in axs.flat:
    ax.plot(np.random.rand(10))
    ax.axis('off')  # Switch off axes visibility

Controlling X and Y Axes Separately

If you want to hide only the x-axis or y-axis individually, you can toggle their visibility separately by:

for ax in axs.flat:
    ax.plot(np.random.rand(10))
    ax.get_xaxis().set_visible(False)  # Hide x-axis only
    ax.get_yaxis().set_visible(False)  # Hide y-axis only

Removing Axis Borders (Spines) Only

Sometimes you want to keep the ticks and labels but remove just the frame lines around the subplot. You can do this by hiding the spines:

for ax in axs.flat:
    ax.plot(np.random.rand(10))
    for spine in ax.spines.values():
        spine.set_visible(False)  # Hide the borders of the plot

Summary

Matplotlib offers several ways to turn off axes in subplots depending on how much of the axis you want to hide—from removing everything with set_axis_off() or axis('off'), to selectively hiding axis lines or labels. These techniques help you create visually clean and focused plots tailored to your presentation needs.


If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!


Best WordPress Hosting


Share:


Discount Coupons

Unlimited Video Generation

Best Platform to generate videos

Search and buy from Namecheap

Secure Domain for a Minimum Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat