簡體   English   中英

python 如何將 y 軸標簽移動到水平條形圖的中間

[英]python how do I move my y axis labels to the middle of my horizontal bar graph

這是我的代碼

Rate  = ['Total',"10+","20+","30+","40+","50+","60+"]
NetAmount = [4,2,5,-4,8,-6,7][::-1]
df = pd.DataFrame(NetAmount,index=Rate,columns=['Value'])


fig,ax = plt.subplots()
df.plot(ax=ax,kind='barh',legend=None,title='Value')
ax.set_ylabel('Rate',fontsize=12)

這是我的 output 在此處輸入圖像描述

我希望 x=0 上方的 y 軸標簽不在最左邊。 我怎么做?

您可以移動左側脊椎並隱藏頂部和右側脊椎:

ax.spines['left'].set_position(('data', 0))
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

# move back the ylabel to the left (optional)
ax.yaxis.set_label_coords(0, 0.5)

output:

matplotlib 移動 y 軸中心

如果要保留框架,請復制脊椎:

from copy import copy
ax.spines['left_copy'] = copy(ax.spines['left'])
ax.spines['left'].set_position(('data', 0))

ax.yaxis.set_label_coords(0, 0.5)

output:

matplotlib 移動了剩余的軸框架

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM