matplotlib xtick labels not aligned
10,095
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()
Related videos on Youtube
Comments
-
As3adTintin over 1 year
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: