繁体   English   中英

双x轴图表不适用于plt.clf标签

[英]Twin x-axis chart doesn't work with plt.clf labels

[ http://queirozf.com/entries/add-labels-and-text-to-matplotlib-plots-annotation-examples#add-text-to-plot][1]

我在这里的图表中添加了一个图表-但我无法正确读取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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM