Setting the xtick.alignment paramter locally

Hello,

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?

Thanks

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

something like

ax.set_xticklabels(['1','2','3','4'], 
                   fontdict={'horizontalalignment': 'center'})

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

for label in ax.xaxis.get_xticklabels():
    label.set_horizontalalignment('right')