[英]change the position (move) of tick labels when plotting with matplotlib
[英]Matplotlib: Tick labels position
我想绘制一条roc曲线,我使用了这段代码: http : //scikit-learn.org/stable/auto_examples/plot_roc.html 。 但是刻度标签0.0出现在两个轴上,我通过直接设置标签删除了一个刻度标签:
pl.gca().set_yticks([0.2, 0.4, 0.6, 0.8, 1.0])
pl.gca().set_xticks([0.0, 0.2, 0.4, 0.6, 0.8, 1.0])
x轴的刻度标签“0.0”如何与y轴对齐? 标签应移动到y轴的左边界,它开始于与y轴中其他刻度标签相同的垂直位置。
我想你要修剪x轴:
#!/usr/bin/env python3
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
data = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(data,data)
ax.xaxis.set_major_locator(MaxNLocator(5, prune='lower'))
ax.yaxis.set_major_locator(MaxNLocator(4))
fig.savefig("1.png")
编辑 :
可悲但真实:matplotlib不适用于交叉轴2D图。 如果您确定两个轴的零点位于左下方的方框角落,而不是我建议手动将其放在那里:
#!/usr/bin/env python3
import matplotlib
from matplotlib import pyplot as plt
from matplotlib.ticker import MaxNLocator
data = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(data,data)
ax.xaxis.set_major_locator(MaxNLocator(5, prune='lower'))
ax.yaxis.set_major_locator(MaxNLocator(4, prune='lower'))
fig.tight_layout()
ax.text(-0.01, -0.02,
"0",
horizontalalignment = 'center',
verticalalignment = 'center',
transform = ax.transAxes)
fig.savefig("1.png")
这里可以手动调整零位。
我个人根据情况修剪x或y轴,并对此感到满意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.