How to move tick labels off left spine

前端 未结 2 553
时光说笑
时光说笑 2020-12-22 09:57

In matplotlib, how do I move the y tick labels to the far left side of the charting areas when I have moved the left spine?

Code:

import matplotlib.p         


        
2条回答
  •  甜味超标
    2020-12-22 10:36

    Changing the transformation of the y-ticklabels back to the original y-axis transform would give you the desired ticks on the left side of the axes.

    plt.setp(ax.get_yticklabels(), transform=ax.get_yaxis_transform())
    

    Complete code for reproducibility

    import matplotlib.pyplot as plt
    
    X = range(-10,5)
    y = [i**2 for i in X]
    
    fig, ax = plt.subplots(1,1, figsize=(10,8))
    plt.plot(X, y)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    ax.spines['left'].set_position('zero')
    
    plt.setp(ax.get_yticklabels(), transform=ax.get_yaxis_transform())
    
    plt.show()
    

提交回复
热议问题