Pandas: 플롯 범례를 만들고 사용자 정의하는 방법


다음 기본 구문을 사용하여 Pandas의 플롯에 범례를 추가할 수 있습니다.

 plt. legend ([' A ',' B ',' C ',' D '], loc=' center left ', title=' Legend Title ')

다음 예에서는 실제로 이 구문을 사용하는 방법을 보여줍니다.

예: Pandas에서 플롯 범례 생성 및 사용자 정의

다음과 같은 팬더 DataFrame이 있다고 가정합니다.

 import pandas as pd

#createDataFrame
df = pd. DataFrame ({' A ':7, 'B':12, ' C ':15, ' D ':17}, index=[' Values '])

다음 구문을 사용하여 DataFrame의 값을 시각화하고 사용자 정의 레이블이 있는 범례를 추가하는 막대 차트를 만들 수 있습니다.

 import matplotlib. pyplot as plt

#create bar chart
df. plot (kind=' bar ')

#add legend to bar chart
plt. legend ([' A Label ',' B Label ',' C Label ',' D Label '])

loc 인수와 title 인수를 사용하여 범례의 위치와 제목을 수정할 수도 있습니다.

 import matplotlib. pyplot as plt

#create bar chart
df. plot (kind=' bar ')

#add custom legend to bar chart
plt. legend ([' A Label ', ' B Label ', ' C Label ', ' D Label '],
            loc=' upper left ', title=' Labels ') 

마지막으로 size 인수를 사용하여 범례의 글꼴 크기를 수정할 수 있습니다.

 import matplotlib. pyplot as plt

#create bar chart
df. plot (kind=' bar ')

#add custom legend to bar chart
plt. legend ([' A Label ', ' B Label ', ' C Label ', ' D Label '], prop={' size ': 20 }) 

이제 캡션의 글꼴 크기가 훨씬 커졌습니다.

추가 리소스

다음 튜토리얼에서는 Pandas에서 다른 일반적인 작업을 수행하는 방법을 설명합니다.

Pandas DataFrame에서 원형 차트를 만드는 방법
Pandas DataFrame에서 포인트 클라우드를 만드는 방법
Pandas DataFrame에서 히스토그램을 만드는 방법

의견을 추가하다

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다