python - 如何在绘图标记上添加标签

标签 python matplotlib plot plot-annotations

我正在绘制精确度/召回率曲线,并希望为图中的每个标记添加特定标签。

这是生成绘图的代码:

from matplotlib import pyplot

pyplot.plot([0, 100], [94, 100], linestyle='--')

pyplot.xlabel("Recall")
pyplot.ylabel("Precision")
list_of_rec = [
99.96,99.96,99.96,99.96,99.96,99.96,99.8,98.25,96.59,93.37,83.74,63.53,48.72,25.05,10.7,4.27,0.73,0.23]

list_of_prec = [
94.12,94.12,94.12,94.12,94.12,94.12,94.42,95.14,95.92,96.57,97.33,98.26,98.72,99.0,99.0,99.17,99.75,99.19]

list_of_markers = [
    0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 5.5, 6.0, 6.5, 7.0, 7.5, 8.0, 8.5
]

# plot the precision-recall curve for the model
pyplot.plot(list_of_rec, list_of_prec, marker='*', markersize=8)

pyplot.show()

这给了我以下情节:

enter image description here

对于图中的每个标记 (*),我想使用 list_of_markers 中的文本来标记它们。似乎没有找到将文本标签列表传递到绘图的任何选项,任何帮助表示赞赏。

最佳答案

您可以通过循环遍历每个标记并将标签作为文本注释来注释每个标记

for x, y, text in zip(list_of_rec, list_of_prec, list_of_markers):
    plt.text(x, y, text)

enter image description here

关于python - 如何在绘图标记上添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56147039/

相关文章:

python - 在用 Python 编写的基于代理的模型中使用 __slots__

python - 为重复项添加增量值

来自 3d 坐标的 Python 热图

python - 如何轻松地将颜色图应用于线图?

python - 绘制一个看起来像球体的球体

python - 如何在 pipenv 环境中处理 `pip install` (未反射(reflect)在 Pipfile 中,仅反射(reflect)在 `pipenv graph` 中)?

python - 带有 python api 的 Elasticsearch Percolator

python - 使用 Matplotlib 绘制二元高斯分布

python - Python 散点图颜色点

python - Matplotlib 也在图外扩展了最佳定位