Matplotlib - Different tick label alignment along the same axis

蓝咒 提交于 2019-12-06 06:21:58

Once you fix the ticks, you will be able to access and set the ticklabels' alignment as you wish to avoid overlapping between neighboring subplots.

import matplotlib.pyplot as plt

plt.rcParams.update({"xtick.direction" : "in", 
                     "ytick.direction" : "in",
                     "figure.subplot.wspace" : 0.02,
                     "axes.xmargin" : 0,
                     "figure.figsize" : (5,2.4)})

fig, axes = plt.subplots(1,2, sharey=True)
for ax in axes:
    ax.plot([0,.3,.6], [0,1,2])
    # Fix ticks
    ax.set_xticks([0,.2,.4,.6])
    # Get ticklabels for fixed ticks
    ticklabels = ax.get_xticklabels()
    # set the alignment for outer ticklabels
    ticklabels[0].set_ha("left")
    ticklabels[-1].set_ha("right")

plt.show()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!