Setting the xtick.alignment paramter locally


To set the x-axis tick alignment globally, we can set xtick.alignment in a matplotlibrc file. But how do I this on a per plot basis? For example, I can do this:

mpl.rcParams[‘xtick.alignment’] = ‘center’
plt.plot([1,2,3], [5,3,9])

mpl.rcParams[‘xtick.alignment’] = ‘left’
plt.plot([1,2,3], [5,3,9])

but how can I get/set the alignment on a specific plot?


The most straightforward way is probably to pass it in as an argument to set_{x,y}ticklabels

something like

                   fontdict={'horizontalalignment': 'center'})

and if you don’t want to manually set labels: (c&p @quantum.analyst) from centered ticklabels

for label in ax.xaxis.get_xticklabels():