import matplotlib.pyplot as plt
import numpy as np
plt.clf()
# using some dummy data for this example
n = 25
xs = np.linspace(1, 100, num=n)
ys = np.random.normal(loc=3, scale=0.4, size=n)
# 'bo-' means blue color, round points, solid lines
plt.plot(xs,ys,'bo-')
# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):
label = "{:.2f}".format(y)
plt.annotate(label, # this is the text
(x,y), # these are the coordinates to position the label
textcoords="offset points", # how to position the text
xytext=(10,10), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
def annot_max(x,y, ax=None):
xmax = x[np.argmax(y)]
ymax = y.max()
#text= "x={:.3f}, y={:.3f}".format(xmax, ymax)
label = "{:.2f}".format(ymax)
if not ax:
ax=plt.gca()
kw = dict(xycoords='data',textcoords="offset points",ha="center",bbox=dict(facecolor='none', edgecolor='black', pad=2.0))
ax.annotate(label, xy=(xmax, ymax), xytext=(10,10),color='red', **kw)
annot_max(xs,ys)
for pos in ['right', 'top']:
plt.gca().spines[pos].set_visible(False)
plt.savefig("ex401.png", dpi=100)
plt.show()
Like this:
Like Loading...
Related
Recent Comments