如何更改matplotlib中的对数刻度刻度标签

时间:2017-08-14 09:47:50

标签: python matplotlib

我正在尝试更改matplotlib中日志图的刻度标签,这通常可以通过手动设置标签来正常工作。但是,通常会出现下面显示的问题,手动移动标签似乎会保留一些旧标签。知道如何解决这个问题吗?

import matplotlib.pyplot as plt
%matplotlib inline

fig, ax = plt.subplots()
x = [1, 10]
y = [0, 1]
ax.plot(x, y)
ax.set_xscale('log')
ax.set_xlim(0, 10)

ax.set_xticks([2.5, 7.5])

另外,我最近升级到了matplotlib 2.0.2,我不记得曾经见过这种行为。

enter image description here

1 个答案:

答案 0 :(得分:2)

显示的值是次要刻度,要禁用它们,您可以声明:

ax.minorticks_off()

这将导致7.5标记的滴答标签。

您可能需要的是以下解决方案:

from matplotlib.ticker import StrMethodFormatter, NullFormatter
ax.xaxis.set_major_formatter(StrMethodFormatter('{x:.1f}'))
ax.xaxis.set_minor_formatter(NullFormatter())
相关问题