[ http://queirozf.com/entries/add-labels-and-text-to-matplotlib-plots-annotation-examples#add-text-to-plot][1]
([ http://queirozf.com/entries/add-labels-and-text-to-matplotlib-plots-annotation-examples#add-text-to-plot][1])
I have added a chart within a chart here - but I can't get the ax2 array to read correctly - it just keeps repeating 17 - I assume the last value of the ys2 array here
(我在这里的图表中添加了一个图表-但我无法正确读取ax2数组-它一直重复17-我假设这里ys2数组的最后一个值)
ys1 = [0. 0.12 0.17 0.29 0.14 0.33 0.25 0.29 0.07 0.38 0.73 0.54 0.62 0.94]
shape = (14,)
ys2 = [ 9. 16. 12. 14. 14. 18. 16. 14. 15. 13. 15. 13. 13. 17.]
shape = (14,)
xs1 & xs2 are assigned with # np.linspace(0,14,len(data))
import numpy as np
import matplotlib.pyplot as plt
print (x1data)
print(x1data.shape)
# array of numbers bwtween zero and 0.94 data = x1data
xax1 = np.linspace(0,14,len(data))
# An array of 14 numbers between 10 and 21 print (x2data)
rint(x2data.shape)
datax2= x2data xax2 = np.linspace(0,14,len(datax2))
xs = xax2 ys = datax2
color = 'tab:red' fig = plt.figure(1, (7,4)) ax =
fig.add_subplot(1,1,1)
ax.plot(xax1, data, color=color)
ax2 = ax.twinx() # instantiate a second axes
color = 'tab:blue' ax2.set_ylabel('# of Countries', color=color)
ax2.plot(xax2, datax2, color=color)
ax2.tick_params(axis='y', labelcolor=color)
# zip joins x and y coordinates in pairs
for x,y in zip(xs,ys):
plt.label = "{:.1f}".format(y)
plt.annotate(label,
(x,y),
textcoords="offset points",
xytext=(0,10),
ha='center')
plt.xticks(np.arange(0,14,1))
plt.yticks(np.arange(0,100,10))
plt.show()
ask by Edward translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…