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$\"
.
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()