본문 바로가기

Backup/Python

Matplotlib

Matplotlib을 사용하는 방법(interface)이 두가지가 있다:

 

 

1. pyplot : state based interface

2. Object oriented interface 

  -> 이것만 마스터하자

 

다은과 같은 순서로 시각화 작업 수행:

 

 

2.1.  figure and axes

  • Figure Axes를 동시에 생성하는데 Figure는 캔바스 자체, Axes는 그림의 여러 부분을 담고 있습니다.
  • background color, grid color 등은 plt.style.use()를 사용하면 설정이 용이합니다.
  • fontsize, linewidth 등은 seaborn의 seaborn.set_context()를 사용하면 좋습니다.
  • 그림의 크기를 함께 바꿔봅시다. plt.subplots() 안에 figsize=(가로, 세로)를 넣으면 됩니다.
    단위는 inch입니다.
  •  
import seaborn as sns

plt.style.use('seaborn-whitegrid')
sns.set_context('talk')

fig, ax = plt.subplots(figsize=(7,3))
>>> fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(7, 7))
>>> ax1, ax2, ax3, ax4 = ax.flatten()  # flatten a 2d NumPy array to 1d

 

 

 

 

>>> x = np.random.randint(low=1, high=11, size=50)
>>> y = x + np.random.randint(1, 5, size=x.size)
>>> data = np.column_stack((x, y))

>>> fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2,
...                                figsize=(8, 4))

>>> ax1.scatter(x=x, y=y, marker='o', c='r', edgecolor='b')
>>> ax1.set_title('Scatter: $x$ versus $y$')
>>> ax1.set_xlabel('$x$')
>>> ax1.set_ylabel('$y$')

>>> ax2.hist(data, bins=np.arange(data.min(), data.max()),
...          label=('x', 'y'))
>>> ax2.legend(loc=(0.65, 0.8))
>>> ax2.set_title('Frequencies of $x$ and $y$')
>>> ax2.yaxis.tick_right()

 

 

 

 

 

 

 

'Backup > Python' 카테고리의 다른 글

matplotlib cheat sheet  (0) 2019.01.11