2017-08-19 104 views
0

根據經典的麥克阿瑟威爾遜模型(「The Theory of Island Biogeography」,普林斯頓大學出版社,1993),我寫了一個簡單的腳本來描繪大/小島和近島/遠島的移民/消亡曲線。 1967年)。沿着Matplotlib曲線的凹凸

from __future__ import division 
from math import log 
import numpy as np 
import matplotlib.pyplot as plt 

I0 = log(1) 
b = 0.1 
d = 0.01 

s = np.linspace(0, 50, 10) 
z1 = np.exp(I0 - b * s) 
z2 = np.exp(d * s) - 1 

I0 = log(1/2) 
d = 0.014 
z3 = np.exp(I0 - b * s) 
z4 = np.exp(d * s) - 1 

plt.xlabel("No. of Species (R)") 
plt.ylabel("Rate (I or E)") 
plt.ylim(0.0, 1.0) 
plt.plot(s, z1) 
plt.plot(s, z2) 
plt.plot(s, z3, linestyle="--") 
plt.plot(s, z4, linestyle="--") 
plt.show() 

下面是結果:

enter image description here

它工作正常,但我想註釋的曲線,以確定根據從種庫的大小和距離各一臺。下圖顯示了數字我想什麼來獲得:

enter image description here

我試圖提供here的解決方案,它看起來很有希望,但不能使它工作。我修改我的代碼,如下所示:

fig, axes = plt.subplots() 
plt.xlabel("No. of Species (R)") 
plt.ylabel("Rate (I or E)") 
plt.ylim(0.0, 1.0) 
line1, = axes.plot(s, z1) 
line2, = axes.plot(s, z2) 
line3, = axes.plot(s, z3, linestyle="--") 
line4, = axes.plot(s, z4, linestyle="--") 
label_line(line3, "Some Label", s, z3, color="black") 

但無法找出與我的代碼相關的label_line函數的正確參數。

有人能給我幾個提示嗎?

在此先感謝!

最好的問候,

回答

1

大廈關閉鏈接到question you provided中,answer by @DilithiumMatrix大約是一樣的認可的答案,但重構代碼後。

所以,這是我剛剛從該答案複製的代碼。

def label_line(line, label, x, y, color='0.5', size=12): 
    """ 
    Add a label to a line, at the proper angle. 

    Arguments 
    --------- 
    line : matplotlib.lines.Line2D object, 
    label : str 
    x : float 
     x-position to place center of text (in data coordinated 
    y : float 
     y-position to place center of text (in data coordinates) 
    color : str 
    size : float 
    """ 
    xdata, ydata = line.get_data() 
    x1 = xdata[0] 
    x2 = xdata[-1] 
    y1 = ydata[0] 
    y2 = ydata[-1] 

    ax = line.get_axes() 
    text = ax.annotate(label, xy=(x, y), xytext=(-10, 0), 
         textcoords='offset points', 
         size=size, color=color, 
         horizontalalignment='left', 
         verticalalignment='bottom') 

    sp1 = ax.transData.transform_point((x1, y1)) 
    sp2 = ax.transData.transform_point((x2, y2)) 

    rise = (sp2[1] - sp1[1]) 
    run = (sp2[0] - sp1[0]) 

    slope_degrees = np.degrees(np.arctan2(rise, run)) 
    text.set_rotation(slope_degrees) 
    return text 

這是我改編自你的問題

from __future__ import division 
from math import log 
import numpy as np 
import matplotlib.pyplot as plt 

I0 = log(1) 
b = 0.1 
d = 0.01 

s = np.linspace(0, 50, 10) 
z1 = np.exp(I0 - b * s) 
z2 = np.exp(d * s) - 1 

I0 = log(1/2) 
d = 0.014 
z3 = np.exp(I0 - b * s) 
z4 = np.exp(d * s) - 1 

plt.xlabel("No. of Species (R)") 
plt.ylabel("Rate (I or E)") 
plt.ylim(0.0, 1.0) 
line = plt.plot(s, z1, color="red") 
line2 = plt.plot(s, z2, color="green") 
line3 = plt.plot(s, z3, linestyle="--", color="blue") 
line4 = plt.plot(s, z4, linestyle="--", color="yellow") 

# Above this line, I just added the colours to the lines so it is easier to read which text is for which line 

# we annotate each of the lines with data coordinates. 
label_line(line[0], "Label 1", s[2], z1[2], color="red") 
# label_line(line[0], "Label 1", s[1], z1[1], color="red") # this would move "Label 1" up a little along the red line 
label_line(line2[0], "Label 2", s[5], z2[5], color="green") 
label_line(line3[0], "Label 3", s[1], z3[1], color="blue") 
label_line(line4[0], "Label 4", s[5], z4[5], color="yellow") 

plt.show() 

enter image description here

+0

感謝代碼,它的工作! – maurobio