沿着马厩散步

2024-04-19 21:52:54 发布

您现在位置:Python中文网/ 问答频道 /正文

根据经典的麦克阿瑟-威尔逊模型(“岛屿生物地理学理论”,普林斯顿大学出版社,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\u line函数的正确参数。你知道吗

有人能给我一些提示吗?你知道吗

提前谢谢!你知道吗

致以最诚挚的问候


Tags: fromimportlogplotnpplt曲线z3
1条回答
网友
1楼 · 发布于 2024-04-19 21:52:54

在构建到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

相关问题 更多 >