Hide Axis, Borders and White Spaces in Matplotlib
×


Hide Axis, Borders and White Spaces in Matplotlib

2038

How to Hide Axis, Borders, and White Spaces in Matplotlib

Matplotlib is a powerful Python library for creating static, animated, and interactive visualizations. By default, plots include axes, borders, and white spaces, which might not be desirable for certain presentations or designs. This guide explores various methods to hide these elements effectively in Matplotlib.

Hiding the Entire Axis

To remove both the X and Y axes from a plot, you can use the axis('off') command. This is particularly useful when you want to display the data without any axis lines or labels.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.axis('off')  # Hide both axes
plt.show()

In this example, the sine wave is displayed without any axis lines or labels, providing a clean visualization.

Hiding Specific Axes

If you prefer to hide only one axis, you can use xticks([]) or yticks([]) to remove the ticks and labels of the X or Y axis, respectively.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.xticks([])  # Hide X-axis
plt.show()

Here, the X-axis is hidden, but the Y-axis remains visible, allowing you to focus on the vertical data representation.

Removing Borders and White Spaces

Even after hiding the axes, there might still be unwanted borders and white spaces around the plot. To remove these, you can adjust the layout using tight_layout() and save the figure with bbox_inches='tight' and pad_inches=0.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

plt.plot(x, y)
plt.axis('off')
plt.tight_layout(pad=0)
plt.savefig('sine_wave.png', bbox_inches='tight', pad_inches=0)
plt.show()

This approach ensures that the saved figure has no extra white spaces or borders, making it suitable for presentations or publications.

Hiding Axis Spines

Axis spines are the lines connecting the axis tick marks and noting the boundaries of the plot area. You can hide specific spines to customize the appearance of your plot.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.spines['top'].set_visible(False)  # Hide top spine
ax.spines['right'].set_visible(False)  # Hide right spine
plt.show()

In this example, the top and right spines are hidden, leaving the left and bottom spines visible for reference.

Using set_visible(False) for Axes

For complete removal of axis lines and labels, you can set the visibility of the axes to False.

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)
ax.xaxis.set_visible(False)  # Hide X-axis
ax.yaxis.set_visible(False)  # Hide Y-axis
plt.show()

This method completely hides the axes, leaving only the plot content visible.

Conclusion

Matplotlib provides several methods to hide axes, borders, and white spaces, allowing you to create clean and minimalistic visualizations. Whether you're preparing plots for presentations, publications, or personal projects, these techniques can help enhance the visual appeal of your data representations.


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