向 Matplotlib 折线图上的每 3 个标记添加文本

Adding text to every 3rd marker on a Matplotlib line chart

我创建了下面的图表,每个数据点都有文本。我想要的是它显示 marker.Line 上的每第 3 个文本将保留,但每个数据点的文本应跳过两个。所以我希望看到同一行,但文本只有 10%、40%、70% 和 100%。

这是我的代码:

import pandas as pd
from matplotlib import pyplot as plt

x = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
y = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct']

df = pd.DataFrame(list(zip(x,y)), columns=['Percentages', 'Months'])

fig, ax = plt.subplots()
ax.plot(df['Months'], df['Percentages'], color='#0076B6', dashes=[6, 2], marker='o')
for j, v in enumerate(df['Percentages']):
    ax.text(j, v+2, str('{:.2f}'.format(round(v, 2)))+'%', fontsize=5.5, horizontalalignment='center')
plt.show()

希望说得通,有人可以帮忙。

提前致谢,

阿克塞尔

每隔三个元素循环一次:

for j, v in enumerate(df['Percentages'][::3]):
    ax.text(j*3, ...)