的代碼不同的結果: -變化語法導致輸出
def build_graph_year(self,year):
data=self.req_da(0,year)
n=[]
h=[]
for i in data:
t=''
for x in range(0,len(i),1):
z=i[x]
if z!='-':
t=t+z
if z=='-':
n.append(t)
h.append(i[x+1:])
fig=plt.figure("Graph For Year {0}".format(year))
rect=fig.patch
rect.set_facecolor('green')
pos=np.arange(0,len(n),1)+1
graph1=fig.add_subplot(1,1,1)
graph1.barh(pos,h,align='center',color='r')
graph1.set_xlabel('Height',color='w')
graph1.set_ylabel('Names',color='w')
graph1.set_title('Height Of Students in the year {0}'.format(year),color='w')
graph1.tick_params(axis='x',color='w')
graph1.tick_params(axis='y',color='w')
graph1.spines['top'].set_color('black')
graph1.spines['bottom'].set_color('black')
graph1.spines['left'].set_color('black')
graph1.spines['right'].set_color('black')
plt.yticks(pos,n)#OVER HERE
plt.show()
的代碼工作正常。但是當我將第二行的「plt」替換爲「graph1」時,勾號位置不再被存儲在列表n中的字符串替換。反而會發生錯誤: -
Traceback (most recent call last):
File "F:\Python\Python-Programs\Graph_use_1_heights.py", line 109, in <module>
h.build_graph_year('2016')
File "F:\Python\Python-Programs\Graph_use_1_heights.py", line 105, in build_graph_year
graph1.yticks(pos,n)
AttributeError: 'AxesSubplot' object has no attribute 'yticks'
但是,如果我用「.set_yticks'取代」 .yticks',因此不再發生錯誤,但刻度位置並不在列表n中的字符串替換。
爲什麼會發生這種情況,graph.set_yticks(pos,n) 和plt.yticks(pos,n)之間的區別是什麼?
我相信不同的是選擇到** **情節的y_ticks並選擇設置** y_ticks值**包含在graph1對象中。 –
在繪圖之前,我正在設置graph1對象中的yticks的值,所以不應該將它繪製出來並修改爲我? –