본문 바로가기
재미로 하는 코딩

시각화 뽀개기13

by 헬푸밍 2023. 3. 30.

Visualizing statistical relationships(통계적 관계 시각화)

Showing multiple relationships with facets(패싯으로 여러 관계 보여주기)

이번 튜토리에서 사용한 함수는 몇가지 시맨틱 변수를 한번에 보여줄수 있지만 그것이 항상 효과적이지는 않다는 것을 강조했다! 두 변수 사이의 관계가 둘 이상의 다른 변수에 어떻게 의존하는지 이해하고싶을때는 어떻게 할까?

 

두개 이상의 그래프를 만드는 것이 가장 좋은 접근법이다. replot()은 FacetGrid를 기반으로 해서 두 개 이상의 서브플롯을 그리기가 쉽다. 추가변수의 영향을 보여주기 위해, 추가변수를 플롯에서 시맨틱 변수에 할당하는 대신, 시각화를 패싯하는데 사용하자! 즉, 여러 축을 만들고 각 축에 대한 데이터의 부분집합을 그리자!

tips데이터에서

x는 총금액, y는 팁, hue는 흡연여부

col은 시간으로 해서 산점도를 그려보면..

tips = sns.load_dataset("tips")
sns.relplot(
    data=tips,
    x="total_bill", y="tip", hue="smoker", col="time",
);

시간별로 산점도를 그릴 수 있다!

 

두 변수를 하나는 컬럼에 패시팅하고 하나는 로우에 패시팅함으로서 두 변수의 영향을 보여줄 수 있다! 격자에 변수를 추가하다 보면 그림크기를 줄이고 싶을 수 있다! FacetGrid의 사이즈는 각 패싯에 height와 aspect 비율로 매개변수화된다!

fmri데이터로, x는 timepoint, y는 signal, hue는 subject

col은 region, row는 event

그리고 height=3으로 크기를 조절해주면...

fmri = sns.load_dataset("fmri")
sns.relplot(
    data=fmri, kind="line",
    x="timepoint", y="signal", hue="subject",
    col="region", row="event", height=3,
    estimator=None
);

이렇게 그릴 수 있다!

 

다양한 수준의 변수에 대한 전반적인 효과를 관찰하고 싶다면, 열로 해당 변수를 패싯한다음 행으로 패싯을 래핑하는 것이 좋을 수 있다!

fmri데이터에서 region이 frontal인 것만 꺼내와서...

x는 timepoint, y는 signal, hue와 style은 event,

col은 subject로 하고 col_wrap으로 한줄에 5개씩 보여주면...

sns.relplot(
    data=fmri.query("region == 'frontal'"), kind="line",
    x="timepoint", y="signal", hue="event", style="event",
    col="subject", col_wrap=5,
    height=3, aspect=.75, linewidth=2.5,
);

이렇게 볼 수 있다!

 

lattice(격자)플롯 또는 small-multiples(작은 배수)라 불리는 이런 시각화는 전체 패턴과 이런 패턴의 편차를 눈으로 쉽게 감지할 수 있는 형식으로 데이터를 제공하기 때문 매우 효과적이다! scatterplot()과 relplot()이 제공하는 유연성을 활용해야 하지만, 하나의 복잡한 플롯보다 몇개의 단순한 플롯이 보통 더 효과적이라는 것을 알고 있자!


참고사이트

https://seaborn.pydata.org/tutorial/relational.html#showing-multiple-relationships-with-facets

 

Visualizing statistical relationships — seaborn 0.12.2 documentation

Visualizing statistical relationships Statistical analysis is a process of understanding how variables in a dataset relate to each other and how those relationships depend on other variables. Visualization can be a core component of this process because, w

seaborn.pydata.org

 

'재미로 하는 코딩' 카테고리의 다른 글

numpy를 사용한 이미지 압축해보기!  (2) 2023.03.30
시각화 뽀개기12  (0) 2023.03.23
시각화 뽀개기11  (0) 2023.03.19
다이아몬드 가격 예측해보기  (4) 2023.03.19
시각화 뽀개기10  (0) 2023.03.16

댓글