2017-04-14 88 views
1

我有一個線圖,部分在0的x軸的上方和下方。 如何爲線條上方的所有區域着色綠色,並且全部在紅色下方? 下面的代碼:matplotlib圖填充軸上方和下方的2種顏色

hydropathy_dict = {"I":-0.528, 
     "L":-0.342, 
     "F":-0.370, 
     "V":-0.308, 
     "M":-0.324, 
     "P":-0.322, 
     "W": -0.270, 
     "H": 2.029, 
     "T": 0.853, 
     "E": 3.173, 
     "Q": 2.176, 
     "C": 0.081, 
     "Y": 1.677, 
     "A":-0.495, 
     "S": 0.936, 
     "N": 2.354, 
     "D": 9.573, 
     "R": 4.383, 
     "G": 0.386, 
     "K": 2.101 
     } 
    seq = 'CHCRRSCYSTEYSYGTCTVMGINHRFCC' 
    hydropathy_list = [] 
    plot_x_axis = [] 
    for aa in seq: 
     hydropathy_list.append(hydropathy_dict[aa]) 
    print(acc,hydropathy_list) 
    for i in range(len(hydropathy_list)): 
     plot_x_axis.append(i) 

    plt.plot(plot_x_axis,hydropathy_list) 
    plt.plot([0,len(hydropathy_list)],[0,0]) 
    plt.show() 

回答

2

試試這個:

import matplotlib.pyplot as plt 
import numpy as np 
from scipy import interpolate 

hydropathy_dict = {"I":-0.528, 
     "L":-0.342, 
     "F":-0.370, 
     "V":-0.308, 
     "M":-0.324, 
     "P":-0.322, 
     "W": -0.270, 
     "H": 2.029, 
     "T": 0.853, 
     "E": 3.173, 
     "Q": 2.176, 
     "C": 0.081, 
     "Y": 1.677, 
     "A":-0.495, 
     "S": 0.936, 
     "N": 2.354, 
     "D": 9.573, 
     "R": 4.383, 
     "G": 0.386, 
     "K": 2.101 
     } 

seq = 'CHCRRSCYSTEYSYGTCTVMGINHRFCC' 
hydropathy_list = [] 
plot_x_axis = [] 
for aa in seq: 
    hydropathy_list.append(hydropathy_dict[aa]) 

#print(seq, hydropathy_list) 
for i in range(len(hydropathy_list)): 
    plot_x_axis.append(i) 

# convert to np array 
hydropathy_list = np.array(hydropathy_list) 
plot_x_axis = np.array(plot_x_axis) 

# densify data for filled color plot 
f = interpolate.interp1d(plot_x_axis, hydropathy_list) 
xnew = np.arange(plot_x_axis[0], plot_x_axis[-1], 0.1) 
ynew = f(xnew) 

plt.plot(plot_x_axis, hydropathy_list)  # blue line 
plt.plot([0, len(hydropathy_list)], [0,0]) # green line 

# use xnew, ynew to plot filled-color graphs 
plt.fill_between(xnew, 0, ynew, where=(ynew-1) < -1 , color='red') 
plt.fill_between(xnew, 0, ynew, where=(ynew-1) > -1 , color='green') 

plt.show() 

with interpolated points

編輯

(在中註釋的問題的回答)

聲明where=(ynew-1)<-1裏面fill_between()需要ynew作爲一個numpy數組(簡單的列表不起作用)。

Scipy.interpolate()用於沿着曲線獲取更多點,以便顏色完全填充目標區域。

憑藉獨創點,結果是這樣的:

with original points

+0

謝謝,即使有其他意見,我也無法完全實現它。插值和numpy的數組需要還是有matplotlib解決方案? – RnRoger

+0

@RnRoger,我編輯了我的答案,以澄清你的一些問題。要獲得曲線的密集點,您還可以對數據的每個段使用'np.arange()',對它們使用'np.concaternate()'。如果你仍然有問題,你可以將它作爲一個新問題發佈。 – swatchai

0

使用這種格式:

import matplotlib.pyplot as plt 

x = range(10) 
plt.fill_between(x,0,100,color='red') 
plt.fill_between(x,0,-100,color='blue') 

plt.show() 
1

看一看的matplotlib fill-between demo。那肯定會回答你的問題。

但這就是說你的代碼不顯示任何圖形定義,這使得這更加困難。通常你做到以下幾點:

fig, ax = plt.figure() 
ax.plot(plot_x_axis, line_1) 
ax.plot(plot_x_axis, line_2) 
ax.fill_between(plot_x_axis, line_1, line_2, color='r') 
ax.fill_between(plot_x_axis, line_2, line_1, color='g') 
+0

爲什麼要使用一個數字?在這種情況下更好嗎? – RnRoger

+1

它使情節工作更容易,如果你想要多個地塊,它們之間消除歧義。另外如果你需要一個數字的多軸,這是必要的。 – Grr