"일꾼이 일을 잘하려면 먼저 도구를 갈고 닦아야 한다." - 공자, 『논어』.
첫 장 > 프로그램 작성 > matplotlib에서 축, 범례 및 흰색 패딩을 제거하는 방법은 무엇입니까?

matplotlib에서 축, 범례 및 흰색 패딩을 제거하는 방법은 무엇입니까?

2025-03-23에 게시되었습니다
검색:904

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

matplotlib에서 축, 범례 및 흰색 패딩을 제거하는

Axes

원본 코드 스 니펫은 x 축과 y 축을 숨겨서 그림의 축을 성공적으로 제거합니다. However, this technique may not entirely resolve the issue of white padding and frame around the image.
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')
Removing White Padding

To remove the white padding, we can use the axis('off') method, which hides all axes and borders, leaving only the image itself. 그러나이 방법은 여전히 ​​이미지 주위에 소량의 공백을 남길 수 있습니다. How to Remove Axes, Legends, and White Padding in Matplotlib?

흰색 패딩을 추가로 제거하려면 savefig 명령에 bbox_inches = 'tight'를 추가 할 수 있습니다. 이렇게하면 저장된 이미지를 이미지 데이터의 정확한 크기로 자르고 테두리 주위에 공백이 남아 있지 않습니다.

업데이트 된 코드 스 니펫

def make_image (inputname, outputname) : data = mpimg.imread (inputname) [:, :, 0] 그림 = plt.imshow (데이터) 그림 세트 _cmap ( '핫') plt.axis ( 'off') plt.savefig (outputname, bbox_inches = 'tight')

Axis ( 'off') 및 bbox_inches = 'tight'를 사용하여 모든 축, 범례 및 흰색 패딩을 효과적으로 제거 할 수 있습니다.

릴리스 선언문 이 기사는 다음과 같이 재현됩니다.
최신 튜토리얼 더>

부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.

Copyright© 2022 湘ICP备2022001581号-3