Reputation: 643
I've got this data frame
Poverty White Black Native_American
CHSI_State_Name
Alabama 17.085075 69.795522 28.510448 0.570149
Alaska 12.144444 54.485185 1.174074 36.159259
Arizona 16.840000 81.086667 1.886667 14.813333
...
And I plot my figure like this:
fig, ax = plt.subplots()
poverty.plot(ax = ax, figsize = (16, 8))
ax.set_xticklabels(poverty.index, rotation = 90)
fig.savefig('output7.png', xbbox_inches = 'tight')
I would expect the file to have state names under each value (all states), but instead it's only got a few with large gaps.
What am I doing wrong? Thanks!
Upvotes: 3
Views: 156
Reputation: 10724
The problem in your code is that
ax.set_xticklabels(poverty.index, rotation = 90)
sets the xticklabels
on the range of the data. If your data ranges from 10 to 16, it will put names on 10,11,12,13,14 and 15.
Instead of putting labels on each integer xtick
you want to specify the position / order of the labels as well.
By using
ax.set_xticks(range(len(poverty.index)))
You specify the range as big as the poverty.index
which makes sure that each label is positioned inside your plot.
Upvotes: 1
Reputation: 643
This seems to do it:
ax.set_xticklabels(poverty.index, rotation = 90)
ax.set_xticks(range(len(poverty.index)))
Upvotes: 1
Reputation: 21542
Try substitute ax.set_xticklabels(poverty.index, rotation = 90)
with:
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
For me it works with the expected behaviour. But actually I don't know how your method does not work. This is a proposal workaround. Hope that helps.
Upvotes: 0