Home  >  Article  >  Backend Development  >  How to Remove Axes, Legends, and White Padding from Matplotlib Images?

How to Remove Axes, Legends, and White Padding from Matplotlib Images?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-23 18:17:36916browse

How to Remove Axes, Legends, and White Padding from Matplotlib Images?

Eliminating Axes, Legends, and White Padding in Matplotlib Images

To create a clean visual representation of images in Matplotlib, removing axes, legends, labels, and any additional elements is essential. Here's how to do it:

  1. Remove Axes: Use the axis('off') method to hide both x and y axes.
  2. Remove Legends: To remove legends, set the legend() function to None or simply don't call it at all.
  3. Eliminate White Padding: Using bbox_inches='tight' in the savefig() command removes most of the surrounding white space.

In your example:

def make_image(inputname, outputname):
    data = mpimg.imread(inputname)[:,:,0]
    fig = plt.imshow(data)
    fig.set_cmap('hot')
    plt.axis('off')
    plt.savefig(outputname, bbox_inches='tight')

This code successfully removes the axes and eliminates the white padding, leaving only the desired image.

Note: In some newer versions of Matplotlib, bbox_inches='tight' has been replaced with bbox_inches=0.

The above is the detailed content of How to Remove Axes, Legends, and White Padding from Matplotlib Images?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn