Matplotlib xtick shortcuts not aligned

I created a fairly simple glass histogram, but for some reason my x-tick marks are not centered. I tried to use some properties tick.label.set_to align them, but to no avail. Any suggestions? Thank!

the 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')

Picture

enter image description here

+4
source share
1 answer

Use align="center"in your schedule, by default they line up at 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

+6
source

Source: https://habr.com/ru/post/1543228/


All Articles