Following code generates a barplot with the xticklabels centered for each bar. However, scaling the x-axis, changing the number of bars or changing the bar width does alter
So the problem is that you only call ax.set_xticklabels
. This fixes the labels, but the tick positions are still handled by the AutoLocator, which will add/remove ticks upon changing the axis limits.
So you also need to fix the tick positions:
ax.set_xticks(x)
ax.set_xticklabels(xl)
By calling set_xticks
the AutoLocator
is replaced under the hood with a FixedLocator.
And then you can center the bars to make it look nicer (optional):
ax.bar(x, y, 0.5, align='center')