"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Remove Axes, Legends, and White Padding in Matplotlib Plots?

How to Remove Axes, Legends, and White Padding in Matplotlib Plots?

Published on 2024-11-03
Browse:782

How to Remove Axes, Legends, and White Padding in Matplotlib Plots?

Eliminating Axes, Legends, and White Padding in Matplotlib

To create a clean image without distracting elements, it's crucial to remove axes, labels, and white padding from Matplotlib plots. While setting the axis visibility to False resolves the axis issue, it leaves behind a white border and padding.

Addressing White Padding

To eliminate the white padding surrounding the image, you can employ the bbox_inches parameter when saving the image. Setting it to 'tight' will crop the figure boundaries to the extent of the data, reducing white space significantly.

Newer Matplotlib Versions

Note that for newer Matplotlib versions, you may need to use bbox_inches=0 instead of 'tight' to achieve the same result. This ensures that the image is saved with no margins or padding whatsoever.

Example Code

Here's an updated code snippet that implements the above fixes:

from numpy import random
import matplotlib.pyplot as plt

data = random.random((5, 5))
img = plt.imshow(data, interpolation='nearest')
img.set_cmap('hot')
plt.axis('off')
plt.savefig("test.png", bbox_inches=0)

Output

The resulting image will be free of axes, labels, white padding, and frame, providing a clean and focused representation of your data.

Release Statement This article is reprinted at: 1729668826 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3