Python hide ticks but show tick labels

前端 未结 9 757
Happy的楠姐
Happy的楠姐 2020-12-07 11:23

I can remove the ticks with

ax.set_xticks([]) 
ax.set_yticks([]) 

but this removes the labels as well. Any way I can plot the tick labels b

9条回答
  •  囚心锁ツ
    2020-12-07 11:59

    Thanks for your answers @julien-spronck and @cmidi.
    As a note, I had to use both methods to make it work:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(11, 3))
    
    data = np.random.random((4, 4))
    
    ax1.imshow(data)
    ax1.set(title='Bad', ylabel='$A_y$')
    # plt.setp(ax1.get_xticklabels(), visible=False)
    # plt.setp(ax1.get_yticklabels(), visible=False)
    ax1.tick_params(axis='both', which='both', length=0)
    
    ax2.imshow(data)
    ax2.set(title='Somewhat OK', ylabel='$B_y$')
    plt.setp(ax2.get_xticklabels(), visible=False)
    plt.setp(ax2.get_yticklabels(), visible=False)
    # ax2.tick_params(axis='both', which='both', length=0)
    
    ax3.imshow(data)
    ax3.set(title='Nice', ylabel='$C_y$')
    plt.setp(ax3.get_xticklabels(), visible=False)
    plt.setp(ax3.get_yticklabels(), visible=False)
    ax3.tick_params(axis='both', which='both', length=0)
    
    plt.show()
    

提交回复
热议问题