![](/img/trans.png)
[英]How do I plot percentage labels for a horizontal bar graph in Python?
[英]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)
我希望 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:
如果要保留框架,請復制脊椎:
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:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.