#coding: utf-8
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x=np.array([0,0,0,0,1,1,1,1,0,0,0,0])
plt.plot(x,'ro-')
[<matplotlib.lines.Line2D at 0xa378b38>]
(1)按加號新增一個"Markdown"區塊,描述你看到了什麼。
(2)再按加號新增一個"code"區塊,嘗試畫出 "三個帽子"的波形
(按加號後,點選 'Cell Toolbar' 左方是下拉式選單,選擇 CODE)
用滑鼠點選以下程式碼,並按下"Play"鈕執行看看結果
#coding: utf-8
import numpy as np
t=np.arange(0,1,0.1)
print t
print "這是 np.arange(0,1,0.1)的結果:" + str(t)
print "加上一些換行字元\n\n\n" + "換行的結果是這樣\n"
t=np.arange(0,1,0.01)
print "這是 np.arange(0,1,0.01)的結果:"
print t
sumt=sum(np.arange(0,10,1))
print "\n\n\n這是 sum(np.arange(0,10,1))的結果:" +str(sumt)
[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 這是 np.arange(0,1,0.1)的結果:[ 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9] 加上一些換行字元 換行的結果是這樣 這是 np.arange(0,1,0.01)的結果: [ 0. 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14 0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28 0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42 0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56 0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7 0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84 0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98 0.99] 這是 sum(np.arange(0,10,1))的結果:45
新增一個"Markdown"區塊,將以下問題之答案寫下來。
(1)請推論 np.arange 這個函數的用處,以及三個輸入值的意義。
(2)新增一個"code"區塊,利用python 程式碼,計算100以下偶數之總合。
用滑鼠點選以下程式碼,並按下"Play"鈕執行看看結果
#coding: utf-8
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
t=np.arange(0,1,0.01)
pi=np.pi
curve=np.sin(2*pi*10*t);
curve2=np.sin(2*pi*5*t);
plt.plot(t,curve,'b',t,curve2,'r');
(1)假設橫軸時間單位為秒,請問curve及curve2各為幾Hz弦波(也就是在一秒內走了幾個週期)?
(新增一個"Markdown"區塊,回答此問題)
(2)又我們如何改變指令,畫出一個 3 Hz的Cosine波呢?
(新增一個"CODE"區塊,回答此問題)
用滑鼠點選以下程式碼,並按下"Play"鈕執行看看結果
#coding: utf-8
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x=np.array([0,0,0,1,1,1,1,0,0,0])
y=np.array([x,x,x,x,x,x,x,x,x,x])
print y
print y.shape
plt.figure(1)
plt.imshow(y,cmap='gray',interpolation='nearest')
y=np.array([x/5.,x/4.,x/3.,x/2.,x/1.,x,x,x,x,x])
plt.figure(2)
plt.imshow(y,cmap='gray',interpolation='nearest')
[[0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0] [0 0 0 1 1 1 1 0 0 0]] (10L, 10L)
<matplotlib.image.AxesImage at 0xa9d78d0>
用滑鼠點選以下程式碼,並按下"Play"鈕執行看看結果
#coding: utf-8
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
x=np.array([0,0,0,1,1,1,1,0,0,0])
y=np.array([x,x,x,x,x,x,x,x,x,x])
z=np.zeros([10,10,3])
z[:,:,0]=y
plt.figure(figsize=[15,5])
plt.subplot(1,4,1)
plt.imshow(z,interpolation='nearest')
#######################################
z=np.zeros([10,10,3])
z[:,:,1]=y
plt.subplot(1,4,2)
plt.imshow(z,interpolation='nearest')
#######################################
z=np.zeros([10,10,3])
z[:,:,2]=y
plt.subplot(1,4,3)
plt.imshow(z,interpolation='nearest')
#######################################
z=np.zeros([10,10,3])
z[:,:,0]=y
z[:,:,1]=y
plt.subplot(1,4,4)
plt.imshow(z,interpolation='nearest')
<matplotlib.image.AxesImage at 0xb509198>
用滑鼠點選以下程式碼,並按下"Play"鈕執行看看結果
#coding: utf-8
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
import scipy.misc as misc
lena_img=misc.imread('lena_std.jpg');
print lena_img.shape
plt.figure(figsize=[15,5])
plt.subplot(1,4,1)
plt.imshow(lena_img)
plt.title('Original')
plt.subplot(1,4,2)
plt.imshow(lena_img[:,:,2],cmap='gray')
plt.title('Fig. 1');
plt.subplot(1,4,3)
plt.imshow(lena_img[:,:,1],cmap='gray')
plt.title('Fig. 2');
plt.subplot(1,4,4)
plt.imshow(lena_img[:,:,0],cmap='gray')
plt.title('Fig. 3');
(512L, 512L, 3L)
(1)Fig.1 , 2, 3 代表Lena 圖其中的三原色,若紅光影像數值愈高者(愈白愈亮),代表該像素的中的紅光成份愈高。
請用肉眼觀察以及程式碼的觀察,來說明Fig. 1, 2,3 各代表 R、G、B 中的那一原色。
(2)新增一個code 區塊,將Lena影像中的紅色影像減少20%。
(hint: lena_img[:,:,X]=lena_img[:,:,X]*0.9, 就代表將X色光通道減弱 10%)
恭喜你,做到這步實驗課就結束囉。通知助教來檢查。最後這個範例你可以執行看看,用滑鼠調整RGB值,來觀察影像。
#coding: utf-8
%matplotlib inline
import numpy as np
import scipy.misc as misc
import matplotlib.pyplot as plt
from IPython.html.widgets import interact, interactive, fixed
from IPython.display import display
lena_img=misc.imread('lena_std.jpg');
lims = (0.1,1.0,0.1)
def edit_image( r=1.0, g=1.0, b=1.0):
lena_img2=lena_img*0
lena_img2[:,:,0] = r*lena_img[:,:,0]
lena_img2[:,:,1] = g*lena_img[:,:,1]
lena_img2[:,:,2] = b*lena_img[:,:,2]
plt.imshow(lena_img2)
#return lena_img2
w = interactive(edit_image, r=lims, g=lims, b=lims)
display(w)