꺼내먹는지식 준

Matplotlib 기본기에서 벗어나는 몇가지 팁 본문

CS/데이터시각화

Matplotlib 기본기에서 벗어나는 몇가지 팁

알 수 없는 사용자 2022. 2. 7. 15:33

기본적 Grid 는 축과 평행한 선을 사용하여 거리 및 갑 정보를 보조적으로 제공 

 

Default Grid

색(color): 무채색 (다른 표현 방해 안하도록)

순서(zorder): Layer 순서 맨 아래 

큰 격자/ 세부 격자(which='major', 'minor', 'both')

X축 Y축(axis = 'x','y','both')

  • which : major ticks, minor ticks
  • axis : x, y
  • linestyle
  • linewidth
  • zorder
np.random.seed(970725)

x = np.random.rand(20)
y = np.random.rand(20)


fig = plt.figure(figsize=(16, 7))
ax = fig.add_subplot(1, 1, 1, aspect=1)


ax.scatter(x, y, s=150, 
           c='#1ABDE9',
           linewidth=1.5,
           edgecolor='black', zorder=10)


# ax.set_xticks(np.linspace(0, 1.1, 12, endpoint=True), minor=True)

ax.set_xlim(0, 1.1)
ax.set_ylim(0, 1.1)

    
ax.grid(zorder=0, linestyle='--')    
ax.set_title(f"Default Grid", fontsize=15,va= 'center', fontweight='semibold')

plt.tight_layout()
plt.show()

 

다양한 타입 Grid

1) 두 변수의 합이 중요하면 x+y = c 

feature의 절대적 합이 중요할 때 

ex. 국어 + 수학 비중 평가 

fig = plt.figure(figsize=(16, 7))
ax = fig.add_subplot(1, 1, 1, aspect=1)


ax.scatter(x, y, s=150, 
           c=['#1ABDE9' if xx+yy < 1.0 else 'darkgray' for xx, yy in zip(x, y)],
           linewidth=1.5,
           edgecolor='black', zorder=10)

## Grid Part
x_start = np.linspace(0, 2.2, 12, endpoint=True)

for xs in x_start:
    ax.plot([xs, 0], [0, xs], linestyle='--', color='gray', alpha=0.5, linewidth=1)


ax.set_xlim(0, 1.1)
ax.set_ylim(0, 1.1)

ax.set_title(r"Grid ($x+y=c$)", fontsize=15,va= 'center', fontweight='semibold')

plt.tight_layout()
plt.show()

2) 비율이 중요하다면 y = cx

가파를 수록 y/x 가 커짐 

ex. 국어 수학 성적 비율 등

fig = plt.figure(figsize=(16, 7))
ax = fig.add_subplot(1, 1, 1, aspect=1)


ax.scatter(x, y, s=150, 
           c=['#1ABDE9' if yy/xx >= 1.0 else 'darkgray' for xx, yy in zip(x, y)],
           linewidth=1.5,
           edgecolor='black', zorder=10)

## Grid Part
radian = np.linspace(0, np.pi/2, 11, endpoint=True)

for rad in radian:
    ax.plot([0,2], [0, 2*np.tan(rad)], linestyle='--', color='gray', alpha=0.5, linewidth=1)


ax.set_xlim(0, 1.1)
ax.set_ylim(0, 1.1)

ax.set_title(r"Grid ($y=cx$)", fontsize=15,va= 'center', fontweight='semibold')

plt.tight_layout()
plt.show()

3) 두 변수의 곱이 중요하다면 xy = c

 

4) 특정 데이터 중심: (x - x')^2 + (y-y')^2 = c 

특정 지점에서 거리를 살펴볼 수 있다. 

가장 가까운 포인트 혹은 한 데이터에서 특정 범위의 데이터 파악 가능 

fig = plt.figure(figsize=(16, 7))
ax = fig.add_subplot(1, 1, 1, aspect=1)


ax.scatter(x, y, s=150, 
           c=['darkgray' if i!=2 else '#1ABDE9'  for i in range(20)] ,
           linewidth=1.5,
           edgecolor='black', zorder=10)

## Grid Part
rs = np.linspace(0.1, 0.8, 8, endpoint=True)

for r in rs:
    xx = r*np.cos(np.linspace(0, 2*np.pi, 100))
    yy = r*np.sin(np.linspace(0, 2*np.pi, 100))
    ax.plot(xx+x[2], yy+y[2], linestyle='--', color='gray', alpha=0.5, linewidth=1)

    ax.text(x[2]+r*np.cos(np.pi/4), y[2]-r*np.sin(np.pi/4), f'{r:.1}', color='gray')

ax.set_xlim(0, 1.1)
ax.set_ylim(0, 1.1)

ax.set_title(r"Grid ($(x-x')^2+(y-y')^2=c$)", fontsize=15,va= 'center', fontweight='semibold')

plt.tight_layout()
plt.show()


심플한 처리 

선 추가 

평균, 상한 하한 표기, 기울기 표현 

 

면 추가 

구간을 설명하기 위해 추가가능 

 

Setting 바꾸기 

fivethirtyeight, ggplot 등 테마를 사용 

 

rc: run command (setting 커맨드로 자주 사용)

기본 세팅 변경 

 

mpl

plt

둘다 동일하게 작동한다. 

기본 해상도만 높이는데 default setting 을 사용해도 좋다. 

 

 

'CS > 데이터시각화' 카테고리의 다른 글

Matplotlib Text Visualization  (0) 2022.02.07
Matplotlib Color  (0) 2022.02.07
Matplotlib Scatter Plot  (0) 2022.02.04
Matplotlib Line Plot  (0) 2022.02.03
Matplotlib Bar Plot  (0) 2022.02.03
Comments