首页 > 解决方案 > 如何使用 matplotlib 修复注释问题

问题描述

我目前正在尝试在 的每个标记位置下绘制两个线图以及注释ylabel。但是,我面临一个问题,即仅绘制第一个而不是在其位置。

这是我的代码:

import matplotlib.pyplot as plt
ax = plt.subplot(111)
first =[0.9122,0.9091,0.9073,0.898,0.888,0.8855,0.8831,0.8837,0.8815,0.8612,0.8628,0.8419,0.8022,0.7805,0.7414]

second=[0.9499,0.9472,0.9421,0.938,0.9401,0.94,0.9417,0.9387,0.9398,0.9395,0.9263,0.9115,0.9089,0.9050,0.8893]

x = [10,20,30,40,50,60,70,80,90,100,200,400,600,800,1000]
xi = [i for i in range(0, len(x))]
plt.xticks(xi, x)

ax.plot(xi, first, marker='x')
ax.plot(xi, second, marker='^')

plt.xlabel('xlabel')
plt.ylabel('ylabel')

ax.legend(loc='upper center',
          bbox_to_anchor=(0.5,  # horizontal
                      1.12),# vertical
          ncol=3, fancybox=True)

for x, y in zip(x, first):
    label = y * 100

    ax.annotate(label,  # this is the text
            (x, y),  # this is the point to label
            textcoords="offset points",  # how to position the text
            xytext=(0, 0),  # distance from text to points (x,y)
            ha='center')

plt.show()

标签: pythonmatplotlibdata-visualization

解决方案


推荐阅读