I've show the y1 label using plt.annotate, but I couldn't do the same for the y2. Could you help me?
plt.figure(figsize=(10,4))
x = [201901, 202001, 202101, 202202]
y1 = [6.86, 21.45, 6.25, 0.88]
y2 = [6.33, 6.33, 7.04, 5.63]
plt.box(False)
plt.plot(x, y1, marker='o', label='Your Return')
plt.plot(x, y2, marker='o', label='Benckmark')
plt.legend()
plt.title('Your Return vs Benckmark', pad=30)
plt.xlabel('Period')
plt.ylabel('Return')
for x, y in zip(x, y1):
label = y
plt.annotate(label, (x, y),
xycoords="data",
textcoords="offset points",
xytext=(0, 10), ha="center")
plt.show()
You just had to add another for loop for y1
to annotate the data. I've also renamed x
to x_
as the name of the iterator otherwise it overrides your original variable.
for x_, y in zip(x, y1):
label = y
plt.annotate(label, (x_, y),
xycoords="data",
textcoords="offset points",
xytext=(0, 10), ha="center", color ='blue')
for x_, y in zip(x, y2):
label = y
plt.annotate(label, (x_, y),
xycoords="data",
textcoords="offset points",
xytext=(0, -10), ha="center", color= 'orange')
Output: