How to display x or y values within the figure Matplotlib?

Viewed 417

I want to display the x (or y) values within my figure in Matplotlib (like the following figure.). Can anyone guide me with that?

Many thanks!

enter image description here

1 Answers

You can try using annotate. Here is a code example:

import numpy as np
import matplotlib.pyplot as plt

Y = np.random.rand(10, 1)
X = np.arange(10)
plt.plot(X, Y)
for x, y in zip(X, Y):
    plt.annotate(text=str(y[0])[:5], xy=(x, y))
plt.show()

Will output this: enter image description here

Notice that text is a string so you can add either x or y or both if you'd like.

Related