Source From Here
Question
Is it possible, with Matplotlib, to print the values of each point on the graph? For example, if I have:
How can I display y values on the plot (e.g. print a 5 near the (0,5) point, print a 3 near the (1,3) point, etc.)?
How-To
You can use the annotate command to place text annotations at any x and y values you want. To place them exactly at the data points you could do this:
If you want the annotations offset a little, you could change the annotate line to something like:
Question
Is it possible, with Matplotlib, to print the values of each point on the graph? For example, if I have:
- import numpy as np
- from matplotlib import pyplot as plt
- x = np.arange(0,10)
- y = np.array([5,3,4,2,7,5,4,6,3,2])
- plt.plot(x,y)
How can I display y values on the plot (e.g. print a 5 near the (0,5) point, print a 3 near the (1,3) point, etc.)?
How-To
You can use the annotate command to place text annotations at any x and y values you want. To place them exactly at the data points you could do this:
- import numpy as np
- from matplotlib import pyplot as plt
- x = np.arange(10)
- y = np.array([5,3,4,2,7,5,4,6,3,2])
- fig = plt.figure()
- ax = fig.add_subplot(111)
- ax.set_ylim(0,10)
- plt.plot(x,y)
- for i,j in zip(x,y):
- ax.annotate(str(j),xy=(i,j))
- plt.show()
If you want the annotations offset a little, you could change the annotate line to something like:
- ax.annotate(str(j),xy=(i,j+0.5))
沒有留言:
張貼留言