使用matplotlib注释特定的点

4

虽然我能够破解代码来绘制XY图,但我想要一些额外的东西:

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

enter image description here

我如何在mathplotlib中制作这样的图形?


我不太明白你的第三点,你是想指定保存图像的大小,还是裁剪轴,或者其他什么? - fraxel
在我的特定情况下,我希望它(源图像的强度投影)适合一个800600的空间,就在800600的源图像下方。 - Jesvin Jose
savefig函数接受figsizedpi参数。因此,您可以轻松设置大小,还可能希望关闭坐标轴。 - fraxel
这会引入缩放伪影,看起来可能非常丑陋。 - hochl
2个回答

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

请注意,有些奇怪的是yminymax值从0到1变化,因此需要将其标准化到轴上。

enter image description here


编辑:问答者已修改代码以使其更具面向对象特征:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接