对数图中重叠的轴刻度标签 [英] Overlapping axis tick labels in logarithmic plots

查看:99
本文介绍了对数图中重叠的轴刻度标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码大约一年前使用pyplot运行得很好.我使用对数y轴使用plt.plot(x,y)绘制了一个图,并用以下自定义集替换了y轴的刻度和刻度标签:

I have some code that worked very well a year or so ago using pyplot; I made a plot using plt.plot(x,y) using a logarithmic y-axis and replaced the y-axis ticks and tick labels with a custom set as follows:

# 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)

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

After recently updating my python installation to the current version of miniconda, I now find that while the new labels still appear, they are partly overwritten by matplotlib's default labels in scientific notation. So it appears that whereas the above code used to replace the default ticks and labels, it now merely adds to them.

我该怎么做才能恢复预期的行为?为什么首先要改变它?

What do I have to do regain the desired behavior? And why did it change in the first place?

推荐答案

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

The problem you encountered is a known bug which is not easy to fix. The core of the problem is the mixing of major and minor ticks; setting the yticks redefines the major ticks, and the minor ticks are causing the overlaps.

解决此问题之前的一种解决方法是使用plt.minorticks_off()(或使用面向对象的API的ax.minorticks_off())手动禁用次要滴答声:

A workaround until the issue is fixed is to manually disable the minor ticks using plt.minorticks_off() (or ax.minorticks_off() using the object-oriented 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)

关于何时发生的更改:附带了使用matplotlib 2.0进行的默认样式更改.

这篇关于对数图中重叠的轴刻度标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆