Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Matplotlib에서 Seaborn 산점도 외부로 범례를 이동하는 방법은 무엇입니까?


범례를 Seaborn 산점도 외부로 이동하려면 다음 단계를 수행할 수 있습니다. -

  • 그림 크기를 설정하고 서브플롯 사이 및 주변 여백을 조정합니다.
  • 3개의 열, column1, column2로 Pandas 데이터 프레임 만들기 및 열 3 .
  • 여러 의미 그룹화 가능성이 있는 산점도를 그립니다.
  • 범례를 플롯 외부에 배치하려면 bbox_to_anchor를 사용하세요. legend()에서 방법.
  • 그림을 표시하려면 show()를 사용하세요. 방법.

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True

df = pd.DataFrame(dict(col1=[2, 1, 4],
                       col2=[5, 2, 1],
                       col3=[4, 0, 1]))

sns.scatterplot(data=df)

plt.legend(bbox_to_anchor=(1.25, 1), borderaxespad=0)

plt.show()

출력

Matplotlib에서 Seaborn 산점도 외부로 범례를 이동하는 방법은 무엇입니까?