5

This is my data:

a3=pd.DataFrame({'OfficeName':['a','b','c','d','e','f','g','h'],
                 'Ratio': [0.1,0.15,0.2,0.3,0.2,0.25,0.1,0.4]})

and this is my code to draw a bar chart:

fig, ax = plt.subplots()
ind = np.arange(a3.loc[:,'OfficeName'].nunique())    # the x locations for the groups
width = 0.35         # the width of the bars
p1 = ax.bar(ind,a3.loc[:,'Ratio'],width)
ax.set_title('Ratio of refunds to purchases')
ax.set_xticklabels(a3.loc[:,'OfficeName'],ha='center')
#ax.set_xticklabels(['a','b','c','d','e','f','g','h'],ha='center')
ax.set_xlabel('x Group')
ax.set_ylabel('Ratio')
plt.show()

But, in my chart the first x label is missing:

enter image description here

I think this problem is not the same as Matplotlib: Move x-axis tick labels one position to left because I do not even need rotate the x labels.

Could anyone explain why this happens and how to fix it?

DYZ
  • 55,249
  • 10
  • 64
  • 93
Feng Chen
  • 2,139
  • 4
  • 33
  • 62

1 Answers1

0

There is an easier way to produce your bar chart in Pandas:

a3.set_index('OfficeName').plot.bar(width=0.35, legend=False)
plt.xticks(rotation=0, ha='center')

(You still need to set the x and y axis labels and the title.)

DYZ
  • 55,249
  • 10
  • 64
  • 93
  • Thanks. Could you also tell me why my problem happens? – Feng Chen Jul 24 '18 at 05:16
  • 1
    I can only tell that for some reason `ax.bar()` adds another tick at the location -1 (call `ax.get_xticks()` to see for yourself!)) The label "a" is assigned to that tick and is off the chart. – DYZ Jul 24 '18 at 05:20
  • Thanks. That really helps – Feng Chen Jul 24 '18 at 05:22
  • 2
    @FengChen I know I'm a bit late. However, FYI: I've faced the same problem with labels on X-axis and `ax.set_xticks(...)` did the job exactly in the way I wanted to. Please check out this question to get that clear: https://stackoverflow.com/questions/26131822/how-to-display-all-label-values-in-matplotlib – Roman Posokhin Mar 23 '19 at 08:34