以对数轴

时间:2018-01-05 05:18:46

标签: python-2.7 matplotlib axis-labels

我正在尝试编辑刻度标签,但即使在设置了刻度后,我仍然会获得科学记数法。这是一个MWE:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(9, 7))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)

ax.ticklabel_format(style='plain')

plt.plot([1,4],[3,6] )

ax.set_yscale('log')
ax.set_xscale('log')

ax.set_xticks([0.7,1,1.5,2,2.5,3,4,5])

ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())

产生这个情节

enter image description here

正如您所看到的,ax.ticklabel_format(style='plain')似乎不起作用,因为我不断使用科学记数法获得刻度标签,并且在使用ax.set_xticks时,旧的刻度标签仍然存在。我看了一下this主题,似乎问题在于选择刻度线,如果我使用例如0.3而不是0.7作为它的第一个刻度,但是我需要在这里做一个情节具体范围和使用对数刻度。

有什么解决方法吗?

1 个答案:

答案 0 :(得分:1)

实际上,您的代码正在执行您需要的操作,问题是来自次要标记的标签不受影响且与主要标记重叠

你可以简单地添加一行:

ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())

完整代码:

import matplotlib.pyplot as plt

fig, ax = plt.subplots(figsize=(9, 7))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)

ax.ticklabel_format(style='plain')

plt.plot([1,4],[3,6] )

ax.set_yscale('log')
ax.set_xscale('log')

ax.set_xticks([0.7,1,1.5,2,2.5,3,4,5])
ax.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
ax.get_xaxis().set_minor_formatter(matplotlib.ticker.NullFormatter())

enter image description here

相关问题