플롯 커스터마이제이션
Figure, Axes, Axis의 관계
- Figure (fig): 캔버스(액자) 전체. 차트의 모든 요소를 담는 가장 큰 단위.
- Axes (ax): 실제 데이터가 그려지는 하나의 플롯 공간
- x축(Axis), y축(Axis), 제목, 플롯 자체를 모두 포함.
- Axis: 하나의 축 (x-axis, y-axis).
- 가로 8, 세로 4 크기의 Figure 생성
fig, ax = plt.subplots(figsize=(8, 4))
sns.histplot(data=df, x='price', bins=20, kde=True, ax=ax)
- 시본으로 그래프를 그리면, 그려진 Axes를 반환
ax = sns.histplot(data=df, x='price', bins=20, kde=True)
제목 및 축 라벨
plt.rcParams['font.family'] = 'Malgun Gothic'
plt.rcParams['axes.unicode_minus'] = False
ax = sns.histplot(data=df, x='price', bins=20, kde=True)
ax.set_title('자동차 가격 분포')
ax.set_xlabel('가격')
ax.set_ylabel('빈도')
하나의 Figure에 여러 Axes
fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(12, 4))
sns.histplot(data=df, x='price', bins=20, kde=True, ax=axs[0])
sns.boxplot(data=df, x='price', ax=axs[1])
설명 추가
ax = sns.scatterplot(data=df, x='year', y='price', hue='model')
ax.text(2010, 1200, '★2010년', fontsize=12, color='red')
ax.annotate('600만원', xy=(2013, 600), xytext=(2016, 400),
arrowprops={'facecolor': 'black', 'arrowstyle': '->'},
fontsize=10, color='red')
범례
ax = sns.scatterplot(data=df, x='year', y='price', hue='model')
ax.legend(title='모델', loc='lower right')
- 위치:
- 'best' (기본값, 자동으로 최적 위치 선정)
upper left upper center upper right
center left center center right
lower left lower center lower right
스타일
plt.style.use('dark_background')
ax = sns.scatterplot(data=df, x='year', y='price', hue='model')
plt.style.use('default')
plt.style.available
그림 저장
fig.savefig('chart_name.png')
fig.savefig('chart_name.pdf')
fig.savefig('high_res.png', dpi=300)
fig.savefig('transparent.png', transparent=True)
- ax만 있을 경우에는 ax.figure로 그림에 접근할 수 있음
ax.figure.savefig('chart_name.png')