(3-4) More Tips

210822

1. Grid ์ดํ•ดํ•˜๊ธฐ

1.1 Default Grid

๊ธฐ๋ณธ์ ์ธ Grid๋Š” ์ถ•๊ณผ ํ‰ํ–‰ํ•œ ์„ ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฑฐ๋ฆฌ ๋ฐ ๊ฐ‘ ์ •๋ณด๋ฅผ ๋ณด์กฐ์ ์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.

  • ์ƒ‰์€ ๋‹ค๋ฅธ ํ‘œํ˜„๋“ค์„ ๋ฐฉํ•ดํ•˜์ง€ ์•Š๋„๋ก ๋ฌด์ฑ„์ƒ‰

  • ํ•ญ์ƒ Layer ์ˆœ์„œ ์ƒ ๋งจ ๋ฐ‘์— ์˜ค๋„๋ก zorder๋ฅผ ์กฐ์ •

  • ํฐ ๊ฒฉ์ž์™€ ์„ธ๋ถ€ ๊ฒฉ์ž๋Š” which= ๋กœ major ์™€ minor ๊ทธ๋ฆฌ๊ณ  both ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค

  • X์ถ•, Y์ถ•๋„ axis= ๋กœ x ์™€ y ๊ทธ๋ฆฌ๊ณ  both ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

1.2 ๋‹ค์–‘ํ•œ ํƒ€์ž…์˜ Grid

์ „ํ˜•์ ์ธ Grid๋Š” ์•„๋‹ˆ์ง€๋งŒ ์—ฌ๋Ÿฌ ํ˜•ํƒœ๋กœ ์กด์žฌํ•œ๋‹ค.

  • ๋‘ ๋ณ€์ˆ˜์˜ ํ•ฉ์ด ์ค‘์š”ํ•˜๋‹ค : x+y=c

  • ๋น„์œจ์ด ์ค‘์š”ํ•˜๋‹ค : y=cx

  • ๋‘ ๋ณ€์ˆ˜์˜ ๊ณฑ์ด ์ค‘์š”ํ•˜๋‹ค : xy = c

  • ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์ค‘์‹ฌ์œผ๋กœ ๋ณด๊ณ  ์‹ถ๋‹ค : (x-x')^2 + (y-y')^2 = c

  • ์—ฌ๊ธฐ์— ๋งŽ์€ ์˜ˆ์‹œ๊ฐ€ ์žˆ๋‹ค.

๊ธฐ๋ณธ์ ์ธ Grid

๋‘ ๋ณ€์ˆ˜์˜ ํ•ฉ์ด ์ค‘์š”ํ•œ ๊ทธ๋ฆฌ๋“œ

  • ์ถ•๊ตฌ์„ ์ˆ˜์˜ ๊ณต๊ฒฉ๋ ฅ๊ณผ ์ˆ˜๋น„๋ ฅ ๋น„๊ต

  • ๊ณ ๋“ฑํ•™์ƒ์˜ ์ˆ˜ํ•™์ ์ˆ˜์™€ ์˜์–ด์ ์ˆ˜ ๋น„๊ต

๋น„์œจ์ด ์ค‘์š”ํ•œ ๊ทธ๋ฆฌ๋“œ

  • ๊ฐ€ํŒŒ๋ฅผ์ˆ˜๋ก Y/X ๊ฐ€ ์ปค์ง€๋ฉฐ, Feature์˜ ๋น„์œจ์ด ์ค‘์š”ํ•œ ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

  • ํˆฌ์ž๋Œ€๋น„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์˜ˆ์ƒ ์ˆ˜์ต๋Ÿ‰

ํŠน์ • ๋ฐ์ดํ„ฐ๊ฐ€ ์ค‘์‹ฌ์ธ ๊ทธ๋ฆฌ๋“œ

  • ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ํ‘œํ˜„ํ•  ๋•Œ ์ข‹๋‹ค.

  • circle ๋กœ๋„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ plot ์œผ๋กœ ํ‘œํ˜„ํ•  ๊ฒƒ์ž„

2. ์‹ฌํ”Œํ•œ ์ฒ˜๋ฆฌ

2.1 ์„  ์ถ”๊ฐ€ํ•˜๊ธฐ

  • ์ƒํ•œ๊ณผ ํ•˜ํ•œ์„ ํ‘œํ˜„ํ•˜๋Š” ๋Œ€์‹œ์„ 

  • ํ‰๊ท ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ ์„ 

  • ์›์ ์ด ์ค‘์‹ฌ์ด๋ผ๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด y์ถ•๊ณผ ํ‰ํ–‰ํ•˜๊ฒŒ ๊ทธ์€ ์‹ค์„ 

2.2 ๋ฉด ์ถ”๊ฐ€ํ•˜๊ธฐ

Netflix ์˜ํ™” ์ƒ์˜ ๋“ฑ๊ธ‰ ๋ถ„ํฌ
  • ์—ฐ๋ น๋ณ„๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋‹ˆ ๊ฐ€๋…์„ฑ์ด ์ข‹์•„์ง€๋Š” ๋ชจ์Šต

3. Setting ๋ฐ”๊พธ๊ธฐ

3.1 Theme

๋Œ€ํ‘œ์ ์œผ๋กœ fivethirtyeight์ด๋‚˜ ggplot์„ ์‚ฌ์šฉํ•œ๋‹ค.

3-4-More Information

์ •๋ณด๋Ÿ‰์„ ๋Š˜๋ฆฌ๊ณ , ๋” ๊น”๋”ํ•œ matplotlib์„ ์œ„ํ•œ ํŒ๋“ค์„ ์•Œ์•„๋ด…์‹œ๋‹ค.

1. Grid

๊ฐ•์˜์—์„œ ์†Œ๊ฐœํ•œ ๋‚ด์šฉ์„ ์ฝ”๋“œ๋กœ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt

1-1. grid

๊ธฐ๋ณธ์ ์ธ Grid๋ถ€ํ„ฐ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์ธ ๊ทธ๋ฆฌ๋“œ์—์„œ๋Š” ๋‹ค์Œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

  • which : major ticks, minor ticks

  • axis : x, y

  • linestyle

  • linewidth

  • zorder

fig, ax = plt.subplots()
ax.grid()
plt.show()
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)

# scatter๋Š” ์ ์ด ๋„ˆ๋ฌด ํฌ๋ฉด grid์™€ ์ž˜ ์–ด์šธ๋ฆฌ์ง€ ์•Š๋Š”๋‹ค
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)

# ๊ฒน์น˜์ง€ ์•Š๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด zorder๋ฅผ ์„ค์ •, plot์ฒ˜๋Ÿผ linestyle ์‚ฌ์šฉ๊ฐ€๋Šฅ
# axis๋กœ ์ถ•์„ ์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค : y, x, both
# linewidth๋กœ grid์˜ ์„  ๋‘๊ป˜๋ฅผ ์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ž‘์œผ๋ฉด ์„ธ๋ จ๋˜๋ณด์ด๊ณ  ํฌ๋ฉด ๋งŒํ™”๊ฐ™์•„ ๋ณด์ธ๋‹ค
ax.grid(zorder=0, linestyle='--')    
ax.set_title(f"Default Grid", fontsize=15,va= 'center', fontweight='semibold')

plt.tight_layout()
plt.show()

1-2. x + y = c

๊ทธ๋ฆฌ๋“œ ๋ณ€๊ฒฝ์€ grid ์†์„ฑ์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ ์กด์žฌํ•˜์ง€๋งŒ ๊ฐ„๋‹จํ•œ ์ˆ˜์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ๊ทธ๋ฆด ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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()

1-3. y = cx

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()

1-4. ๋™์‹ฌ์›

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()

2. Line & Span

์šฐ์„  ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ฒ ์Šต๋‹ˆ๋‹ค.

import pandas as pd 

student = pd.read_csv('./StudentsPerformance.csv')
student.head()

gender

race/ethnicity

parental level of education

lunch

test preparation course

math score

reading score

writing score

0

female

group B

bachelor's degree

standard

none

72

72

74

1

female

group C

some college

standard

completed

69

90

88

2

female

group B

master's degree

standard

none

90

95

93

3

male

group A

associate's degree

free/reduced

none

47

57

44

4

male

group C

some college

standard

none

76

78

75

2-1. Line

  • axvline()

  • axhline()

์ง๊ต์ขŒํ‘œ๊ณ„์—์„œ ํ‰ํ–‰์„ ์„ ์›ํ•˜๋Š” ๋ถ€๋ถ„ ๊ทธ๋ฆด ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

์„ ์€ Plot์œผ๋กœ ๊ทธ๋ฆฌ๋Š”๊ฒŒ ๋” ํŽธํ•  ์ˆ˜ ์žˆ๊ธฐ์— ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ทธ๋ ค์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

fig, ax = plt.subplots()

ax.set_aspect(1)
ax.axvline(0, color='red')
ax.axhline(0, color='green')

ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)

plt.show()

ax์˜ ์ „์ฒด ๊ตฌ๊ฐ„์„ 0, 1๋กœ ์‚ผ์•„ ํŠน์ • ๋ถ€๋ถ„์—๋งŒ ์„ ์„ ๊ทธ๋ฆด ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠน์ • ๋ถ€๋ถ„์„ ์„ ์œผ๋กœ ํ•  ๋•Œ๋Š” ์˜คํžˆ๋ ค plot์ด ์ข‹์Šต๋‹ˆ๋‹ค.

fig, ax = plt.subplots()

ax.set_aspect(1)
ax.axvline(0, ymin=0.3, ymax=0.7, color='red')

ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)

plt.show()
fig, ax = plt.subplots(figsize=(10, 10))
ax.set_aspect(1)

math_mean = student['math score'].mean()
reading_mean = student['reading score'].mean()

ax.axvline(math_mean, color='gray', linestyle='--')
ax.axhline(reading_mean, color='gray', linestyle='--')

ax.scatter(x=student['math score'], y=student['reading score'],
           alpha=0.5,
           color=['royalblue' if m>math_mean and r>reading_mean else 'gray'  for m, r in zip(student['math score'], student['reading score'])],
           zorder=10,
          )

ax.set_xlabel('Math')
ax.set_ylabel('Reading')

ax.set_xlim(-3, 103)
ax.set_ylim(-3, 103)
plt.show()

ํ‰๊ท ๋ณด๋‹ค ๋†’์€ ์ ๋“ค์— ๋Œ€ํ•ด์„œ๋งŒ ์ƒ‰์„ ํ‘œํ˜„ํ–ˆ๋‹ค.

2-2. Span

  • axvspan

  • axhspan

์„ ๊ณผ ํ•จ๊ป˜ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŠน์ • ๋ถ€๋ถ„ ๋ฉด์ ์„ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

fig, ax = plt.subplots()

ax.set_aspect(1)
ax.axvspan(0,0.5, color='red')
ax.axhspan(0,0.5, color='green')

ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)

plt.show()
fig, ax = plt.subplots()

ax.set_aspect(1)
ax.axvspan(0,0.5, ymin=0.3, ymax=0.7, color='red')

ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)

plt.show()

ํŠน์ • ๋ถ€๋ถ„์„ ๊ฐ•์กฐํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์˜คํžˆ๋ ค ํŠน์ • ๋ถ€๋ถ„์˜ ์ฃผ์˜๋ฅผ ์—†์•จ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

fig, ax = plt.subplots(figsize=(8, 8))
ax.set_aspect(1)

math_mean = student['math score'].mean()
reading_mean = student['reading score'].mean()

ax.axvspan(-3, math_mean, color='gray', linestyle='--', zorder=0, alpha=0.3)
ax.axhspan(-3, reading_mean, color='gray', linestyle='--', zorder=0, alpha=0.3)

ax.scatter(x=student['math score'], y=student['reading score'],
           alpha=0.4, s=20,
           color=['royalblue' if m>math_mean and r>reading_mean else 'gray'  for m, r in zip(student['math score'], student['reading score'])],
           zorder=10,
          )

ax.set_xlabel('Math')
ax.set_ylabel('Reading')

ax.set_xlim(-3, 103)
ax.set_ylim(-3, 103)
plt.show()

2-3. Spines

  • ax.spines : ๋งŽ์€ ์š”์†Œ๊ฐ€ ์žˆ์ง€๋งŒ ๋Œ€ํ‘œ์ ์ธ 3๊ฐ€์ง€๋ฅผ ์‚ดํŽด๋ด…์‹œ๋‹ค.

    • set_visible : ์ถ•์„ ์•ˆ๋ณด์ด๊ฒŒํ•จ

    • set_linewidth : ์ถ•์˜ ๋‘๊ผ๋ฅผ ์„ค์ •

    • set_position : ์ถ•์˜ ์œ„์น˜๋ฅผ ์˜ฎ๊ธด๋‹ค

fig = plt.figure(figsize=(12, 6))

_ = fig.add_subplot(1,2,1)
ax = fig.add_subplot(1,2,2)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_linewidth(1.5)
ax.spines['bottom'].set_linewidth(1.5)
plt.show()
fig = plt.figure(figsize=(12, 6))

_ = fig.add_subplot(1,2,1)
ax = fig.add_subplot(1,2,2)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
plt.show()

์ถ•์€ ๊ผญ ์ค‘์‹ฌ ์™ธ์—๋„ ์›ํ•˜๋Š” ๋ถ€๋ถ„์œผ๋กœ ์˜ฎ๊ธธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • 'center' -> ('axes', 0.5)

  • 'zero' -> ('data', 0.0)

fig = plt.figure(figsize=(12, 6))

ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

for ax in [ax1, ax2]:
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)

ax1.spines['left'].set_position('center')
ax1.spines['bottom'].set_position('center')

ax2.spines['left'].set_position(('data', 0.3))
ax2.spines['bottom'].set_position(('axes', 0.2))

ax2.set_ylim(-1, 1)
plt.show()

๋น„์œจ์ ์ธ ์ธก๋ฉด์—์„œ ๋ฐ”๊ฟ€ ๋–„๋Š” axes๋ฅผ, ํŠน์ • ๋ฐ์ดํ„ฐ์— ์œ„์น˜๋กœ ๋ฐ”๊ฟ€ ๋•Œ์—๋Š” ๋ฐ์ดํ„ฐ์˜ ์œ„์น˜์— tupleํ˜•ํƒœ๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

fig = plt.figure(figsize=(12, 9))

ax = fig.add_subplot(aspect=1)

x = np.linspace(-np.pi, np.pi, 1000)
y = np.sin(x)

ax.plot(x, y)

ax.set_xlim(-np.pi, np.pi)
ax.set_ylim(-1.2, 1.2)

ax.set_xticks([-np.pi, -np.pi/2, 0, np.pi/2, np.pi])
ax.set_xticklabels([r'$\pi$', r'-$-\frac{\pi}{2}$', r'$0$', r'$\frac{\pi}{2}$', r'$\pi$'],)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

ax.spines['left'].set_position('center')
ax.spines['bottom'].set_position('center')
plt.show()

3. Settings

3-1. mpl.rc

plt.rcParams['lines.linewidth'] = 2
plt.rcParams['lines.linestyle'] = ':'

# plt.rcParams['figure.dpi'] = 150
plt.rc('lines', linewidth=2, linestyle=':')
plt.rcParams.update(plt.rcParamsDefault)

3-2. theme

print(mpl.style.available)

mpl.style.use('seaborn')
# mpl.style.use('./CUSTOM.mplstyle') # ์ปค์Šคํ…€์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด

plt.plot([1, 2, 3])
['Solarize_Light2', '_classic_test_patch', 'bmh', 'classic', 'dark_background', 'fast', 'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn', 'seaborn-bright', 'seaborn-colorblind', 'seaborn-dark', 'seaborn-dark-palette', 'seaborn-darkgrid', 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks', 'seaborn-white', 'seaborn-whitegrid', 'tableau-colorblind10']
with plt.style.context('fivethirtyeight'):
    plt.plot(np.sin(np.linspace(0, 2 * np.pi)))
plt.show()
plt.plot(np.sin(np.linspace(0, 2 * np.pi)))

Last updated

Was this helpful?