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

Patricia Arquette
Release: 2024-10-23 18:17:36
Original
915 people have browsed it

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')
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template