Modify a specific x-axis tick label in python

后端 未结 1 1930
清酒与你
清酒与你 2021-01-26 00:38

I am an undergrad newbie to the Python pyplot. What I want to do is to plot a function against a sequence, for example, x = [1,2,3,4,5].

The pyplot.p

1条回答
  •  温柔的废话
    2021-01-26 01:16

    This is how you do it:

    from matplotlib import pyplot as plt
    
    x = [1,2,3,4,5]
    y = [1,2,0,2,1]
    
    plt.clf()
    plt.plot(x,y,'o-')
    ax = plt.gca() # grab the current axis
    ax.set_xticks([1,2,3]) # choose which x locations to have ticks
    ax.set_xticklabels([1,"key point",2]) # set the labels to display at those ticks
    

    By omitting 4 and 5 from your xtick list, they won't be shown.

    0 讨论(0)
提交回复
热议问题