I'm using matplotlib to generate a diagram, and I use set_ticks
and set_ticklabels
to mark out several important values on the x-axis. But some of these labels are too close and get overlapped. But I don't know how to move these labels without moving the ticks.
Here is an illustration:
I have tried several tricks but failed, and I haven't found an answer to this question on the Internet. Any help, thanks.
Update:
The source code:
fig = mpl.figure( figsize=(3, 3) ) # force figsize here to reproduce the problem.
ax = fig.add_subplot(1, 1, 1, frameon=False)
ax.set_xlim(-0.015, 1.515)
ax.set_ylim(-0.01, 1.01)
ax.set_xticks([0, 0.3, 0.4, 1.0, 1.5])
ax.grid(True)
mpl.show()
And the result:
I have tried several tricks and the best one is:
fig = mpl.figure( figsize=(3, 3) )
ax = fig.add_subplot(1, 1, 1, frameon=False)
ax.set_xlim(-0.015, 1.515)
ax.set_ylim(-0.01, 1.01)
ax.set_xticks([0, 0.3, 0.4, 1.0, 1.5])
ax.set_xticklabels([0.0, "", "", 1.0, 1.5])
ax.set_xticks([0.35], minor=True)
ax.set_xticklabels(["0.3 0.4"], minor=True)
ax.grid(True)
mpl.show()
But there is a minor tick at x = 0.35 and I don't know how to remove it.
Update 2:
OK, I found how to remove minor ticks myself:
for line in ax.xaxis.get_minorticklines():
line.set_visible(False)
In this manner, we can utilize minor ticks to put tick labels anywhere we want.
Thanks to all warm-hearted!