Search code examples
pythonmatplotlibannotations

Annotate every nth point on plot with Matplotlib


I have the following code to annotate a plot made with matplotlib:

for i,j in data.items():
   ax.annotate(str(j), xy=(i, j))

This annotates every point on a very point dense plot. Is there anyway to only put an annotation or label on every nth point?


Solution

  • As suggested in my comment:

    for count, (i,j) in enumerate(data.items()):
        if count % 50 == 0:
            ax.annotate(str(j), xy=(i, j))
    

    Replace 50 with a different integer if you don't want to annotate every 50'th point.