Python 沿Matplotlib曲线的附着

Python 沿Matplotlib曲线的附着,python,matplotlib,annotations,Python,Matplotlib,Annotations,我根据经典的麦克阿瑟·威尔逊模型(“岛屿生物地理学理论”,普林斯顿大学出版社,1967年),为绘制大/小和近/远岛屿的移民/灭绝曲线写了一个简单的脚本 结果如下: 它工作得很好,但我想注释曲线,根据其大小和与物种库的距离来识别每一条曲线。下图显示了我希望获得的内容: 我尝试了提供的解决方案,这看起来很有希望,但无法使其起作用。我修改了我的代码如下: fig, axes = plt.subplots() plt.xlabel("No. of Species (R)") plt.ylabel("

我根据经典的麦克阿瑟·威尔逊模型(“岛屿生物地理学理论”,普林斯顿大学出版社,1967年),为绘制大/小和近/远岛屿的移民/灭绝曲线写了一个简单的脚本

结果如下:

它工作得很好,但我想注释曲线,根据其大小和与物种库的距离来识别每一条曲线。下图显示了我希望获得的内容:

我尝试了提供的解决方案,这看起来很有希望,但无法使其起作用。我修改了我的代码如下:

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函数的正确参数

有人能给我一些提示吗

提前谢谢


最好的祝愿,

构建到的链接,与批准的答案大致相同,但在重构代码之后

这就是我刚才从答案中复制的代码

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()

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()