2023. 2. 20. 22:51ㆍ기술 공부/Seaborn
+ Seaborn의 User guide and tutorial를 필사한 내용입니다.
+ 실행 환경은 Anaconda, IPython, Jupyter NoteBook 입니다.
이번에 다룰 주제는,
지난 여섯 번째 글과 동일한 Figure-level vs. axes-level functions 이다.
지난 글에서 Figure-level vs. axes-level functions 파트의
'Axes-level functions make self-contained plots'까지 알아보았다.
오늘은 이 글의 나머지 내용들을 정리할 것이다.
바로 시작해보자.
'Figure-level functions own their figure'
제목은 이러하다.
번역하자면, '그림 수준 함수는 자신의 그림을 소유합니다.'이다.
반대로 그림 수준 함수는 다른 플롯과 (쉽게) 구성될 수 없습니다. 설계상 초기화를 포함하여 고유한 그림을 "소유"하므로 그림 수준 함수를 사용하여 기존 축에 플롯을 그리는 개념이 없습니다. 이 제약 조건을 통해 그림 수준 함수는 범례를 플롯 외부에 배치하는 것과 같은 기능을 구현할 수 있습니다.
그럼에도 불구하고 반환하는 객체의 matplotlib 축에 액세스하고 플롯에 다른 요소를 추가하여 그림 수준 함수가 제공하는 것 이상을 수행할 수 있습니다.
In contrast, figure-level functions cannot (easily) be composed with other plots. By design, they “own” their own figure, including its initialization, so there’s no notion of using a figure-level function to draw a plot onto an existing axes. This constraint allows the figure-level functions to implement features such as putting the legend outside of the plot.
Nevertheless, it is possible to go beyond what the figure-level functions offer by accessing the matplotlib axes on the object that they return and adding other elements to the plot that way:
호오. 축(Axes) 수준 함수와는 반대로,
그림(Figure) 수준 함수는 다른 플롯과 (쉽게) 구성될 수 없다고 한다.
+ 여태까지 생각했던 그림(Figure) 수준 함수가 축(Axes) 수준의 함수를 소유(?)하고 있다는 생각이 틀렸다는 것이다.
그림(Figure) 수준 함수는 고유한 그림을 '소유'하므로,
그림(Figure) 수준 함수를 사용하여 기존 축에 플롯을 그리는 개념이 없다고 한다.
이 제약 조건을 통해, 범례를 플롯 외부에 배치하는 것과 같은 기능을 구현할 수 있다고 한다.
하지만, 무조건 안되는 것은 아니고 반환되는 객체를 이용하여 그 이상을 수행할 수 있다고 한다.
정리를 해보자면,
- Seaborn의 객체들은 모두 수평적이다. 어떠한 함수가 다른 함수에 종속되는 관계가 아니다를 확인할 수 있었다.
- 그림(Figure) 수준 함수는 고유한 '그림(Figure)'을 가지고 있다.
- 그림(Figure) 수준 함수는 기존 축에 플롯을 그리는 개념이 없다.
즉, 플롯이 그려지는 과정이 그림(Figure) 수준 함수 내부에서 축(Axes) 수준 함수를 호출하는 것으로 보인다. - 3번의 제약 조건을 통해, '그림(Figure)'를 다루는 특화된 기능이 구현되어 있다.
- 하지만, 반환된 matplotlib 객체로 얼마든지 자유롭게 다룰 수 있다.
바로 예시를 봐보자.
오늘의 예제 데이터 셋은 'tips'다.
tips = sns.load_dataset('tips')
tips
relplot()을 호출하고 그 반환값(객체, figure)를 grid(원문에는 g)에 담았다.
grid = sns.relplot(
data='tips',
x='total_bill',
y='tip'
)
grid 객체(Figure, 그림)의 ax(Axes, 축)에 axline을 추가하였다.
grid.ax.axline(
xy1=(10, 2),
slope=.2,
color='b',
dashes=(5, 2)
)
완성된 그림(Figure)을 봐보자.
plt.show()
훌륭하게 그려졌다.
다음으로 '그림 수준 함수에서 플롯 사용자 지정'이라는 제목의 글이 나왔다.
그림 수준 함수는 서브플롯 구성에 대해 "스마트"한 방식으로 플롯의 속성을 사용자 정의하기 위한 몇 가지 방법이 있는 FacetGrid 인스턴스를 반환합니다. 예를 들어 한 줄의 코드를 사용하여 외부 축의 레이블을 변경할 수 있습니다.
The figure-level functions return a FacetGrid instance, which has a few methods for customizing attributes of the plot in a way that is “smart” about the subplot organization. For example, you can change the labels on the external axes using a single line of code:
그림(Figure) 수준 함수는 플롯의 속성을 커스터마이징할 수 있는 FacetGrid를 반환한다고 한다.
외부 축의 레이블을 변경하는 예시를 봐보자.
(penguins 데이터 셋을 불러오는 과정은 생략하였다.)
grid = sns.relplot(
data=penguins,
x='flipper_length_mm',
y='bill_length_mm',
col='sex'
);
relplot()의 반환값(FacetGrid)를 grid 변수에 담아주었다.
grid.set_axis_labels('Flipper Length (mm)', 'Bill Length (mm)')
grid(FacetGrid)의 set_axis_labels를 통해, 외부 축의 제목을 설정해주었다.
결과물을 봐보자.
plt.show()
왼쪽과 아래에 set_axis_labels로 설정해준 제목들이 들어갔다.
이러한 FacetGrid를 통해 그림(Figure)을 설정하는 기능은 matplotlib API의 기능이 아닌, Seaborn의 그림(Figure) 수준 함수를 사용할 때만 사용할 수 있다고 한다. 내부 처리는 조금 더 복잡해지긴 하지만, 실제로 사용하는 입장에서는 편리한 기능인 것 같다.
출처 :
User guide and tutorial — seaborn 0.12.2 documentation
https://seaborn.pydata.org/tutorial/function_overview.html#figure-level-functions-own-their-figure
Overview of seaborn plotting functions — seaborn 0.12.2 documentation
Overview of seaborn plotting functions Most of your interactions with seaborn will happen through a set of plotting functions. Later chapters in the tutorial will explore the specific features offered by each function. This chapter will introduce, at a hig
seaborn.pydata.org
GitHub :
MoonsRainbow/seaborn-tutorial
https://github.com/MoonsRainbow/seaborn-tutorial
GitHub - MoonsRainbow/seaborn-tutorial: Seaborn User guide and tutorial 필사 코드입니다.
Seaborn User guide and tutorial 필사 코드입니다. Contribute to MoonsRainbow/seaborn-tutorial development by creating an account on GitHub.
github.com
'기술 공부 > Seaborn' 카테고리의 다른 글
9) Data structures accepted by seaborn (1) (1) | 2023.02.23 |
---|---|
8) Figure-level vs. axes-level functions (3) (1) | 2023.02.21 |
6) Figure-level vs. axes-level functions (1) (0) | 2023.02.12 |
5) Similar functions for similar tasks (0) | 2023.02.12 |
4) Opinionated defaults and flexible customization (0) | 2023.02.09 |