Adding a custom tick and label

后端 未结 1 1436
误落风尘
误落风尘 2021-01-18 14:44

I would like to add a custom major tick and label in matplotlib. A typical use is to add a label at the location math.pi with the label \"$\\pi$\".

相关标签:
1条回答
  • 2021-01-18 15:02

    This is what I usually do, though I've never been completely satisfied with the approach. There may be a better way, without calling draw().

    fig,ax=plt.subplots()
    x=linspace(0,10,1000)
    x.plot(x,exp(-(x-pi)**2))
    plt.draw() # this is required, or the ticklabels may not exist (yet) at the next step
    labels = [w.get_text() for w in ax.get_xticklabels()]
    locs=list(ax.get_xticks())
    labels+=[r'$\pi$']
    locs+=[pi]
    ax.set_xticklabels(labels)
    ax.set_xticks(locs)
    ax.grid()
    plt.draw()
    

    enter image description here

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