在对数图中重叠轴刻度标签

Gra*_*tty 6 python matplotlib

我有一些代码在大约一年前使用pyplot工作得非常好; 我plt.plot(x,y)使用对数y轴绘制了一个绘图,并用自定义集替换了y轴刻度和刻度标签,如下所示:

# set the axis limits
Tmin = -100  # min temperature to plot
Tmax = 40    # max temperature
Pmin = 100   # min pressure
Pmax = 1000  # max pressure
plt.axis([Tmin, Tmax, Pmax, Pmin])

# make the vertical axis a log-axis
plt.semilogy()

# make a custom list of tick values and labels
plist = range(Pmin,Pmax,100)
plabels = []
for p in plist:
    plabels.append(str(p))

plt.yticks(plist,plabels)
Run Code Online (Sandbox Code Playgroud)

在最近将我的python安装更新到当前版本的miniconda之后,我现在发现虽然新标签仍然出现,但它们被matplotlib的科学记谱法的默认标签部分覆盖.因此看起来,虽然上面的代码用于替换默认的刻度和标签,但它现在只是增加了它们.

我该怎样做才能重新获得理想的行为?为什么它首先改变了?

And*_*eak 5

您遇到的问题是一个已知的错误不容易修复。问题的核心是主要刻度和次要刻度的混合;设置yticks重新定义主要刻度,次要刻度导致重叠。

plt.minorticks_off()解决问题之前的解决方法是使用(或ax.minorticks_off()使用面向对象的 API)手动禁用次要刻度:

Tmin = -100  # min temperature to plot
Tmax = 40    # max temperature
Pmin = 100   # min pressure
Pmax = 1000  # max pressure
plt.axis([Tmin, Tmax, Pmax, Pmin])

# make the vertical axis a log-axis
plt.semilogy()
plt.minorticks_off() # <-- single addition

# make a custom list of tick values and labels
plist = range(Pmin,Pmax,100)
plabels = []
for p in plist:
    plabels.append(str(p))

plt.yticks(plist,plabels)
Run Code Online (Sandbox Code Playgroud)

禁用小刻度的结果:无重叠

至于更改发生的时间:它附带了使用 matplotlib 2.0 进行的默认样式更改。