使用matplotlib来注释某些点

Jes*_*ose 4 python matplotlib

虽然我可以将代码拼凑在一起绘制XY图,但我想要一些额外的东西:

  • 垂直线从X轴向上延伸到指定距离
  • 文本注释那一点,接近是必须的(见红色文字)
  • 图形是自包含的图像:一个800长的序列应占据800像素的宽度(我希望它与特定图像对齐,因为它是一个强度图)

在此输入图像描述

如何在mathplotlib中创建这样的图形?

fra*_*xel 7

你可以这样做:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
data = (0, 2, 3, 5, 5, 5, 9, 7, 8, 6, 6)
ax.plot(data, 'r-', linewidth=4)

plt.axvline(x=5, ymin=0, ymax=4.0 / max(data), linewidth=4)
plt.text(5, 4, 'your text here')
plt.show()
Run Code Online (Sandbox Code Playgroud)

注意,有些奇怪的是yminymax值的运行0 to 1,因此需要对轴进行标准化

在此输入图像描述


编辑: OP修改了代码使其更加OO:

fig = plt.figure()
data = (0, 2, 3, 5, 5, 5, 9, 7, 8, 6, 6)

ax = fig.add_subplot(1, 1, 1)
ax.plot(data, 'r-', linewidth=4)
ax.axvline(x=5, ymin=0, ymax=4.0 / max(data), linewidth=4)
ax.text(5, 4, 'your text here')
fig.show()
Run Code Online (Sandbox Code Playgroud)