Home > front end >  python how do I move my y axis labels to the middle of my horizontal bar graph
python how do I move my y axis labels to the middle of my horizontal bar graph

Time:09-15

this is my code

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)

This is my output enter image description here

I want labels of my y axis above x=0 not on the extreme left. How do I do that?

CodePudding user response:

You could move the left spine and hide the top and right ones:

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 move yaxis center

If you want to keep the frame, duplicate the spine:

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 moved axis frame remaining

  • Related