【问题标题】:Matplotlib: Tick labels positionMatplotlib:勾选标签位置
【发布时间】:2014-07-08 22:10:46
【问题描述】:

我想绘制一条 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 轴上的其他刻度标签在相同的垂直位置开始。

【问题讨论】:

  • 我不懂你。你只是想要:pl.gca().set_yticks([0.0,0.2, 0.4, 0.6, 0.8, 1.0]) pl.gca().set_xticks([0.2, 0.4, 0.6, 0.8, 1.0])??

标签: python matplotlib plot


【解决方案1】:

我想你想修剪 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 轴,并对此感到满意。

【讨论】:

  • 是的,这是另一种方式,修剪一个刻度标签。但是如何将 y 轴 (0) 上的第一个刻度标签移动到与 x 轴 (0.8, ...) 上的刻度标签相同的高度。如何将0移到两个轴的原点?
猜你喜欢
  • 2017-08-31
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 2019-08-25
  • 1970-01-01
相关资源
最近更新 更多