Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

python - 双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]

([ 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...