Question

I have data in a numpy record array:

a = np.array([(29.40818036, '1'), (34.96458222, '2'), (16.05225074, '3'),
       (13.23025364, '4'), (6.340924671, '5+')], 
       dtype=[('f0', '<f8'), ('f1', 'S2')])

And I'm plotting a bar graph like this:

plt.bar(np.arange(5)+0.5,a['f0'],width=1,color='0.95')
plt.ylim(0,40)
plt.xlim(0.5,5.5)
ax=plt.gca()
ax.set_xticklabels(a['f1'])

Giving:

enter image description here

Note the x-axis values do not align correctly with the bars, the first value in a['f1'] is missing ('1').

a['f1'] is ['1' '2' '3' '4' '5+'] - I was expecting these 5 strings to placed underneath the five bars. However, they are shunted to the left by one and the '1' drops off. I am looking for a way to 'shunt' the values to the right.

What is the best way to adjust the x-axis tick labels?

Was it helpful?

Solution

You have to set the tick positions first:

ax.set_xticks(np.arange(5) + 1.)
ax.set_xticklabels(a['f1'])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top