2014-10-27 174 views
3
fig = plt.figure(num=1) 
ax=fig.add_subplot(111) 

def f1(x): 
    return 1/x 
def f2(x): 
    return 2/x 
def f3(x): 
    return np.sqrt(x**2-1) 
def f4(x): 
    return np.sqrt(x**2-2) 
s= np.arange(np.sqrt(2),5,0.05) 
t=np.arange(1,5,0.05) 
y1=f1(t) 
y2=f2(t) 
y3=f3(t) 
y4=f4(s) 
ax.plot(t,y1,'k-',t,y2,'k-',t,y3,'k-',s,y4,'k-') 
plt.xlim([1.1,2.1]) 
plt.ylim([0.5,1.5]) 
plt.show() 

我想填補這四個函數描述的曲線之間的區域。不確定在這種情況下如何使用fill_between選項。Python,如何在多條(4)曲線之間填充?

+0

是否要填補之間的區域最低和最高的功能? (每個給定點) – sebix 2014-10-27 07:08:28

回答

2

只有fill_between兩條曲線不是四條。因此,使用您擁有的四條曲線,可以創建兩個曲線,分別爲y5y6,這些曲線位於下方,然後使用這些曲線來fill_between。一個例子如下所示:

我也ploted使用的符號,這樣很容易看到剛剛創建的不同的新曲線的各個功能...

import pylab as plt 
import numpy as np 

fig = plt.figure(num=1) 
ax=fig.add_subplot(111) 

def f1(x): return 1/x 
def f2(x): return 2/x 
def f3(x): return np.sqrt(x**2-1) 
def f4(x): return np.sqrt(x**2-2) 

t=np.arange(1,5,1e-4) # use this if you want a better fill 
t=np.arange(1,5,0.05) 
y1=f1(t) 
y2=f2(t) 
y3=f3(t) 
y4=f4(t*(t**2>2) + np.sqrt(2)*(t**2<=2)) # Condition checking 

y5 = np.array(map(min, zip(y2, y3))) 
y6 = np.array(map(max, zip(y1, y4))) 

ax.plot(t,y1, 'red') 
ax.plot(t,y2, 'blue') 
ax.plot(t,y3, 'green') 
ax.plot(t,y4, 'purple') 

ax.plot(t, y5, '+', ms=5, mfc='None', mec='black') 
ax.plot(t, y6, 's', ms=5, mfc='None', mec='black') 

ax.fill_between(t, y5, y6, where=y5>=y6) 

plt.xlim([1.1,2.1]) 
plt.ylim([0.5,1.5]) 

plt.show()