python - 使用 matplotlib 标注某些点

标签 python matplotlib

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

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

enter image description here

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

最佳答案

你可以这样做:

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


编辑:OP 修改了代码以使其更面向对象:

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

关于python - 使用 matplotlib 标注某些点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10499885/

相关文章:

在OSX上的PyCharm中,matplotlib支持失败

python-3.x - 如何在散点图中制作relim()和autoscale()

python - 从字符串动态导入文件中的方法

python - Django 模板 - 无法迭代此列表

python - Pytest 运行所有测试两次并比较模拟结果和真实结果

python - numpy std计算: TypeError: cannot perform reduce with flexible type

python - 切割列表列表

python - 如何用线连接非纳米值?

python - 将 groupby 函数应用于 df 的 Matplotlib 时间序列图

python - 如何制作 Matplotlib 动画 fiddle 图?