Aligning rotated xticklabels with their respective xticks

前端 未结 4 1452
刺人心
刺人心 2020-11-27 11:24

Check the x axis of the figure below. How can I move the labels a bit to the left so that they align with their respective ticks?

I\'m rotating the labels using:

4条回答
  •  -上瘾入骨i
    2020-11-27 11:34

    Rotating the labels is certainly possible. Note though that doing so reduces the readability of the text. One alternative is to alternate label positions using a code like this:

    import numpy as np
    n=5
    
    x = np.arange(n)
    y = np.sin(np.linspace(-3,3,n))
    xlabels = ['Long ticklabel %i' % i for i in range(n)]
    
    
    fig, ax = plt.subplots()
    ax.plot(x,y, 'o-')
    ax.set_xticks(x)
    labels = ax.set_xticklabels(xlabels)
    for i, label in enumerate(labels):
        label.set_y(label.get_position()[1] - (i % 2) * 0.075)
    

    For more background and alternatives, see this post on my blog

提交回复
热议问题