刻度标签位置

3 投票
1 回答
4964 浏览
提问于 2025-04-18 12:28

我想画一个ROC曲线,但是在两个坐标轴上都出现了“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轴上其他刻度标签的垂直位置保持一致。

1 个回答

4

我觉得你想要修剪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并不支持交叉坐标轴的二维图。如果你确定两个轴的零点都在左下角,那么我建议你手动把它放在那里:

#!/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轴,我对此很满意。

撰写回答