沿Matplotlib曲线进行Anottating

时间:2017-08-19 22:13:46

标签: python matplotlib annotations

我根据经典的麦克阿瑟 - 威尔逊模型(" The Island of Island Biogeography",Princeton University Press,)编写了一个简单的脚本,用于绘制大/小和近/远岛屿的移民/消亡曲线。 1967年)。

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 个答案:

答案 0 :(得分: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