2014-11-23 103 views
1

我是Python和Scipy的新手。目前我正在試圖在matplotlib中繪製一個p型晶體管傳輸曲線。這是分段定義的,我很努力地找到獲得最終曲線的好方法。到目前爲止,我所擁有的是:用python/matplotlib繪製分段定義函數

import matplotlib.pyplot as plt 
import numpy as np 
from scipy.constants import epsilon_0 

V_GS = np.linspace(-15, 10, 100) # V 
V_th = 1.9 # V 
V_DS = -10 # V 
mu_p = 0.1e-4 # m²/Vs 
epsilon_r = 7.1 
W = 200e-6 # m 
L = 10e-6 # m 
d = 70e-9 # m 
C_G = epsilon_0*epsilon_r/d 
beta = -mu_p*C_G*W/L 

Ids_cutoff = np.empty(100); Ids_cutoff.fill(-1e-12) 
Ids_lin = beta*((V_GS-V_th)*V_DS-V_DS**2/2) 
Ids_sat = beta*1/2*(V_GS-V_th)**2 

plt.plot(V_GS, Ids_lin, label='lin') 
plt.plot(V_GS, Ids_sat, label='sat') 
plt.plot(V_GS, Ids_cutoff, label='cutoff') 

plt.xlabel('V_GS [V]') 
plt.ylabel('I [A]') 
plt.legend(loc=0) 

plt.show() 

這給出了完整V_GS範圍內的三條曲線。現在,我想定義

Ids = Ids_cutoff for V_GS >= V_th 
Ids = Ids_lin for V_GS < V_th; V_DS >= V_GS - V_th 
Ids = Ids_sat for V_GS < V_th; V_DS < V_GS - V_th 

我發現一個例子np.vectorize(),但不知何故,我struggeling瞭解如何使用這些數組。我可以創建一個經歷所有值的for循環,但我相當確定有更有效的方法來執行此操作。

除了推導出Ids的值列表並繪製它與V_GS的關係之外,是否還有可能將matplotlib的三個方程式分段繪製爲一條曲線?

回答

0

看了多地進入我終於想出了一個辦法做到這一點numpy的細節後:

Ids_cutoff = -1e-12 # instead of creating an array as posted above 
# create masks for range of validity for linear and saturation region 
is_lin = np.zeros_like(V_GS, dtype=np.bool_) 
is_lin[(V_GS < V_th) & (V_DS >= V_GS - V_th)] = 'TRUE' 
is_sat = np.zeros_like(V_GS, dtype=np.bool_) 
is_sat[(V_GS < V_th) & (V_DS < V_GS - V_th)] = 'TRUE' 

# create final array and fill with off-current 
Ids = np.zeros_like(V_GS); Ids.fill(Ids_cutoff) 
# replace by values for linear and saturation region where valid 
Ids = np.where(is_lin, Ids_lin, Ids) 
Ids = np.where(is_sat, Ids_sat, Ids) 
plt.plot(V_GS, Ids, '*', label='final') 
1

是否要根據您的選擇器填充陣列Vds

Vds = np.zeros_like(V_GS) # for the same shape 
Vds[V_GS >= V_th] = Ids_cutoff 
Vds[(V_GS < V_th) & (V_DS >= V_GS - V_th)] = Ids_lin 
Vds[(V_GS < V_th) & (V_DS < V_GS - V_th)] = Ids_sat 

通過繪製分段,你的意思是忽略了一定的範圍?在

plt.plot([0,1,2,3,np.nan,10,11], np.arange(7)) 

結果:

enter image description here

由於不是一個數字不plottable,沒有行會被吸引您可以使用np.nan了點。

+0

所以我改變了中間部分: – MrCyclophil 2014-11-24 10:04:07

+0

我得到的是: IDS [V_GS> = V_th] = Ids_cutoff ValueError:NumPy布爾數組索引分配無法爲掩碼爲true的33個輸出值分配100個輸入值 – MrCyclophil 2014-11-24 10:12:22

+0

爲什麼Ids_cutoff的固定大小爲100?只需給它分配一個值,所以我們可以根據我們的掩碼將它分配給任意數量的值。 – sebix 2014-11-24 12:46:51