1. Pandas 기초

  • Series: Python의 1차원 배열
  • DataFrame: Python의 2차원 배열
In [1]:
import pandas as pd
import numpy as np
In [2]:
s = pd.Series([1, 3, 5, np.nan, 6, 8])
s
Out[2]:
0    1.0
1    3.0
2    5.0
3    NaN
4    6.0
5    8.0
dtype: float64
In [3]:
dates = pd.date_range('20190716', periods=7) # 날짜형 데이터
dates
Out[3]:
DatetimeIndex(['2019-07-16', '2019-07-17', '2019-07-18', '2019-07-19',
               '2019-07-20', '2019-07-21', '2019-07-22'],
              dtype='datetime64[ns]', freq='D')
In [4]:
df = pd.DataFrame(np.random.randn(7,4), index=dates, columns=['A', 'B', 'C', 'D']) # 7행 4열 random 변수 생성
df
Out[4]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-18 1.001709 2.062204 0.603075 0.543653
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039
2019-07-21 0.231179 -1.021597 -0.452473 0.024489
2019-07-22 -0.731543 0.628950 -1.164443 -0.459914
In [5]:
df.head(3)
Out[5]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-18 1.001709 2.062204 0.603075 0.543653
In [6]:
df.index
Out[6]:
DatetimeIndex(['2019-07-16', '2019-07-17', '2019-07-18', '2019-07-19',
               '2019-07-20', '2019-07-21', '2019-07-22'],
              dtype='datetime64[ns]', freq='D')
In [7]:
df.columns
Out[7]:
Index(['A', 'B', 'C', 'D'], dtype='object')
In [8]:
df.values
Out[8]:
array([[-0.42760552,  0.84124323, -1.08119535, -0.64056013],
       [ 0.46189158, -0.49185823, -1.22301401,  1.35682128],
       [ 1.0017088 ,  2.06220433,  0.60307464,  0.54365332],
       [ 1.1271527 , -1.13610609, -1.4695818 ,  0.88682942],
       [ 1.42035588, -0.11648819, -0.0791372 , -3.20203895],
       [ 0.23117855, -1.02159651, -0.4524734 ,  0.0244893 ],
       [-0.73154322,  0.62895045, -1.16444282, -0.45991406]])
In [9]:
df.info() # DataFrame의 개요 확인
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 7 entries, 2019-07-16 to 2019-07-22
Freq: D
Data columns (total 4 columns):
A    7 non-null float64
B    7 non-null float64
C    7 non-null float64
D    7 non-null float64
dtypes: float64(4)
memory usage: 280.0 bytes

* describe(): 통계적 개요 확인

  • count: 개수
  • mean: 평균
  • std: 표준편차
  • min: 최소
  • max: 최대
In [10]:
df.describe()
Out[10]:
A B C D
count 7.000000 7.000000 7.000000 7.000000
mean 0.440448 0.109478 -0.695253 -0.212960
std 0.808098 1.144694 0.749600 1.498945
min -0.731543 -1.136106 -1.469582 -3.202039
25% -0.098213 -0.756727 -1.193728 -0.550237
50% 0.461892 -0.116488 -1.081195 0.024489
75% 1.064431 0.735097 -0.265805 0.715241
max 1.420356 2.062204 0.603075 1.356821

* sort_values(): by로 지정된 컬럼을 기준으로 정렬

  • ascending: 오름차순 정렬
In [11]:
df.sort_values(by='B', ascending=False)
Out[11]:
A B C D
2019-07-18 1.001709 2.062204 0.603075 0.543653
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-22 -0.731543 0.628950 -1.164443 -0.459914
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-21 0.231179 -1.021597 -0.452473 0.024489
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
In [12]:
df
Out[12]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-18 1.001709 2.062204 0.603075 0.543653
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039
2019-07-21 0.231179 -1.021597 -0.452473 0.024489
2019-07-22 -0.731543 0.628950 -1.164443 -0.459914
In [13]:
df['A']
Out[13]:
2019-07-16   -0.427606
2019-07-17    0.461892
2019-07-18    1.001709
2019-07-19    1.127153
2019-07-20    1.420356
2019-07-21    0.231179
2019-07-22   -0.731543
Freq: D, Name: A, dtype: float64
In [14]:
df[0:4]
Out[14]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-18 1.001709 2.062204 0.603075 0.543653
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
In [15]:
df['20190719':'20190721']
Out[15]:
A B C D
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039
2019-07-21 0.231179 -1.021597 -0.452473 0.024489

* loc: 슬라이싱 할 때 location 옵션 이용해서 위치 값 지정 가능

In [16]:
df.loc[dates[3]]
Out[16]:
A    1.127153
B   -1.136106
C   -1.469582
D    0.886829
Name: 2019-07-19 00:00:00, dtype: float64
In [17]:
df.loc[:, ['A', 'B']]
Out[17]:
A B
2019-07-16 -0.427606 0.841243
2019-07-17 0.461892 -0.491858
2019-07-18 1.001709 2.062204
2019-07-19 1.127153 -1.136106
2019-07-20 1.420356 -0.116488
2019-07-21 0.231179 -1.021597
2019-07-22 -0.731543 0.628950
In [18]:
df.loc['20190718':'20190720', ['A','B']]
Out[18]:
A B
2019-07-18 1.001709 2.062204
2019-07-19 1.127153 -1.136106
2019-07-20 1.420356 -0.116488
In [19]:
df.loc['20190718':'20190720', 'A':'B']
Out[19]:
A B
2019-07-18 1.001709 2.062204
2019-07-19 1.127153 -1.136106
2019-07-20 1.420356 -0.116488
In [20]:
df.loc['20190722', 'A':'B']
Out[20]:
A   -0.731543
B    0.628950
Name: 2019-07-22 00:00:00, dtype: float64
In [21]:
df.loc[dates[2], 'C']
Out[21]:
0.6030746436887479

* iloc: 슬라이싱 할 때 행과 열 번호를 이용해 데이터 바로 접근 가능

In [22]:
df.iloc[3]
Out[22]:
A    1.127153
B   -1.136106
C   -1.469582
D    0.886829
Name: 2019-07-19 00:00:00, dtype: float64
In [23]:
df.iloc[2:4, :3]
Out[23]:
A B C
2019-07-18 1.001709 2.062204 0.603075
2019-07-19 1.127153 -1.136106 -1.469582
In [24]:
df.iloc[[1,2,5],3]
Out[24]:
2019-07-17    1.356821
2019-07-18    0.543653
2019-07-21    0.024489
Name: D, dtype: float64
In [25]:
df.iloc[0:4, :]
Out[25]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-18 1.001709 2.062204 0.603075 0.543653
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
In [26]:
df.iloc[:, 0:3]
Out[26]:
A B C
2019-07-16 -0.427606 0.841243 -1.081195
2019-07-17 0.461892 -0.491858 -1.223014
2019-07-18 1.001709 2.062204 0.603075
2019-07-19 1.127153 -1.136106 -1.469582
2019-07-20 1.420356 -0.116488 -0.079137
2019-07-21 0.231179 -1.021597 -0.452473
2019-07-22 -0.731543 0.628950 -1.164443
In [27]:
df
Out[27]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-18 1.001709 2.062204 0.603075 0.543653
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039
2019-07-21 0.231179 -1.021597 -0.452473 0.024489
2019-07-22 -0.731543 0.628950 -1.164443 -0.459914
In [28]:
df[df.B < 0]
Out[28]:
A B C D
2019-07-17 0.461892 -0.491858 -1.223014 1.356821
2019-07-19 1.127153 -1.136106 -1.469582 0.886829
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039
2019-07-21 0.231179 -1.021597 -0.452473 0.024489
In [29]:
df[df < 0]
Out[29]:
A B C D
2019-07-16 -0.427606 NaN -1.081195 -0.640560
2019-07-17 NaN -0.491858 -1.223014 NaN
2019-07-18 NaN NaN NaN NaN
2019-07-19 NaN -1.136106 -1.469582 NaN
2019-07-20 NaN -0.116488 -0.079137 -3.202039
2019-07-21 NaN -1.021597 -0.452473 NaN
2019-07-22 -0.731543 NaN -1.164443 -0.459914
In [30]:
df2 = df.copy() # 데이터의 내용까지 복사함
In [31]:
df2['E'] = ['one', 'one', 'two', 'three', 'four', 'five', 'six']
df2
Out[31]:
A B C D E
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560 one
2019-07-17 0.461892 -0.491858 -1.223014 1.356821 one
2019-07-18 1.001709 2.062204 0.603075 0.543653 two
2019-07-19 1.127153 -1.136106 -1.469582 0.886829 three
2019-07-20 1.420356 -0.116488 -0.079137 -3.202039 four
2019-07-21 0.231179 -1.021597 -0.452473 0.024489 five
2019-07-22 -0.731543 0.628950 -1.164443 -0.459914 six
In [32]:
df2['E'].isin(['one', 'two'])
Out[32]:
2019-07-16     True
2019-07-17     True
2019-07-18     True
2019-07-19    False
2019-07-20    False
2019-07-21    False
2019-07-22    False
Freq: D, Name: E, dtype: bool
In [33]:
df2[df2['E'].isin(['one', 'two'])]
Out[33]:
A B C D E
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560 one
2019-07-17 0.461892 -0.491858 -1.223014 1.356821 one
2019-07-18 1.001709 2.062204 0.603075 0.543653 two

* apply(): 함수 적용

  • cumsum: 누적합
  • lambda: one-line 함수
In [34]:
df.apply(np.cumsum)
Out[34]:
A B C D
2019-07-16 -0.427606 0.841243 -1.081195 -0.640560
2019-07-17 0.034286 0.349385 -2.304209 0.716261
2019-07-18 1.035995 2.411589 -1.701135 1.259914
2019-07-19 2.163148 1.275483 -3.170717 2.146744
2019-07-20 3.583503 1.158995 -3.249854 -1.055295
2019-07-21 3.814682 0.137399 -3.702327 -1.030806
2019-07-22 3.083139 0.766349 -4.866770 -1.490720
In [35]:
df.apply(lambda x: x.max() - x.min())
Out[35]:
A    2.151899
B    3.198310
C    2.072656
D    4.558860
dtype: float64

2. Pandas 고급 - 두 DataFrame 병합

In [36]:
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']},
                    index = [0, 1, 2, 3])
df2 = pd.DataFrame({'A': ['A4', 'A5', 'A6', 'A7'],
                    'B': ['B4', 'B5', 'B6', 'B7'],
                    'C': ['C4', 'C5', 'C6', 'C7'],
                    'D': ['D4', 'D5', 'D6', 'D7']},
                    index = [4, 5, 6, 7])
df3 = pd.DataFrame({'A': ['A8', 'A9', 'A10', 'A11'],
                    'B': ['B8', 'B9', 'B10', 'B11'],
                    'C': ['C8', 'C9', 'C10', 'C11'],
                    'D': ['D8', 'D9', 'D10', 'D11']},
                    index = [8, 9, 10, 11])
In [37]:
df1
Out[37]:
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
In [38]:
df2
Out[38]:
A B C D
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
In [39]:
df3
Out[39]:
A B C D
8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
In [40]:
result = pd.concat([df1, df2, df3]) # 데이터를 열 방향으로 단순 합침
result
Out[40]:
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
In [41]:
result = pd.concat([df1, df2, df3], keys=['x', 'y', 'z'])
result
Out[41]:
A B C D
x 0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
y 4 A4 B4 C4 D4
5 A5 B5 C5 D5
6 A6 B6 C6 D6
7 A7 B7 C7 D7
z 8 A8 B8 C8 D8
9 A9 B9 C9 D9
10 A10 B10 C10 D10
11 A11 B11 C11 D11
In [42]:
result.index # key 지정된 구분은 다중 index 되어 level 형성함
Out[42]:
MultiIndex(levels=[['x', 'y', 'z'], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]],
           labels=[[0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]])
In [43]:
result.index.get_level_values(0)
Out[43]:
Index(['x', 'x', 'x', 'x', 'y', 'y', 'y', 'y', 'z', 'z', 'z', 'z'], dtype='object')
In [44]:
result.index.get_level_values(1)
Out[44]:
Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dtype='int64')
In [45]:
df4 = pd.DataFrame({'B': ['B2', 'B3', 'B6', 'B7'],
                    'D': ['D2', 'D3', 'D6', 'D7'],
                    'F': ['F2', 'F3', 'F6', 'F7']},
                    index = [2, 3, 6, 7])
result = pd.concat([df1, df4], axis=1)
In [46]:
df1
Out[46]:
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
3 A3 B3 C3 D3
In [47]:
df4
Out[47]:
B D F
2 B2 D2 F2
3 B3 D3 F3
6 B6 D6 F6
7 B7 D7 F7
In [48]:
result
Out[48]:
A B C D B D F
0 A0 B0 C0 D0 NaN NaN NaN
1 A1 B1 C1 D1 NaN NaN NaN
2 A2 B2 C2 D2 B2 D2 F2
3 A3 B3 C3 D3 B3 D3 F3
6 NaN NaN NaN NaN B6 D6 F6
7 NaN NaN NaN NaN B7 D7 F7
In [49]:
# 공통된 index로 합치고 그렇지 않은 index의 데이터 버림 
result = pd.concat([df1, df4], axis=1, join='inner')
result
Out[49]:
A B C D B D F
2 A2 B2 C2 D2 B2 D2 F2
3 A3 B3 C3 D3 B3 D3 F3
In [50]:
result = pd.concat([df1, df4], axis=1, join_axes=[df1.index]) # 열의 index 직접 지정
result
Out[50]:
A B C D B D F
0 A0 B0 C0 D0 NaN NaN NaN
1 A1 B1 C1 D1 NaN NaN NaN
2 A2 B2 C2 D2 B2 D2 F2
3 A3 B3 C3 D3 B3 D3 F3
In [51]:
result = pd.concat([df1, df4], ignore_index=True, sort=True) # index 무시하고 합친 후 다시 index 부여
result
Out[51]:
A B C D F
0 A0 B0 C0 D0 NaN
1 A1 B1 C1 D1 NaN
2 A2 B2 C2 D2 NaN
3 A3 B3 C3 D3 NaN
4 NaN B2 NaN D2 F2
5 NaN B3 NaN D3 F3
6 NaN B6 NaN D6 F6
7 NaN B7 NaN D7 F7
In [52]:
left = pd.DataFrame({'key': ['K0', 'K4', 'K2', 'K3'],
                    'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3']})
right = pd.DataFrame({'key': ['K0', 'K1', 'K2', 'K3'],
                     'C': ['C0', 'C1', 'C2', 'C3'],
                     'D': ['D0', 'D1', 'D2', 'D3']})
In [53]:
left
Out[53]:
key A B
0 K0 A0 B0
1 K4 A1 B1
2 K2 A2 B2
3 K3 A3 B3
In [54]:
right
Out[54]:
key C D
0 K0 C0 D0
1 K1 C1 D1
2 K2 C2 D2
3 K3 C3 D3

* merge(): 병합

  • on: 공통된 열에 대해서만 합침
  • how: 기준 데이터 설정
    • outer: merge한 데이터 결과를 모두 가짐(합집합)
    • inner: 공통된 요소만 가짐(교집합)
In [55]:
pd.merge(left, right, on='key')
Out[55]:
key A B C D
0 K0 A0 B0 C0 D0
1 K2 A2 B2 C2 D2
2 K3 A3 B3 C3 D3
In [56]:
pd.merge(left, right, how='left', on='key')
Out[56]:
key A B C D
0 K0 A0 B0 C0 D0
1 K4 A1 B1 NaN NaN
2 K2 A2 B2 C2 D2
3 K3 A3 B3 C3 D3
In [57]:
pd.merge(left, right, how='right', on='key')
Out[57]:
key A B C D
0 K0 A0 B0 C0 D0
1 K2 A2 B2 C2 D2
2 K3 A3 B3 C3 D3
3 K1 NaN NaN C1 D1
In [58]:
pd.merge(left, right, how='outer', on='key')
Out[58]:
key A B C D
0 K0 A0 B0 C0 D0
1 K4 A1 B1 NaN NaN
2 K2 A2 B2 C2 D2
3 K3 A3 B3 C3 D3
4 K1 NaN NaN C1 D1
In [59]:
pd.merge(left, right, how='inner', on='key')
Out[59]:
key A B C D
0 K0 A0 B0 C0 D0
1 K2 A2 B2 C2 D2
2 K3 A3 B3 C3 D3

3. Matplotlib 기초

In [60]:
import matplotlib.pyplot as plt
%matplotlib inline
# 그래프의 결과를 출력 세션에 나타나게 하는 설정
In [61]:
plt.figure()
plt.plot([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
plt.show()
In [62]:
t = np.arange(0, 12, 0.01) # 0부터 12까지 0.01 간격으로 데이터 생성
y = np.sin(t) # 사인 함수에 입력
In [63]:
plt.figure(figsize=(10, 6))
plt.plot(t, y)
plt.show()
In [64]:
plt.figure(figsize=(10, 6))
plt.plot(t, y)
plt.grid() # 그리드 적용
plt.xlabel('time') # x축 라벨 적용
plt.ylabel('Amplitude') # y축 라벨 적용
plt.title('Example of sinewave') # 제목 적용  
plt.show()
In [65]:
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t))
plt.plot(t, np.cos(t))
plt.grid()
plt.xlabel('time')
plt.ylabel('Amplitude')
plt.title('Example of sinewave')
plt.show()
In [66]:
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), label='sin')
plt.plot(t, np.cos(t), label='cos')
plt.grid()
plt.legend() # 범례 추가
plt.xlabel('time')
plt.ylabel('Amplitude')
plt.title('Example of sinewave')
plt.show()
In [67]:
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), lw=3, label='sin')
plt.plot(t, np.cos(t), 'r', label='cos')
plt.grid()
plt.legend()
plt.xlabel('time')
plt.ylabel('Amplitude')
plt.title('Example of sinewave')
plt.show()
In [68]:
plt.figure(figsize=(10, 6))
plt.plot(t, np.sin(t), lw=3, label='sin')
plt.plot(t, np.cos(t), 'r', label='cos')
plt.grid()
plt.legend()
plt.xlabel('time')
plt.ylabel('Amplitude')
plt.title('Example of sinewave')
plt.ylim(-1.2, 1.2) # y축 제한 설정
plt.xlim(0, np.pi) # x축 제한 설정
plt.show()
In [69]:
t = np.arange(0, 5, 0.5) # 0부터 5까지 0.5 간격

plt.figure(figsize=(10, 6))
plt.plot(t, t, 'r--')
plt.plot(t, t**2, 'bs')
plt.plot(t, t**3, 'g^')
plt.show()
In [70]:
plt.figure(figsize=(10, 6))
pl1 = plt.plot(t, t**2, 'bs')

plt.figure(figsize=(10, 6))
pl2 = plt.plot(t, t**3, 'g^')

plt.show()
In [71]:
t = [0, 1, 2, 3, 4, 5, 6]
y = [1, 4, 5, 8, 9, 5, 3]

plt.figure(figsize=(10, 6))
plt.plot(t, y, color='orange')
plt.show()
In [72]:
plt.figure(figsize=(10, 6))
plt.plot(t, y, color='pink', linestyle='dashed')
plt.show()
In [73]:
plt.figure(figsize=(10, 6))
plt.plot(t, y, color='skyblue', linestyle='dashed', marker='o')
plt.show()
In [74]:
plt.figure(figsize=(10, 6))
plt.plot(t, y, color='gray', linestyle='dashed', marker='o',
        markerfacecolor='red', markersize=10)

plt.xlim([-0.5, 6.5])
plt.ylim([0.5, 9.5])
plt.show()
In [75]:
t = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([9, 8, 7, 9, 8, 3, 2, 4, 3, 4])
In [76]:
plt.figure(figsize=(10, 6))
plt.scatter(t, y)
plt.show()
In [77]:
plt.figure(figsize=(10, 6))
plt.scatter(t, y, marker='>')
plt.show()
In [78]:
colormap = t

plt.figure(figsize=(10, 6))
plt.scatter(t, y, s = 40, c = colormap, marker='<') # x축 값에 따라 색상 바꿈
plt.colorbar()
plt.show()
In [79]:
s1 = np.random.normal(loc=0, scale=1, size=1000) # loc: 평균값 지정
s2 = np.random.normal(loc=5, scale=0.5, size=1000) # scale: 표준편차 지정
s3 = np.random.normal(loc=10, scale=2, size=1000)
In [80]:
plt.figure(figsize=(10, 6))
plt.plot(s1, label='s1')
plt.plot(s2, label='s2')
plt.plot(s3, label='s3')
plt.legend()
plt.show()
In [81]:
plt.figure(figsize=(10, 6))
plt.boxplot((s1, s2, s3))
plt.grid()
plt.show()
In [82]:
plt.figure(figsize=(10, 6))

plt.subplot(221) # 2행 2열의 1번째
plt.subplot(222) # 2행 2열의 2번째
plt.subplot(212) # 2행 1열의 2번째

plt.show()
In [83]:
plt.figure(figsize=(10, 6))

plt.subplot(411)
plt.subplot(423)
plt.subplot(424)
plt.subplot(413)
plt.subplot(414)

plt.show()
In [84]:
t = np.arange(0, 5, 0.01)

plt.figure(figsize=(10, 12))

plt.subplot(411)
plt.plot(t, np.sqrt(t)) # 제곱근(루트) 계산
plt.grid()

plt.subplot(423)
plt.plot(t, t**2)
plt.grid()

plt.subplot(424)
plt.plot(t, t**3)
plt.grid()

plt.subplot(413)
plt.plot(t, np.sin(t))
plt.grid()

plt.subplot(414)
plt.plot(t, np.cos(t))
plt.grid()

plt.show()