Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib xtick labels not aligned

I created a pretty simple stacked bar chart, but for some reason my x-tick labels are off centered. I tried using some tick.label.set_ properties to line them up be to no avail. Any suggestions? Thanks!

Code:

bottom = np.vstack((np.zeros((data.shape[1],), dtype=data.dtype), np.cumsum(data, axis=0) [:-1]))
for dat, col, bot, lab in zip(data, colors, bottom, labels):
    ax1.bar(ind, dat, color=col, bottom = bot, alpha = .7, label = lab)

xticks([z for z in range(0,len(income_brackets))],[(str("{0:.0f}".format(k/1000)) + '-' + str("{0:.0f}".format(l/1000))) for k,l in income_brackets])
for tick in ax1.xaxis.get_major_ticks():
    tick.label.set_fontsize(12)
    tick.label.set_horizontalalignment('left')

Image:

enter image description here

like image 239
As3adTintin Avatar asked Jun 04 '14 15:06

As3adTintin


1 Answers

Use align="center" in your bar plot, by default they line up on the edges. Here is a minimal working example:

import pylab as plt

X = [1,2,3,4]
Y = [1,4,3,2]

fig, axes = plt.subplots(2, 1)
ax1, ax2 = axes

ax1.bar(X,Y,alpha=.5)
ax1.set_xticks(X)

ax2.bar(X,Y,align="center",alpha=.5)
ax2.set_xticks(X)           

plt.show()

enter image description here

like image 63
Hooked Avatar answered Oct 21 '22 01:10

Hooked