여러개의 그래프를 위/아래 또는 왼쪽/오른쪽으로 붙여서 시각화를 한 후에 비교를 해보고 싶을 때가 있습니다. 그리고 측정 단위가 서로 같을 경우에는 x축, y축을 여러개의 그래프와 공유하면서 동일한 scale의 눈금으로 시각화 함으로써 그래프 간 비교를 더 쉽고 정확하게 할 수 있습니다. 

 

이번 포스팅에서는

  (1) 여러개의 하위 플롯 그리기: plt.subplots(nrows=2, ncols=2)

  (2) X축과 Y축을 공유하기
        : plt.subplots(sharex=True. sharey=True)

  (3) 여러개의 하위 플롯들 간의 간격을 조절해서 붙이기

        : (3-1) plt.subplots_adjust(wspace=0, hspace=0)

        : (3-2) plt.tight_layout(h_pad=-1, w_pad=-1)

하는 방법을 소개하겠습니다. 

 

 

 

(1) 여러개의 하위 플롯 그릭: plt.subplots(nrows=2, ncols=2)

 

먼저, plt.subplots(nrows=2, ncols=2) 로 2개 행, 2개 열의 layout 을 가지는 하위 플롯을 그려보겠습니다. 평균=0, 표준편차=1을 가지는 표준정규분포로 부터 100개의 난수를 생성해서 파란색(color='b')의 약간 투명한(alpha=0.5) 히스토그램을 그려보겠습니다.

 

for loop 을 돌면서 2 x 2 의 레이아웃에 하위 플롯이 그려질때는 왼쪽 상단이 1번, 오른쪽 상단이 2번, 왼쪽 하단이 3번, 오른쪽 하단이 4번의 순서로 하위 그래프가 그려집니다.  

 

이렇게 하위 플롯이 그려질 때 자동으로 하위 플롯 간 여백(padding)을 두고 그래프가 그려지며, X축과 Y축은 공유되지 않고 각각 축을 가지고 그려집니다.  

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

## subplots with 2 by 2
## (1) there are paddings between subplots
## (2) x and y axes are not shared
fig, axes = plt.subplots(
    nrows=2, 
    ncols=2, 
    figsize=(10, 10))

for i in range(2):
    for j in range(2):
        axes[i, j].hist(
            np.random.normal(loc=0, scale=1, size=100), 
            color='b', 
            alpha=0.5)

subplots with padding and x, y axes

 

 

(2) X축과 Y축을 공유하기
      : plt.subplots(sharex=True. sharey=True)

(3) 여러개의 하위 플롯들 간의 간격을 조절해서 붙이기

      : (3-1) plt.subplots_adjust(wspace=0, hspace=0)

 

여러개의 하위 플롯 간에 X축과 Y축을 공유하려면 plt.subplots()의 옵션 중에서 sharex=True, sharey=True를 설정해주면 됩니다. 

 

그리고 하위 플롯들 간의 간격을 없애서 그래프를 서로 붙여서 그리고 싶다면 두가지 방법이 있습니다. 먼저, plt.subplots_adjust(wspace=0, hspace=0) 에서 wspace 는 폭의 간격(width space), hspace 는 높이의 간격(height space) 을 설정하는데 사용합니다. 

 

fig, axes = plt.subplots(
    nrows=2, ncols=2, 
    sharex=True, # sharing properties among x axes
    sharey=True, # sharing properties among y axes 
    figsize=(10, 10))

for i in range(2):
    for j in range(2):
        axes[i, j].hist(
            np.random.normal(loc=0, scale=1, size=100), 
            color='b', 
            alpha=0.5)
        
## adjust the subplot layout 
plt.subplots_adjust(
    wspace=0, # the width of the padding between subplots 
    hspace=0) # the height of the padding between subplots

matplotlib subplots adjust

 

 

아래 예제에서는 plt.subplots(sharex=False, sharey=True) 로 해서 X축은 공유하지 않고 Y축만 공유하도록 했습니다. 

그리고 plt.subplots_adjust(wspace=0, hspace=0.2) 로 해서 높이의 간격(height space)에만 0.2 만큼의 간격을 부여해주었습니다. 

 

fig, axes = plt.subplots(
    nrows=2, ncols=2, 
    sharex=False, # sharing properties among x axes
    sharey=True, # sharing properties among y axes 
    figsize=(10, 10))

for i in range(2):
    for j in range(2):
        axes[i, j].hist(
            np.random.normal(loc=0, scale=1, size=100), 
            color='b', 
            alpha=0.5)
        
## adjust the subplot layout 
plt.subplots_adjust(
    wspace=0, # the width of the padding between subplots 
    hspace=0.2) # the height of the padding between subplots

 

 

 

(3) 여러개의 하위 플롯들 간의 간격을 조절해서 붙이기

      : (3-2) plt.tight_layout(h_pad=-1, w_pad=-1)

 

하위 플롯 간 간격을 조절하는 두번째 방법으로는 plt.tight_layout(h_pad, w_pad) 을 사용하는 방법입니다. plt.tight_layout(h_pad=-1, w_pad=-1)로 설정해서 위의 2번에서 했던 것처럼 4개의 하위 플롯 간에 간격이 없이 모두 붙여서 그려보겠습니다. (참고: h_pad=0, w_pad=0 으로 설정하면 하위 플롯간에 약간의 간격이 있습니다.)

 

fig, axes = plt.subplots(
    nrows=2, ncols=2, 
    sharex=True, # sharing properties among x axes
    sharey=True, # sharing properties among y axes 
    figsize=(10, 10))

for i in range(2):
    for j in range(2):
        axes[i, j].hist(
            np.random.normal(loc=0, scale=1, size=100), 
            color='b', 
            alpha=0.5)

## adjusting the padding between and around subplots
plt.tight_layout(
    h_pad=-1, # padding height between edges of adjacent subplots
    w_pad=-1) # padding width between edges of adjacent subplots

matplotlib subplots tight_layout

 

 

X축은 공유하지 않고 Y축만 공유하며, plt.tight_layout(h_pad=3, w_pad=0) 으로 설정해서 높이 간격을 벌려보겠습니다. 그리고 하위 플롯이 그려지는 순서대로 'blue', 'red', 'yellow', 'black' 의 색깔을 입혀보겠습니다. 

 

fig, axes = plt.subplots(
    nrows=2, ncols=2, 
    sharex=False, # sharing properties among x axes
    sharey=True, # sharing properties among y axes 
    figsize=(10, 10))

color = ['blue', 'red', 'yellow', 'black']
k=0
for i in range(2):
    for j in range(2):
        axes[i, j].hist(
            np.random.normal(loc=0, scale=1, size=100), 
            color=color[k], 
            alpha=0.5)
        k +=1

## adjusting the padding between and around subplots
plt.tight_layout(
    h_pad=3, # padding height between edges of adjacent subplots
    w_pad=0) # padding width between edges of adjacent subplots

 

[ Reference ]

- plt.subplots(): https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots.html
- plt.subplots_adjust(): https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.subplots_adjust.html
- plt.tight_layout(): https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.tight_layout.html

 

이번 포스팅이 많은 도움이 되었기를 바랍니다. 

행복한 데이터 과학자 되세요!

 

728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 Python의 matplotlib 모듈을 사용해서 

 

  (1) 그래프에 수평선 추가하기 (adding horizontal lines)

  (2) 그래프에 수직선 추가하기 (adding vertical lines) 

 

하는 3가지 방법을 소개하겠습니다. 

 

예제로 사용할 샘플 데이터셋을 정규분포로부터 난수를 생성해서 100개 샘플을 추출하고, 점 그래프를 그려보겠습니다. 

이 기본 점 그래프에 수평선과 수직선을 차례대로 추가해보겠습니다. 

 

import numpy as np
import matplotlib.pyplot as plt

## generating random numbers
np.random.seed(1004)
x = np.random.normal(0, 1, 100)

## plotting the original data
plt.figure(figsize = (10, 6))
plt.plot(x, linestyle='none', marker='o', color='gray')
plt.show()

 

 

 (1) 그래프에 수평선 추가하기 (adding horizontal lines)

 

(a) plt.axhline(y, xmin, xmax) : 축을 따라서 수평선을 추가, xmin 과 xmax 는 0~1 사이의 값을 가짐

(b) plt.hlines(y, xmin, xmax) : xmin ~ xmax 까지 각 y 값의 수평선을 추가

(c) plt.plot((x1, x2), (y1, y2)) : (x1, x2), (y1, y2) 좌표를 연결하는 선 추가

 

(a) 번의 plt.axhline() 은 y축에서 부터 수평선이 시작하고, xmin~xmax 로 0~1 사이의 비율 값을 가지는 반면에, (b)번의 plt.hlines() 는 xmin 값 위치에서 부터 수평선이 시작하고, xmin~xmax 값으로 좌표값을 받는다는 차이점이 있습니다. 

(c) 번의 plt.plot() 은 단지 수평선, 수직선 뿐만이 아니라 범용적으로 두 좌표를 연결하는 선을 추가할 수 있습니다. 

 

plt.figure(figsize = (10, 6))
plt.plot(x, linestyle='none', marker='o', color='gray')
plt.title("Plot with Horizontal Lines", fontsize=16)

## (1) adding a horizontal line across the axis
## xmin and xmax should be b/w 0 and 1
plt.axhline(y=3, xmin=0, xmax=1, color='blue', linestyle='solid')
plt.axhline(y=2, xmin=0.1, xmax=0.9, color='blue', linestyle='dashed')

## (2) adding a horizontal line at each y from xmin to xmax
plt.hlines(y=0, xmin=0, xmax=50, color='red', linestyle='dotted')

## (3) adding a horizontal line using (x1, x2), (y1, y2) coordinates
plt.plot((50, 100), (-2, -2), color='black', linestyle='dashdot')

plt.show()

 

horizontal lines using matplotlib

 

 

(2) 그래프에 수직선 추가하기 (adding vertical lines) 

 

(a) plt.axvline(x, ymin, ymax) : 축을 따라서 수직선을 추가, ymin 과 ymax 는 0~1 사이의 값을 가짐

(b) plt.vlines(x, ymin, ymax) : ymin ~ ymax 까지 각 x 값의 수평선을 추가

(c) plt.plot((x1, x2), (y1, y2)) : (x1, x2), (y1, y2) 좌표를 연결하는 선 추가

 

(a) 번의 plt.axvline() 은 x축에서 부터 수평선이 시작하고, ymin~ymax 로 0~1 사이의 비율 값을 가지는 반면에, (b)번의 plt.vlines() 는 ymin 값 위치에서 부터 수평선이 시작하고, ymin~ymax 값으로 좌표값을 받는다는 차이점이 있습니다. 

(c) 번의 plt.plot() 은 단지 수평선, 수직선 뿐만이 아니라 범용적으로 두 좌표를 연결하는 선을 추가할 수 있습니다. 

 

plt.figure(figsize = (10, 6))
plt.plot(x, linestyle='none', marker='o', color='gray')
plt.title("Plot with vertical Lines", fontsize=16)

## (1) adding a vertical line across the axis
## ymin and ymax should be b/w 0 and 1
plt.axvline(x=0, ymin=0, ymax=1, color='blue', linestyle='solid')
plt.axvline(x=10, ymin=0.1, ymax=0.9, color='blue', linestyle='dashed')

## (2) adding a vertical line at each y from xmin to xmax
plt.vlines(x=50, ymin=0, ymax=3, color='red', linestyle='dotted')

## (3) adding a vertical line using (x1, x2), (y1, y2) coordinates
plt.plot((100, 100), (0, -3), color='black', linestyle='dashdot')

plt.show()

 

vertical lines using matplotlib

 

 

이번 포스팅이 많은 도움이 되었기를 바랍니다. 

행복한 데이터 과학자 되세요!  :-)

 

728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 Python의 matplotlib 모듈을 사용하여, X축의 값은 동일하지만 Y축의 값은 척도가 다르고 값이 서로 크게 차이가 나는 2개의 Y값 데이터에 대해서 이중축 그래프 (plot with 2 axes for a dataset with different scales)를 그리는 방법을 소개하겠습니다. 

 

먼저 간단한 예제 데이터셋을 만들어보겠습니다. 

 

  * x 축은 2021-10-01 ~ 2021-10-10 일까지의 10개 날짜로 만든 index 값을 동일하게 사용하겠습니다. 

  * y1 값은 0~9 까지 정수에 표준정규분포 Z~N(0, 1) 로 부터 생성한 난수를 더하여 만들었습니다. 

  * y2 값은 정수 0~9에 지수를 취하여 만들었습니다. 

 

## importing modules
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt


## generating sample dataset

np.random.seed(123) # for reproducibility
idx = pd.date_range("10 1 2021", 
                     periods=10, 
                     freq="d", 
                     name="Date")

y1 = np.arange(10) + np.random.normal(0, 1, 10)
y2 = np.exp(range(10))

df = pd.DataFrame({'y1': y1, 
                   'y2': y2}, 
                  index = idx)


print(df)
#                   y1           y2
# Date                             
# 2021-10-01 -1.085631     1.000000
# 2021-10-02  1.997345     2.718282
# 2021-10-03  2.282978     7.389056
# 2021-10-04  1.493705    20.085537
# 2021-10-05  3.421400    54.598150
# 2021-10-06  6.651437   148.413159
# 2021-10-07  3.573321   403.428793
# 2021-10-08  6.571087  1096.633158
# 2021-10-09  9.265936  2980.957987
# 2021-10-10  8.133260  8103.083928

 

 

먼저, 스케일이 다른 2개의 y값을 1개의 축을 사용하여 그렸을 때 문제점을 살펴보고, 

다음으로 이를 해결하기 위한 방법 중의 하나로서 matplotlib을 사용해 2중축 그래프를 그려보겠습니다. 

 

(* 참고로, 2중축 그래프 외에 서로 다른 척도(scale)의 두개 변수의 값을 표준화(standardization, scaling) 하여 두 변수의 척도를 비교할 수 있도록 변환해준 후에 하나의 축에 두 변수를 그리는 방법도 있습니다.)

 

 

(1)  스케일이 다른 2개의 y값 변수를 1중축 그래프에 그렸을 때 문제점

==> 스케일이 작은 쪽의 y1 값이 스케일이 큰 쪽의 y2 값에 압도되어 y1 값의 패턴을 파악할 수 없음. (스케일이 작은 y1값의 시각화가 의미 없음)

 

## scale이 다른 데이터를 1개의 y축만을 사용해 그린 그래프
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot() 

ax.plot(df.index, df.y1, marker='s', color='blue')
ax.plot(df.index, df.y2, marker='o', color='red')

plt.title('Plot with 1 Axis', fontsize=16)
plt.xlabel('Date', fontsize=14)
plt.ylabel('Value', fontsize=14)
plt.legend(['y1', 'y2'], fontsize=12, loc='best')
plt.show()

plot with 1 axis for a dataset with the different scales

 

 

(2) 스케일이 다른 2개의 변수에 대해 2중축 그래프 그렸을 때

==> 각 y1, y2 변수별 스케일에 맞게 적절하게 Y축이 조정이 되어 두 변수 값의 패턴을 파악하기가 쉬움

 

이때, 가독성을 높이기 위해서 각 Y축의 색깔, Y축 tick의 색깔과 그래프의 색깔을 동일하게 지정해주었습니다. (color 옵션 사용)

 

## plot with 2 different axes for a dataset with different scales
# left side
fig, ax1 = plt.subplots()
color_1 = 'tab:blue'
ax1.set_title('Plot with 2 Axes for a dataset with different scales', fontsize=16)
ax1.set_xlabel('Date')
ax1.set_ylabel('Y1 value (blue)', fontsize=14, color=color_1)
ax1.plot(df.index, df.y1, marker='s', color=color_1)
ax1.tick_params(axis='y', labelcolor=color_1)

# right side with different scale
ax2 = ax1.twinx() # instantiate a second axes that shares the same x-axis
color_2 = 'tab:red'
ax2.set_ylabel('Y2 value (red)', fontsize=14, color=color_2)
ax2.plot(df.index, df.y2, marker='o', color=color_2)
ax2.tick_params(axis='y', labelcolor=color_2)

fig.tight_layout()
plt.show()

plot with 2 axes for a dataset with different scales

 

 

이번 포스팅이 많은 도움이 되었기를 바랍니다. 

행복한 데이터과학자 되세요!   :-)

 

728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 Python 의 matplotlib 라이브러리를 사용해서 사인 곡선, 코사인 곡선을 그려보겠습니다. 

 

(1) matplotlib, numpy 라이브러리를 사용해서 사인 곡선, 코사인 곡선 그리기

    (Plotting sine, cosine curve using python matplotlib, numpy) 

(2) x축과 y축의 눈금값(xticks, yticks), 범례(legend) 를 설정하기

(3) 그래프에 가로, 세로 그리드 선 추가하기 (adding grid lines)

 

이번 포스팅은 그리 어렵지는 않은데요, 눈금값, 범례, 그리드 선 추가와 같은 소소한 팁들이 필요할 때 참고하라고 정리해 놓습니다. 

 

 

 

(1) matplotlib, numpy 라이브러리를 사용해서 사인 곡선, 코사인 곡선 그리기

    (Plotting sine, cosine curve using python matplotlib, numpy) 

 

 numpy 로 0부터 4 pi 까지 0.1씩 증가하는 배열의 x축 값을 만들고, 이들 값들을 대입하여서  사인 y값, 코사인 z값을 생성하였습니다. 

 이들 x 값들에 대한 사인 y값, 코사인 z값을 가장 간단하게 matplotlib 으로 시각화하면 아래와 같습니다. 

 

import numpy as np
import matplotlib.pyplot as plt
# using Jupyter Notebook
%matplotlib inline

## generating x from 0 to 4pi and y using numpy library
x = np.arange(0, 4*np.pi, 0.1) # start, stop, step
y = np.sin(x)
z = np.cos(x)

## ploting sine and cosine curve using matplotlb
plt.plot(x, y, x, z)
plt.show()

 

 

 

 

(2) x축과 y축의 눈금값(xticks, yticks), 범례(legend) 를 설정하기

 

위의 (1)번 사인 곡선, 코사인 곡선을 보면 그래프 크기가 작고, x축 눈금값과 y축 눈금값이 정수로 되어있으며, x축과 y축의 라벨도 없고, 범례가 없다보니 사인 곡선과 코사인 곡선을 구분하려면 신경을 좀 써야 합니다. 

 

사인 곡선, 코사인 곡선 그래프를 보기에 좀더 가독성을 높이기 위해서 

  (a) 그래프 크기를 [12, 6] 의 가로, 세로 크기로 더 키우고, 

  (b) x축과 y축의 눈금값을 지정해주며 (xticks, yticks)

  (c) 그래프 이름, x축과 y축의 라벨 지정해주기 (title, xlabel, ylabel)

  (d) 범례 지정해주기 (legend) 

를 추가로 해보겠습니다. 

 

## 가로, 세로 그래프 크기 설정해주기
plt.rcParams['figure.figsize'] = [12, 6]

plt.plot(x, y)
plt.plot(x, z)

## xticks, yticks 값 지정해주기
plt.xticks([np.pi, np.pi*2, np.pi*3, np.pi*4],
           [r'$\pi$',r'2$\pi$',r'3$\pi$',r'4$\pi$'])
plt.yticks([-1,-0.5, 0, 0.5, 1])

## title, xlabel, ylabel지정해주기
plt.title('Sine and Cosine Plot')
plt.xlabel('x from 0 to 4pi')
plt.ylabel('sin(x) and cos(x)')

## 범례 지정해주기
plt.legend(['sin(x)', 'cos(x)'])

plt.show()

 

sine curve, cosine curve using matplotlib

 

 

 

 

(3) 그래프에 가로, 세로 그리드 선 추가하기 (adding grid lines)

 

위의 (2)번 사인, 코사인 그래프가 앞서의 (1)번 그래프 보다 훨씬 보기에 좋지요? 그럼, 여기에서 조금만 더 친절하게 가로와 세로의 그리드 점선을 추가해 보겠습니다. 

 

plt.rcParams['figure.figsize'] = [12, 6]
plt.plot(x, y)
plt.plot(x, z)
plt.xticks([np.pi, np.pi*2, np.pi*3, np.pi*4],[r'$\pi$',r'2$\pi$',r'3$\pi$',r'4$\pi$'])
plt.yticks([-1,-0.5, 0, 0.5, 1])
plt.xlabel('x from 0 to 4pi')
plt.ylabel('sin(x) and cos(x)')
plt.title('Sine and Cosine Plot')
plt.legend(['sin(x)', 'cos(x)'])

## grid line 추가하기
plt.grid(b=True, 
         which='major', # both/major/minor
         color='gray', 
         linestyle='--')

plt.show()

 

sine and cosine plot with grid, legend using matplotlib

 

이번 포스팅이 많은 도움이 되었기를 바랍니다. 

행복한 데이터 과학자 되세요!  :-)

 

728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 numpy의 meshgrid()와 matplotlib의 contourf(xx, yy, Z) 함수를 사용하여 Random Forest Classifer 의 의사결정 경계(Decision Boundary)를 시각화해보겠습니다.  


(1) iris dataset 의 species 를 분류할 수 있는 Random Forest 분류 모델 훈련

(2) numpy의 meshgrid() 로 설명변수 공간의 좌표들을 생성하여 Random Forest 분류 모델로 예측

(3) matplotlib의 contourf() 함수로 Random Forest Classifier 예측 결과 시각화(채우기)

    : 의사결정 경계(Decision Boundary)

(4) iris dataset 의 species 의 산점도를 겹쳐서 그리기


의 순서로 진행해보겠습니다. 


  (1) iris dataset 의 species 를 분류할 수 있는 Random Forest 분류 모델 훈련


먼저 iris dataset 을 업로드하고 어떻게 생긴 데이터인지 살펴보겠습니다. 



#%% import libraries

import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

import seaborn as sns


#%% iris dataset

iris = sns.load_dataset('iris')

iris.info()

<class 'pandas.core.frame.DataFrame'>

RangeIndex: 150 entries, 0 to 149

Data columns (total 5 columns):

 #   Column        Non-Null Count  Dtype  

---  ------        --------------  -----  

 0   sepal_length  150 non-null    float64

 1   sepal_width   150 non-null    float64

 2   petal_length  150 non-null    float64

 3   petal_width   150 non-null    float64

 4   species       150 non-null    object 

dtypes: float64(4), object(1)

memory usage: 6.0+ KB



iris.groupby('species').size()

Out[1]: 

species

setosa        50

versicolor    50

virginica     50

dtype: int64



#%% scatter plot by 'species'

sns.scatterplot(x='petal_length', 

                y='petal_width', 

                hue='species', 

                style='species', 

                s=100, 

                data=iris)

plt.show()



이번 포스팅의 시각화 예에서는 'sepal_length', 'sepal_width' 의 두개 칼럼을 설명변수로 사용하고, 'species'를 목표변수로 하여 붓꽂 종류(species)를 Random Forest Classifier 로 분류하는 모델을 만들어보겠습니다. 


(* 이번 포스팅의 주 목적은 모델의 의사결정경계 시각화이므로, 모델링 단계에서의 train/test set 분할, 모델 성과 평가 등은 생략합니다.)



#%% Classification using Random Forest

from sklearn.ensemble import RandomForestClassifier


X = np.array(iris[['petal_length', 'petal_width']])

y = iris['species']

y = np.where(y=='setosa', 0, np.where(y=='versicolor', 1, 2))


rfc = RandomForestClassifier(max_depth=2, n_estimators=200, random_state=1004)

rfc.fit(X, y)

 



  (2) numpy의 meshgrid() 로 설명변수 공간의 좌표들을 생성하여 

      Random Forest 분류 모델로 예측


위의 (1)번에서 적합된 Random Forest Classifier 모델을 사용해서 붓꽂 종류를 예측하고자 할때는 predict() 라는 메소드를 사용하면 됩니다. 



#%% predict

pred = rfc.predict(X)

print(pred == y)

[ True  True  True  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True  True  True  True  True False  True

  True  True  True  True  True False  True  True  True  True  True False

  True  True  True  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True  True  True  True  True False  True

  True  True  True  True  True  True  True  True  True  True  True False

  True  True  True  True  True  True  True  True  True  True  True  True

  True False False  True  True  True  True  True  True  True  True  True

  True  True  True  True  True  True]

 



(a) 이제 x 축('petal_length')과 y 축('petal_width')의 최소값, 최대값을 구하고, (b) 이들 x축과 y축의 최소값 ~ 최대값 사이의 구간을 h = 0.01 단위의 간격으로 좌표를 생성하여, (c) np.meshgrid()로 이들 x축과 y축의 h=0.01 단위의 좌표 벡터를 가지고 격자 행렬을 생성합니다. 

(d) 이렇게 생성한 격자 행렬 좌표값들에 대해 Random Forest Classifier 훈련된 모델의 predict() 메소드로 예측을 하여 contour plot의 높이에 해당하는 Z 값을 구합니다. 



# create coordinate matrices from x_min~x_max, y_min~y_max coordinates

h = 0.01

x_min, x_max = X[:, 0].min() - .1, X[:, 0].max() + .1

y_min, y_max = X[:, 1].min() - .1, X[:, 1].max() + .1

xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

XY = np.c_[xx.ravel(), yy.ravel()]


XY.shape

Out[4]: (158600, 2)


# predict

pred_cls = rfc.predict(XY)


# align the shape of Z with xx

Z = pred_cls.reshape(xx.shape)

 




  (3) matplotlib.contourf() 로 Random Forest Classifier 예측 결과 시각화

      -->  (4) iris dataset 의 species 의 산점도를 겹쳐서 그리기


위의 (2)번에서 만들어놓은 x, y 격자 행렬 좌표값별로 Random Forest Classifier 예측 결과를 matplotlib의 confourf() 함수로 시각화 (채우기)를 하면 아래와 같습니다. 아래에 색깔로 구분된 영역들과 경계선이 바로 Random Forest Classifier가 훈련 데이터로 부터 학습한 species 분류 의사결정 경계(Decision Boundary)가 되겠습니다. 



# Random Forest Classifier: Decision Boundary

plt.contourf(xx, yy, Z)

plt.axis('off')




좀더 보기에 쉽도록 위의 의사결정경계 그래프 위에다가 학습에 사용했던 iris species 값들을 species별로 marker를 구분하여 겹쳐서 다시 한번 시각화해보겠습니다. 



plt.contourf(xx, yy, Z)

plt.axis('off')


sns.scatterplot(x='petal_length', 

                y='petal_width', 

                hue='species', 

                style='species', 

                s=100, 

                data=iris)

plt.title('Decision Boundary by Random Forest', fontsize=14)

plt.show()



 



iris species 별 산점도를 의사결정경계 배경 위에 겹쳐 그릴 때 seaborn.scatterplot() 대신에 아래의 코드처럼 matplotlib.scatter() 과 for loop을 사용해서 그릴 수도 있습니다. 



plt.contourf(xx, yy, Z)

plt.axis('off')


# or plot data points using matplotlib and for loop

N = 50

CLS_NUM = 3

markers = ['o', 'x', 's']

for i in range(CLS_NUM):

    plt.scatter(X[i*N: (i+1)*N, 0], X[i*N:(i+1)*N, 1], s=40, marker=markers[i])


plt.title('Decision Boundary by Random Forest', fontsize=14)
plt.show()



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 1. plt.subplot() 2. plt.subplots() 를 이용해서 


(1) (nrows=1, ncols=3)개의 복수의 하위 막대 그래프를 그리는 방법

     (multiple subplots of bar plots using plt.subplot())

(2) 복수의 막대 그래프 간에 y 축의 단위를 고정하는 방법 (fix y axis scale)

(3) 복수의 막대 그래프 간에 y 축의 이름을 공유하는 방법 (share y axis label)

(4) 복수의 하위 그래프 간 여백을 작게 하여 밀착해서 그리는 방법 (plots in tight layout)


을 소개하겠습니다. 


그리고 마지막에는 복수의 옆으로 누운 막대 그래프 (multiple horizontal bar plots) 를 그리는 방법을 소개하겠습니다. 


먼저 예제로 사용할 간단한 DataFrame을 만들어보겠습니다. 



import numpy as np

import pandas as pd

import matplotlib.pyplot as plt


# make a sample DataFrame

grp = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c']

col = ['x1', 'x2', 'x3'] * 3

val = [3.5, 4.2, 2.9, 0.5, 0.2, 0.3, 1.5, 2.5, 2.6]


df = pd.DataFrame({'grp': grp, 

                  'col': col, 

                  'val': val})


df

grpcolval
0ax13.5
1ax24.2
2ax32.9
3bx10.5
4bx20.2
5bx30.3
6cx11.5
7cx22.5
8cx32.6

 




  1. plt.subplot() 으로 복수의 막대 그래프 그리기


(1-1) (행 1개, 열 3개) 로 구성된 복수의 하위 막대 그래프 그리기 

        (multiple subplots of bar plots using plt.subplot())


plt.subplot(nrow, ncol, position) 의 형태로 행과 열의 위치에 여러개의 하위 그래프를 그릴 수 있습니다. 


아래의 예제에서는 고쳤으면 하는 3가지 문제점이 보이네요. 

첫째 문제는 Y 축의 단위(y axis scale)가 서로 다르다 보니 비교를 하기가 힘듭니다. 

둘째 문제는 X축과 Y축의 이름(label)과 단위 눈금(ticks)이 반복해서 나타나서 지저분해 보입니다. 

셋째 문제는 복수의 하위 그래프 간 간격이 떨어져 있어서 좀 작게 보입니다. 


아래에 차례대로 이들 문제를 해결해 보겠습니다. 



# (1-1) multiple bar plots with different y axis scale

plt.rcParams['figure.figsize'] = [12, 6]


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    plt.xlabel('X variable', fontsize=14)

    plt.ylabel('Value', fontsize=14)

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)






(1-2) 복수의 막대 그래프 간에 y 축의 단위를 고정하는 방법 (fix y axis scale)


plt.ylim(min, max) 의 형태로 y 축의 단위를 설정 또는 고정 (set/ fix y axis scale) 할 수 있습니다. 



 # (1-2) Set fixed y axis scale and Share it together

plt.rcParams['figure.figsize'] = [12, 6]

max_val = np.ceil(max(df['val']))


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    plt.xlabel('X variable', fontsize=14)

    plt.ylabel('Value', fontsize=14)

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)

    # set fixed y axis scale

    plt.ylim(0, max_val)





(1-3) 복수의 막대 그래프 간에  X, Y 축의 이름을 공유하는 방법 (share X and Y axis label)


if 조건문을 사용하여 X축의 이름(X label)은 중간 위치 (index = 1)의 하위 그래프만 나타나게 하고, Y축의 이름(Y label)은 첫번째 그래프(index = 0) 에만 나타나게 하면 X축과 Y축의 이름을 서로 공유한 것과 같은 효과를 낼 수 있습니다. 



# (1-3) Display only 1 X and Y label

plt.rcParams['figure.figsize'] = [12, 6]

max_val = np.ceil(max(df['val']))


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    

    # display only 1 X and Y label

    if i == 1:

        plt.xlabel('X variable', fontsize=14)

    if i == 0:

        plt.ylabel('Value', fontsize=14)

    if i != 0:

        plt.yticks([])

    

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)

    plt.ylim(0, max_val)





(1-4) 복수의 하위 그래프 간 여백을 작게 하여 밀착해서 그리는 방법 (plots in tight layout)


plt.tight_layout() 메소드를 이용하여 복수의 하위 그래프 간 여백 간격을 좁게하여 밀집된 형태로 좀더 크게 복수의 그래프를 그릴 수 있습니다. 



# (1-4) Display multiple plots in Tight Layout

plt.rcParams['figure.figsize'] = [12, 6]

max_val = np.ceil(max(df['val']))


for i, grp in enumerate(['a', 'b', 'c']):

    df_i = df[df['grp'] == grp]

    plt.subplot(1, 3, i+1)

    plt.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    plt.title('Group: %s' %grp, fontsize=18)

    

    # display only 1 X and Y label

    if i == 1:

        plt.xlabel('X variable', fontsize=14)

    if i == 0:

        plt.ylabel('Value', fontsize=14)

    if i != 0:

        plt.yticks([])

    

    plt.xticks(fontsize=12)

    plt.yticks(fontsize=12)

    plt.ylim(0, max_val)


# display in tight layout

plt.tight_layout()





  2. plt.subplots() 로 복수의 막대 그래프 그리기


plt.subplots() 는 위의 plt.subplot() 보다 좀더 적은 코드로 깔끔하게 복수의 하위 막대그래프를 그릴 수 있습니다. 

  • (nrows, ncols) 로 하위 그래프를 그릴 행과 열을 지정해줍니다. 
  • sharey=True 로 y 축 공유, sharex=True 로 복수 그래프 간 x 축을 공유할 수 있습니다. (편리!)
  • if 조건문과 함께 사용해서 ax.set_ylabel(), ax.set_xlabel() 로 y축과 x축의 이름을 하나만 나타나게 하였습니다. 
  • plt.tight_layout() 로 복수의 하위 그래프 간 여백을 좁게 해서 그래프를 그렸습니다. 


# (2-1) Multiple bar plots

# (2-2) Use fixed y axis scale

# (2-3) Display only 1 X and Y label

# (2-4) Display multiple plots in Tight Layout

fig, axes = plt.subplots(nrows=1

                         , ncols=3

                         , sharey=True

                         , figsize=(12,6))


grp = ['a', 'b', 'c']


for i, ax in enumerate(axes):

    df_i = df[df['grp'] == grp[i]]

    ax.bar(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    ax.set_title("Group: {grp}".format(grp=grp[i]), fontsize=18)

    if i == 0:

        ax.set_ylabel("Value", fontsize=14)

    if i == 1:

        ax.set_xlabel("X variable", fontsize=14)

        

plt.tight_layout()





  3. 복수의 옆으로 누운 막대 그래프 (multiple horizontal bar plot) 그리기


ax.barh() 를 사용하여 옆으로 누운 막대 그래프 (horizontal bar plot)을 그렸으며, 복수개의 하위 그래프 그리는 방법은 2번에서 소개한 plt.subplots() 함수를 차용하였습니다. 


이때 옆으로 누운 막대 그래프이기 때문에, 가독성을 높이기 위해서 그룹의 이름을 제목(title)이 아니라 Y 축 이름(y label) 에 표시를 하였습니다. 



# Horizontal Multiple Bar Plots using plt.subplots()

fig, axes = plt.subplots(nrows=3

                         , ncols=1

                         , sharex=True

                         , figsize=(8,10))


grp = ['a', 'b', 'c']


for i, ax in enumerate(axes):

    df_i = df[df['grp'] == grp[i]]

    ax.barh(df_i['col'], df_i['val'], color='blue', alpha=0.5)

    #ax.set_title("Group: {grp}".format(grp=grp[i]), fontsize=18)

    ax.set_ylabel("Group: {grp}".format(grp=grp[i]), fontsize=18)

    if i == 2:

        ax.set_xlabel("Value", fontsize=14)

        

plt.tight_layout()


 



많은 도움이 되었기를 바랍니다. 

이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

Plotly는 퀘벡 몬트리올에 본사가 있는 온라인 데이터 분석과 시각화 툴을 개발하는 테크 회사의 이름이기도 하구요, 분석/ 시각화 라이브러리의 이름이기도 합니다. Plotly 툴은 Python과 Django 프레임워크를 사용했고, 프런트엔드는 JavaScript, 시각화 라이브러리는 D3.js, HTML, CSS를 사용하여 만들어졌습니다. ((https://plot.ly/)


Plotly는 그래프가 (다른 시각화 라이브러리 대비) 아름답구요, 웹 상에 publish 하여 interactive visualization 용도로 사용하는데 매우 훌륭합니다. 


특히 Plotly Dash는 웹 기반 분석 애플리케이션 개발을 위한 오픈소스 파이썬 프레임워크인데요, JavaScript 를 안쓰고도 Python 코드 몇 백 줄로 Data Scientist가 디자이너 도움없이 매우 아름답고 완성도 높은 interactive analytics web application을 짧은 시간안에 만들 수 있어서 매우 매력적입니다. (for more information: https://dash.plot.ly/)


이번 포스팅에서는 웹에 publish 하는 것이 아니고, 로컬 컴퓨터에서 Jupyter Notebook에 offline으로 Plotly 라이브러리를 사용해서 시각화를 하는 방법을 소개하고자 합니다. 



  1. Plotly python package 설치(installation) 및 업그레이드(upgrade)


Plotly를 처음 사용하는 것이면 프롬프트 창에서 먼저 설치를 해야 합니다. 

 

$ pip install plotly




이미 Plotly를 설치해서 사용하고 있는 사용자라면, Plotly가 자주 버전 업그레이드를 하므로 아래처럼 Plotly를 업그레이트를 먼저 해주는 것을 추천합니다. 



$ pip install plotly --upgrade

 




  2. Plotly를 오프라인에서 사용하기 위한 라이브러리 import 및 환경 설정



# import plotly standard

import plotly.plotly as py

import plotly.graph_objs as go

import plotly.figure_factory as ff


# Cufflinks wrapper on plotly

import cufflinks as cf


# Display all cell outputs

from IPython.core.interactiveshell import InteractiveShell


# plotly + cufflinks in offline mode

from plotly.offline import iplot

cf.go_offline()


# set the global theme

cf.set_config_file(world_readable=True, theme='pearl', offline=True)

 




저의 로컴 컴퓨터에서 오프라인 Jupyter Notebook으로 Plotly의 interactive visualization 을 (1) 히스토그램(histogram), (2) 산점도행렬(scatterplot matrix) 의 두개 예를 들어보겠습니다. 


예제 데이터는 iris 데이터프레임입니다. seaborn 패키지에서 iris 데이터셋을 불러오겠습니다. 



import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

 



# data loading

iris = sns.load_dataset('iris')

iris.shape

(150, 5) 




iris.head()

sepal_lengthsepal_widthpetal_lengthpetal_widthspecies
05.13.51.40.2setosa
14.93.01.40.2setosa
24.73.21.30.2setosa
34.63.11.50.2setosa
45.03.61.40.2setosa




  (3) Plotly를 오프라인 Jupyter Notebook에서 사용한 히스토그램(histogram)


(3-1) 각 bin별 빈도(count, frequency)로 히스토그램 그리기


'petal_length' 변수에 대해서 iplot() 함수를 사용하여 20개의 bin으로 나누어서 빈도(count)를 기준으로 히스토그램을 그렸습니다.  아래는 화면 캡쳐한 이미지이다 보니 고정된 그래프인데요, Jupyter Notebook에서 마우스를 그래프 위에 가져다 대면 각 bin의 구간과 빈도가 화면에 interactive하게 나타납니다. 특정 구역을 마우스로 블럭을 설정하면 그 부분만 확대되어서 나타납니다(hover 기능). 



# Histogram with Frequency

iris['petal_length'].iplot(

    kind='hist', 

    bins=20, 

    xTitle='Petal Length(cm)', 

    linecolor='gray', 

    yTitle='Count', 

    title='Histogram of Petal Length')





(3-2) 각 bin별 구성비율로 히스토그램 그리기



# Histogram with Percentage

iris['petal_length'].iplot(

    kind='hist', 

    bins=20, 

    xTitle='Petal Length(cm)', 

    linecolor='gray', 

    histnorm='percent',

    yTitle='Percentage(%)', 

    title='Histogram of Petal Length in Percent')



 




  (4) Plotly를 오프라인 Jupyter Notebook에서 사용한 산점도 행렬(scatterplot matrix)


아래의 Plotly로 그린 산점도 행렬도 마우스를 가져다대면 interactive하게 해당 값이 화면에 나와서 바로 확인을 할 수 있습니다. 



fig = ff.create_scatterplotmatrix(

    iris[['petal_width', 'petal_length', 'sepal_width', 'sepal_length']],

    height=800,

    width=800, 

    diag='histogram') # scatter, histogram, box

iplot(fig)



 



Plotly로 그릴 수 있는 다양한 그래프 예제는 https://plot.ly/d3-js-for-python-and-pandas-charts/ 를 참고하시기 바랍니다. 


많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요.



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 R ggplot2 패키지의 함수를 Python에서 그대로 사용하여 그래프를 그릴 수 있게 해주는 Python의 PlotNine library를 소개하겠습니다. 


R ggplot2 패키지의 그래프 기능이 굉장히 강력하고 체계적이며 이쁘기 때문에 많은 분석가들이 시각화 도구로 R ggplot2를 사용하고 있습니다. 저도 R ggplot2와 RShiny 의 굉장한 팬이기도 합니다. R ggplot2에 이미 익숙한 사용자라면 R ggplot2 함수를 Python에서 그대로 사용하여 시각화를 할 수 있다면 매우 편리하고 빠르게 시각화를 할 수 있을 것입니다. 



연도별 월별 항공기 탑승객 수를 저장해놓은 flights 데이터프레임을 사용하여 파이썬의 PlotNine 라이브러리를 사용하여 파이썬에서 R ggplot2 함수로 선 그래프와 히트맵을 그려보겠습니다. 



import matplotlib.pyplot as plt

import seaborn as sns

plt.rcParams['figure.figsize'] = [10, 8]

 



먼저 flights 데이터프레임을 seaborn 라이브러리에서 가져와 로딩하였습니다. 



# data loading

flights = sns.load_dataset('flights')

flights.head()

yearmonthpassengers
01949January112
11949February118
21949March132
31949April129
41949May121

 




PlotNine 라이브러리를 처음 사용한다면 아래처럼 명령 프롬프트에서 pip install plotnine 으로 설치해주세요. 


  명령 프롬프트에서 'PlotNine' 라이브러리 설치하기



Microsoft Windows [Version 10.0.17134.523]

(c) 2018 Microsoft Corporation. All rights reserved.


C:\Users\admin>conda env list

# conda environments:

#

base                  *  C:\Users\admin\Anaconda3

py_v35                   C:\Users\admin\Anaconda3\envs\py_v35

py_v36                   C:\Users\admin\Anaconda3\envs\py_v36



C:\Users\admin>activate py_v36


(py_v36) C:\Users\admin>pip install plotnine

 





  (1) PlotNine 라이브러리를 사용하여 파이썬에서 R ggplot2 함수로 선 그래프 그리기


일단 plotnine에서 import * 로 모든 클래스를 불러오고 나면 그 다음부터는 R ggplot2 함수를 정확히 똑같이 사용해서 jupyter notebook에서 파이썬 데이터프레임으로 선 그래프를 그릴 수 있습니다. 신기하고 놀랍지요?!  PlotNine 라이브러리 만들어주신 분들에게 깊은 감사를 드립니다. ^^



# line graph using R ggplot2 function in Python environment by plotnine library

from plotnine import *


fig = plt.figure()


ggplot(flights, aes(x='year', y='passengers', group='month', color='month')) \

    + geom_line() \

    + ggtitle('Time Series Graph of Flight')

 






  (2) PlotNine 라이브러리를 사용하여 파이썬에서 R ggplot2 함수로 히트맵 그리기



# heatmap using R ggplot2 function in Python environment by plotnine library

from plotnine import *


fig = plt.figure()


ggplot(flights, aes(x='year', y='month')) \

    + geom_tile(aes(fill='passengers')) \

    + scale_fill_gradientn(colors=['#9ebcda','#8c6bb1','#88419d','#6e016b']) \

    + ggtitle('Heatmap of Flights by plotnine')



많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요.  :-)



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 X축과 Y축에 2개의 범주형 자료의 계급(class)별로 연속형 자료를 집계한 자료를 사용하여, 집계한 값에 비례하여 색깔을 다르게 해서 2차원으로 자료를 시각화하는 히트맵(Heatmap)을 그려보겠습니다. 


기본적인 Python 라이브러리를 importing 하였으며, matplotlib, seaborn, pandas 의 순서대로 히트맵 그리는 방법을 소개하겠습니다. 



import numpy as np

import pandas as pd


import matplotlib.pyplot as plt

import seaborn as sns

plt.rcParams['figure.figsize'] = [10, 8]

 




예제로 사용할 데이터는 연도(year)별 월(month)별 승객 수(passengers)를 모아놓은 flights 라는 seaborn에 내장되어 있는 데이터프레임입니다. 



flights = sns.load_dataset('flights')


flights.head()

yearmonthpassengers
01949January112
11949February118
21949March132
31949April129
41949May121

 




pivot() 함수를 이용하여 월별 연도별 승객수를 집계한 피벗 테이블(pivot table)을 만들어서, 이를 이용해서 이어서 히트맵을 그려보겠습니다. 



df = flights.pivot('month', 'year', 'passengers')


df.head()

year194919501951195219531954195519561957195819591960
month
January112115145171196204242284315340360417
February118126150180196188233277301318342391
March132141178193236235267317356362406419
April129135163181235227269313348348396461
May121125172183229234270318355363420472

 




  (1) matplotlib을 이용한 히트맵 그리기 (Heatmap by matplotlib)


plt.pcolor() 함수를 이용하여 히트맵을 그리고, plt.colorbar() 로 색상에 대한 정보를 오른쪽에 제시해줍니다. 



# heatmap by plt.pcolor()

plt.pcolor(df)

plt.xticks(np.arange(0.5, len(df.columns), 1), df.columns)

plt.yticks(np.arange(0.5, len(df.index), 1), df.index)

plt.title('Heatmap by plt.pcolor()', fontsize=20)

plt.xlabel('Year', fontsize=14)

plt.ylabel('Month', fontsize=14)

plt.colorbar()


plt.show()




  (2) seaborn을 이용한 히트맵 그리기 (Heatmap by seaborn)


seaborn은 데이터가 2차원 피벗 테이블 형태의 DataFrame으로 집계가 되어 있으면 sns.heatmap() 함수로 매우 간단하게 히트맵을 그려줍니다. 



# heatmap by seaborn

ax = sns.heatmap(df)

plt.title('Heatmap of Flight by seaborn', fontsize=20)

plt.show() 




이번에는 annot=True argument를 써서 각 셀에 숫자를 입력(annotate each cell with numeric value)하여 보겠습니다. fmt='d' 는 정수 형태(integer format)로 숫자를 입력하라는 뜻입니다. 



# annotate each cell with the numeric value of integer format

sns.heatmap(df, annot=True, fmt='d')

plt.title('Annoteat cell with numeric value', fontsize=20)

plt.show()





cmap 에 색깔을 다르게 설정해보겠습니다. 

(color maps in Matplotlib 에 대한 자세한 설명은 다음 링크를 참고하세요 ==> https://matplotlib.org/3.2.1/tutorials/colors/colormaps.html )



# different colormap

sns.heatmap(df, cmap='RdYlGn_r')

plt.title('colormap of cmap=RdYlGn_r', fontsize=20)

plt.show()




히트맵의 색깔을 cmap='YIGnBu' 로 설정해서 파란색과 노란색 계열로 바꾸어보겠습니다. 



# different colormap

sns.heatmap(df, cmap='YlGnBu') # 

plt.title('colormap of cmap=YlGnBu', fontsize=20)

plt.show()




색깔 지도(color map)의 중심을 1949년 1월(January)으로 맞추어서 히트맵을 그려보겠습니다. 좌측 최상단이 1949년 1월 (1949 January) 로서 히트맵 색의 중심 위치가 되었습니다. 



# center the colormap at a specific value

sns.heatmap(df, center=df.loc['January', 1949])

plt.title('Center the colormap at Jan. 1949', fontsize=20)

plt.show()





  (3) pandas를 이용한 히트맵 그리기 (Heatmap by pandas)


pandas는 df.style.background_gradient(cmap='summer')를 사용해서 DataFrame에 숫자에 따라서 직접 색을 다르게 입힐 수 가 있습니다. 마치 엑셀에서 피벗 테이블한 다음에 숫자에 따라서 색을 다르게 입히는 것과 유사하게요. 



# heatmap by pandas

df.style.background_gradient(cmap='summer')



이상으로 Python으로 히트맵 그리기를 마치겠습니다. 

많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. :-)



728x90
반응형
Posted by Rfriend
,

이번 포스팅에서는 두개 이상의 다변량 범주형 자료 시각화(visualization with multiple categorical data)의 하나로서 모자이크 그래프 (mosaic chart)를 그리는 방법을 소개하겠습니다. 




statsmodels 라이브러리의 statsmodels.graphics.mosaicplot 내 mosaic 클래스를 사용하면 매우 간단한 코드로 그릴 수 있습니다. 



import numpy as np

import pandas as pd


from statsmodels.graphics.mosaicplot import mosaic

import matplotlib.pyplot as plt

import seaborn as sns

plt.rcParams['figure.figsize'] = [12, 8]

 



예제로 사용할 데이터는 Titanic 침몰로 부터 생존/사망자 데이터셋입니다. 몇 년 전에 Kaggle에서 생존 vs. 사망 분류 모델 만들기 competition을 했었던 데이터입니다. 



# Getting Titanic dataset

url = "https://raw.github.com/mattdelhey/kaggle-titanic/master/Data/train.csv"

titanic = pd.read_csv(url)

titanic.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 11 columns):
survived    891 non-null int64
pclass      891 non-null int64
name        891 non-null object
sex         891 non-null object
age         714 non-null float64
sibsp       891 non-null int64
parch       891 non-null int64
ticket      891 non-null object
fare        891 non-null float64
cabin       204 non-null object
embarked    889 non-null object
dtypes: float64(2), int64(4), object(5)
memory usage: 76.6+ KB

 



모자이크 그래프 시각화에 사용할 3개의 범주형 변수만 남겨놓았으며, 변수 이름과 코드값을 map() 함수를 사용하여 바꾸어보았습니다. 



titanic = titanic[['survived', 'pclass', 'sex']]


# make new variables of 'survived' and 'pclass' with the different class name

titanic["SURVIVE"] = titanic.survived.map({0: "DEAD", 1: "ALIVE"})

titanic["CLASS"] = titanic.pclass.map({1: "1ST", 2: "2ND", 3: "3RD"})

titanic["GENDER"] = titanic.sex.map({'male': 'MAN', 'female': "WOMAN"})


titanic.head()

survivedpclasssexSURVIVECLASSGENDER
003maleDEAD3RDMAN
111femaleALIVE1STWOMAN
213femaleALIVE3RDWOMAN
311femaleALIVE1STWOMAN
403maleDEAD3RDMAN

 




mosaic() 함수를 사용하여 생존여부('SURVIVE')와 티켓 등급('CLASS') 간의 관계를 알아볼 수 있는 모자이크 그래프를 그려보았습니다. 'CLASS' 변수의 코드값을 titanic.sort_values() 로 먼저 정렬을 한 후에 모자이크 그림을 그렸습니다. 1등석('1ST') > 3등석('3RD) > 2등석('2ND')의 순서로 생존율이 높게 나왔군요. 



from statsmodels.graphics.mosaicplot import mosaic

 

mosaic(titanic.sort_values('CLASS'), ['SURVIVE', 'CLASS'], 

      title='Mosaic Chart of Titanic Survivor')

plt.show()




이번에는 생존 여부('SURVIVE')와 성별('GENDER')와의 관계를 알아볼 수 있는 모자이크 그래프를 그려보았습니다. '여성('WOMEN')'의 생존자 비율이 높게 나왔습니다. 



mosaic(titanic, ['SURVIVE', 'GENDER'])

plt.title('Mosaic Chart of Titanic', fontsize=20)

plt.show()




생존 여부('SURVIVE'), 티켓 등급('CLASS'), 성별('GENDER') 3개 범주형 변수를 모두 한꺼번에 사용해서 모자이크 그림을 그릴 수도 있습니다. 



mosaic(titanic.sort_values('CLASS'), ['CLASS', 'SURVIVE', 'GENDER'])

plt.title('Mosaic Chart of Titanic', fontsize=20)

plt.show()

 





조금 더 가독성을 높이기 위해서 gap argument를 사용하여 변수 내 계급 간에 간극(gap)을 좀더 벌려서 모자이크 그림을 그릴 수 있습니다. 



mosaic(titanic.sort_values('CLASS'), ['CLASS', 'SURVIVE', 'GENDER'], 

      gap=0.02)

plt.title('Survivor of Titanic', fontsize=20)

plt.show()

 



많은 도움이 되었기를 바랍니다. 


이번 포스팅이 도움이 되었다면 아래의 '공감~'를 꾹 눌러주세요. ;-)



728x90
반응형
Posted by Rfriend
,