import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
height=4
width=5
depth=3
m=np.zeros((height,width,depth))
m
array([[[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]],

       [[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]])
  • 4개의 row와 5개의 column 3개의 depth(각 column의)
  • 보기엔 그렇게 안 보이지만 첫 번째 블록을 쭉 펼치면 그게 1행인 거고(이런 행이 총 4개) 그 안에 5개의 열이 있고 한 열당 깊이가 3개다
  • 다음 사진을 참고하자
  • ref
  • image.png
plt.imshow(m)
plt.grid()
  • 이미지의 시작은 좌측 상단임
m[:,:,0]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
array([[[255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.]],

       [[255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.]],

       [[255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.]],

       [[255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.],
        [255.,   0.,   0.]]])
m=np.zeros((height,width,depth))
m[:,:,1]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
array([[[  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.]],

       [[  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.]],

       [[  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.]],

       [[  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.],
        [  0., 255.,   0.]]])
m=np.zeros((height,width,depth))
m[:,:,2]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
array([[[  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.]],

       [[  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.]],

       [[  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.]],

       [[  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.],
        [  0.,   0., 255.]]])
m=np.ones((height,width,depth))
plt.imshow(m)
plt.grid()
m
array([[[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]],

       [[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]])
m=np.zeros((height,width,depth))
plt.imshow(m)
plt.grid()
m=np.zeros((height,width,depth))
m[0,:,:]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
array([[[255., 255., 255.],
        [255., 255., 255.],
        [255., 255., 255.],
        [255., 255., 255.],
        [255., 255., 255.]],

       [[  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]]])
m=np.zeros((height,width,depth))
m[0,0,:]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
# 1행 1열의 RGB를 모두 255 = 그래서 흰색이 나옴
array([[[255., 255., 255.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]]])
m=np.zeros((height,width,depth))
m[0,0,1]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m=np.zeros((height,width,depth))
m[0,0,2]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m=np.zeros((height,width,depth))
m[0,0,0]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m=np.zeros((height,width,depth))
plt.imshow(m)
plt.grid()
m=np.zeros((height,width,depth))
m[:,0,1]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
array([[[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.]]])
m[:,-2,2]=255
plt.imshow(m)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
m
array([[[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]]])
m[2,:,0]=255
m[2,:,1]=255
m[2,:,2]=0
plt.imshow(m)
plt.grid()
# R+G=노랑색
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
  • 이미지를 세로로 두배 늘려보자
m_vt=np.vstack([m,m])
plt.imshow(m_vt)
plt.grid()
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
  • 이미지를 가로로 두배 늘려보자
m_hz=np.hstack([m,m])
plt.imshow(m_hz)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
<matplotlib.image.AxesImage at 0x210807a93a0>

어둡게 해보자

m_hz
array([[[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.],
        [  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.],
        [  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]],

       [[255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.],
        [255., 255.,   0.]],

       [[  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.],
        [  0., 255.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0.,   0.],
        [  0.,   0., 255.],
        [  0.,   0.,   0.]]])
m_hz=(m_hz/255)*0.5
plt.imshow(m_hz)
<matplotlib.image.AxesImage at 0x210806facd0>
m_hz
array([[[0. , 0.5, 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0.5],
        [0. , 0. , 0. ],
        [0. , 0.5, 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0.5],
        [0. , 0. , 0. ]],

       [[0. , 0.5, 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0.5],
        [0. , 0. , 0. ],
        [0. , 0.5, 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0.5],
        [0. , 0. , 0. ]],

       [[0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ],
        [0.5, 0.5, 0. ]],

       [[0. , 0.5, 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0.5],
        [0. , 0. , 0. ],
        [0. , 0.5, 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0. ],
        [0. , 0. , 0.5],
        [0. , 0. , 0. ]]])
m=np.zeros((5,5,3))
plt.imshow(m)
<matplotlib.image.AxesImage at 0x21080617790>
m[:,:,:]=1
plt.imshow(m)
<matplotlib.image.AxesImage at 0x21080548310>
m[:,:,:]=0
plt.imshow(m)
<matplotlib.image.AxesImage at 0x21080405910>
m[0::2,0::2,:]=1
plt.imshow(m)
<matplotlib.image.AxesImage at 0x210803382e0>
m[1::2,1::2,:]=1
plt.imshow(m)
<matplotlib.image.AxesImage at 0x210801e9e80>
  • 논리 연산을 사용해서 체크 무늬 만들어보자
n = m.copy()
idx=np.where(n==1)
n[idx]=0.5
plt.imshow(n)
# 흰 부분만 어둡게 된 것을 알 수 있다.
<matplotlib.image.AxesImage at 0x21080174d30>
idx=np.where(n==0)
n[idx]=1
plt.imshow(n)
# 검정색 부분만 흰색으로 만들어줌
<matplotlib.image.AxesImage at 0x210805d6430>
n=m.copy()
plt.imshow(n)
<matplotlib.image.AxesImage at 0x210805bf580>
  • 컬러 뒤집기
idx1=np.where(n==1)
idx2=np.where(n==0)
n[idx1]=0
n[idx2]=1
plt.imshow(n)
<matplotlib.image.AxesImage at 0x210805784f0>
a=np.where(n==1)
n[a[0],a[1],0]=0
n[a[0],a[1],2]=0
plt.imshow(n)
<matplotlib.image.AxesImage at 0x2108073d0a0>
a
(array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,
        3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4], dtype=int64),
 array([1, 1, 1, 3, 3, 3, 0, 0, 0, 2, 2, 2, 4, 4, 4, 1, 1, 1, 3, 3, 3, 0,
        0, 0, 2, 2, 2, 4, 4, 4, 1, 1, 1, 3, 3, 3], dtype=int64),
 array([0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0,
        1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2], dtype=int64))
a[0]
array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,
       3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4], dtype=int64)
a[1]
array([1, 1, 1, 3, 3, 3, 0, 0, 0, 2, 2, 2, 4, 4, 4, 1, 1, 1, 3, 3, 3, 0,
       0, 0, 2, 2, 2, 4, 4, 4, 1, 1, 1, 3, 3, 3], dtype=int64)
  • 즉, a[0],a[1]은 각각 값이 1인 부분의 행 인덱싱 열 인덱싱을 의미함
plt.imshow(n)
<matplotlib.image.AxesImage at 0x21080679ee0>
  • 검정색 부분만 빨강으로 바꾸기
flag_r=(n[:,:,0]==0) 
flag_g=(n[:,:,1]==0) 
flag_b=(n[:,:,2]==0) 

flag_blk=  flag_r&flag_g&flag_b
idx_blk=np.where(flag_blk==True)

n[idx_blk[0],idx_blk[1],0]=1
plt.imshow(n)
<matplotlib.image.AxesImage at 0x21080305130>
n=m.copy()
plt.imshow(n)
<matplotlib.image.AxesImage at 0x210805fe4f0>
n
array([[[1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.]],

       [[0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.]],

       [[1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.]],

       [[0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.]],

       [[1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.],
        [0., 0., 0.],
        [1., 1., 1.]]])
print(np.sum(n,axis=0))
print(np.sum(n,axis=1))
print(np.sum(n,axis=2))
[[3. 3. 3.]
 [2. 2. 2.]
 [3. 3. 3.]
 [2. 2. 2.]
 [3. 3. 3.]]
[[3. 3. 3.]
 [2. 2. 2.]
 [3. 3. 3.]
 [2. 2. 2.]
 [3. 3. 3.]]
[[3. 0. 3. 0. 3.]
 [0. 3. 0. 3. 0.]
 [3. 0. 3. 0. 3.]
 [0. 3. 0. 3. 0.]
 [3. 0. 3. 0. 3.]]
np_sum=np.sum(n,axis=2) # 색 찾기
idx_blk=np.where(np_sum==0) # 검정색인 부분만 찾는다.
n[idx_blk[0],idx_blk[1],0]=1 # idx_blk[0],idx_blk[1] => 해당 위치를 알 수 있음
plt.imshow(n)
<matplotlib.image.AxesImage at 0x21081876fd0>
np.fill_diagonal(n[:,:,0],0)
np.fill_diagonal(n[:,:,1],1)
np.fill_diagonal(n[:,:,2],0)
plt.imshow(n)
<matplotlib.image.AxesImage at 0x210818d5bb0>
m=np.linspace(0,1,10) # 1차원
m=m[:,np.newaxis] # 2차원
m=np.repeat(m,10,axis=1)
plt.imshow(m,cmap='gray')
<matplotlib.image.AxesImage at 0x210819d01c0>
m
array([[0.        , 0.        , 0.        , 0.        , 0.        ,
        0.        , 0.        , 0.        , 0.        , 0.        ],
       [0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.11111111,
        0.11111111, 0.11111111, 0.11111111, 0.11111111, 0.11111111],
       [0.22222222, 0.22222222, 0.22222222, 0.22222222, 0.22222222,
        0.22222222, 0.22222222, 0.22222222, 0.22222222, 0.22222222],
       [0.33333333, 0.33333333, 0.33333333, 0.33333333, 0.33333333,
        0.33333333, 0.33333333, 0.33333333, 0.33333333, 0.33333333],
       [0.44444444, 0.44444444, 0.44444444, 0.44444444, 0.44444444,
        0.44444444, 0.44444444, 0.44444444, 0.44444444, 0.44444444],
       [0.55555556, 0.55555556, 0.55555556, 0.55555556, 0.55555556,
        0.55555556, 0.55555556, 0.55555556, 0.55555556, 0.55555556],
       [0.66666667, 0.66666667, 0.66666667, 0.66666667, 0.66666667,
        0.66666667, 0.66666667, 0.66666667, 0.66666667, 0.66666667],
       [0.77777778, 0.77777778, 0.77777778, 0.77777778, 0.77777778,
        0.77777778, 0.77777778, 0.77777778, 0.77777778, 0.77777778],
       [0.88888889, 0.88888889, 0.88888889, 0.88888889, 0.88888889,
        0.88888889, 0.88888889, 0.88888889, 0.88888889, 0.88888889],
       [1.        , 1.        , 1.        , 1.        , 1.        ,
        1.        , 1.        , 1.        , 1.        , 1.        ]])
m_tr=np.transpose(m)
plt.imshow(m_tr,cmap='gray')
<matplotlib.image.AxesImage at 0x21081a27520>
m_tr=np.transpose(m)
plt.imshow(m_tr,cmap='jet')
<matplotlib.image.AxesImage at 0x21081a81a30>

LaTex 이용해보자

$ \begin{align} \begin{cases} f(x) = \frac{1}{10}x^2 & \text{ for } x\in [0,1.5) \\ f(x) = \sum_{n=1,3,5..}^{N}{\frac{4}{\pi n} \text{sin}(\frac{2\pi n(x-1.5)}{T})}& \text{for} \in[1.5,3] \end{cases} \end{align} $

$\to$ 분할함수(Piecewise Function)


x1=np.arange(0,1.5,0.0001)
y1=1/10*x1**2
x2=np.arange(1.5,3+0.01,0.0001)
n= 5000
t=1
y2=np.zeros(x2.shape)
for n in range(n):
    if n%2==1:
        series = 4/(np.pi*n)*np.sin((2*np.pi*n*(x2-1.5))/t)
        y2=y2+series

n이 커질수록 합이 커짐

plt.plot(x1,y1)
plt.plot(x2,y2)
plt.xlabel('x')
plt.ylabel('y')
plt.title('Piecewise Functions')
Text(0.5, 1.0, 'Piecewise Functions')

세계 온도 변화 시각화

import scipy
import scipy.interpolate as interp
import gzip
import pickle as pkl
file=gzip.GzipFile('GlobalTemperatureData.pkl.gz','rb')
df=pkl.load(file)
file.close
<bound method GzipFile.close of <gzip _io.BufferedReader name='GlobalTemperatureData.pkl.gz' 0x2108fee16a0>>
df.keys()
dict_keys(['YR1881', 'YR1882', 'YR1883', 'YR1884', 'YR1885', 'YR1886', 'YR1887', 'YR1888', 'YR1889', 'YR1890', 'YR1891', 'YR1892', 'YR1893', 'YR1894', 'YR1895', 'YR1896', 'YR1897', 'YR1898', 'YR1899', 'YR1900', 'YR1901', 'YR1902', 'YR1903', 'YR1904', 'YR1905', 'YR1906', 'YR1907', 'YR1908', 'YR1909', 'YR1910', 'YR1911', 'YR1912', 'YR1913', 'YR1914', 'YR1915', 'YR1916', 'YR1917', 'YR1918', 'YR1919', 'YR1920', 'YR1921', 'YR1922', 'YR1923', 'YR1924', 'YR1925', 'YR1926', 'YR1927', 'YR1928', 'YR1929', 'YR1930', 'YR1931', 'YR1932', 'YR1933', 'YR1934', 'YR1935', 'YR1936', 'YR1937', 'YR1938', 'YR1939', 'YR1940', 'YR1941', 'YR1942', 'YR1943', 'YR1944', 'YR1945', 'YR1946', 'YR1947', 'YR1948', 'YR1949', 'YR1950', 'YR1951', 'YR1952', 'YR1953', 'YR1954', 'YR1955', 'YR1956', 'YR1957', 'YR1958', 'YR1959', 'YR1960', 'YR1961', 'YR1962', 'YR1963', 'YR1964', 'YR1965', 'YR1966', 'YR1967', 'YR1968', 'YR1969', 'YR1970', 'YR1971', 'YR1972', 'YR1973', 'YR1974', 'YR1975', 'YR1976', 'YR1977', 'YR1978', 'YR1979', 'YR1980', 'YR1981', 'YR1982', 'YR1983', 'YR1984', 'YR1985', 'YR1986', 'YR1987', 'YR1988', 'YR1989', 'YR1990', 'YR1991', 'YR1992', 'YR1993', 'YR1994', 'YR1995', 'YR1996', 'YR1997', 'YR1998', 'YR1999', 'YR2000', 'YR2001', 'YR2002', 'YR2003', 'YR2004', 'YR2005', 'YR2006', 'YR2007', 'YR2008', 'YR2009', 'YR2010', 'YR2011', 'YR2012', 'YR2013', 'YR2014', 'YR2015', 'YR2016', 'YR2017', 'YR2018', 'YR2019'])

연도별로 세계온도가 들어가있음

yr=list(df.keys())
iyr=10
df_yr=df[yr[iyr]]
df_yr
i j lon lat Temperature(i,j)
0 1 1 -179 -89 9999.0
1 2 1 -177 -89 9999.0
2 3 1 -175 -89 9999.0
3 4 1 -173 -89 9999.0
4 5 1 -171 -89 9999.0
... ... ... ... ... ...
16195 176 90 171 89 9999.0
16196 177 90 173 89 9999.0
16197 178 90 175 89 9999.0
16198 179 90 177 89 9999.0
16199 180 90 179 89 9999.0

16200 rows × 5 columns

df_yr.keys()
Index(['i', 'j', 'lon', 'lat', 'Temperature(i,j)'], dtype='object')

위도, 경도, 온도만 발췌하여 numpy_array로 변형해보자

원래는 dict 형태였음

df_yr[['lon','lat','Temperature(i,j)']]
lon lat Temperature(i,j)
0 -179 -89 9999.0
1 -177 -89 9999.0
2 -175 -89 9999.0
3 -173 -89 9999.0
4 -171 -89 9999.0
... ... ... ...
16195 171 89 9999.0
16196 173 89 9999.0
16197 175 89 9999.0
16198 177 89 9999.0
16199 179 89 9999.0

16200 rows × 3 columns

  • to_numpy()
data=df_yr[['lon','lat','Temperature(i,j)']].to_numpy()
data
array([[-179.,  -89., 9999.],
       [-177.,  -89., 9999.],
       [-175.,  -89., 9999.],
       ...,
       [ 175.,   89., 9999.],
       [ 177.,   89., 9999.],
       [ 179.,   89., 9999.]])
data[np.where(data>9999)]=np.nan

9999를 nan으로 바꾼 이유는 자동으로 plot이 안 되게 하기 위해서

x=np.linspace(-180,180,100)
y=np.linspace(-90,90,100)
grid_x,grid_y=np.meshgrid(x,y)
data_interp=interp.griddata(data[:,[0,1]],data[:,2],(grid_x,grid_y),method='linear')

hfig,hax=plt.subplots()
im=plt.imread('world_map.png')
plt.imshow(im)
<matplotlib.image.AxesImage at 0x21090f0a3a0>

좌표축의 중심을 왼쪽 아래로

그런데 지도까지 뒤집혀버림

hfig,hax=plt.subplots()
im=plt.imread('world_map.png')
plt.imshow(im,origin='lower')
<matplotlib.image.AxesImage at 0x2108feffa00>

다시 지도만 뒤집어줌

hfig,hax=plt.subplots()
im=plt.imread('world_map.png')
plt.imshow(np.flipud(im),origin='lower')
<matplotlib.image.AxesImage at 0x21096ed8340>

size 조정

hfig,hax=plt.subplots()
im=plt.imread('world_map.png')
plt.imshow(np.flipud(im),origin='lower',extent=(0,800,0,400))
<matplotlib.image.AxesImage at 0x2108edf4400>
hfig,hax=plt.subplots()
im=plt.imread('world_map.png')
plt.imshow(np.flipud(im),origin='lower',extent=(-180,180,-90,90))
plt.pcolormesh(grid_x,grid_y,data_interp,cmap='coolwarm',alpha=0.6)
plt.xlim(-180,180)
plt.ylim(-90,90)
plt.title('global temperature change from ty1880 to'+str(yr[iyr]))
plt.xlabel('Altitude')
plt.xlabel('Latitude')
plt.colorbar(fraction=0.022,pad=0.05)
plt.clim(-4,4)
C:\Users\ehfus\AppData\Local\Temp/ipykernel_17584/3478053917.py:4: MatplotlibDeprecationWarning: shading='flat' when X and Y have the same dimensions as C is deprecated since 3.3.  Either specify the corners of the quadrilaterals with X and Y, or pass shading='auto', 'nearest' or 'gouraud', or set rcParams['pcolor.shading'].  This will become an error two minor releases later.
  plt.pcolormesh(grid_x,grid_y,data_interp,cmap='coolwarm',alpha=0.6)