Python Matplotlib半对数图:范围较大时,小刻度线消失

Python Matplotlib半对数图:范围较大时,小刻度线消失,python,matplotlib,Python,Matplotlib,当绘制半对数图(y为对数)时,y轴上的小记号(十年内8个)会自动出现,但当轴范围超过10**10时,它们似乎会消失。我尝试了很多方法强迫他们回去,但都没有用。可能是为了避免过度拥挤,它们会离开大范围,但人们应该有一个选择?matplotlib>=2.0.2的解决方案 让我们考虑下面的例子 由该代码生成: import matplotlib.pyplot as plt import matplotlib.ticker import numpy as np y = np.arange(12) x

当绘制半对数图(y为对数)时,y轴上的小记号(十年内8个)会自动出现,但当轴范围超过10**10时,它们似乎会消失。我尝试了很多方法强迫他们回去,但都没有用。可能是为了避免过度拥挤,它们会离开大范围,但人们应该有一个选择?

matplotlib>=2.0.2的解决方案

让我们考虑下面的例子

由该代码生成:

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

y = np.arange(12)
x = 10.0**y

fig, ax=plt.subplots()
ax.plot(x,y)
ax.set_xscale("log")
plt.show()
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

y = np.arange(12)
x = 10.0**y

fig, ax=plt.subplots()
ax.plot(x,y)
ax.set_xscale("log")
plt.show()
小刻度标签确实消失了,通常显示它们的方式(如
plt.tick_参数(axis='x',which='minor')
)失败

第一步是显示轴上10的所有幂

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=12) 
ax.xaxis.set_major_locator(locmaj)
locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,1.0, ))
ax.xaxis.set_major_locator(locmaj)

其中诀窍是将
numticks
设置为等于或大于刻度数的数字(即在本例中为12或更高)

然后,我们可以添加次要标签,如下所示

locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=12)
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
locmin = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,0.2,0.4,0.6,0.8,1,2,4,6,8,10 )) 
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

请注意,我将此限制为每十年包含4个小刻度(使用8个刻度也是同样可能的,但在本例中会使轴过度拥挤)。还要注意的是,
numticks
也是(非常不直观地)12或更大

最后,我们需要对次要记号使用
NullFormatter()
,这样就不会出现任何记号标签

matplotlib 2.0.0的解决方案 以下内容适用于matplotlib 2.0.0或更低版本,但不适用于matplotlib 2.0.2

让我们考虑下面的例子

由该代码生成:

import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

y = np.arange(12)
x = 10.0**y

fig, ax=plt.subplots()
ax.plot(x,y)
ax.set_xscale("log")
plt.show()
import matplotlib.pyplot as plt
import matplotlib.ticker
import numpy as np

y = np.arange(12)
x = 10.0**y

fig, ax=plt.subplots()
ax.plot(x,y)
ax.set_xscale("log")
plt.show()
小刻度标签确实消失了,通常显示它们的方式(如
plt.tick_参数(axis='x',which='minor')
)失败

第一步是显示轴上10的所有幂

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=12) 
ax.xaxis.set_major_locator(locmaj)
locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,1.0, ))
ax.xaxis.set_major_locator(locmaj)

然后,我们可以添加次要标签,如下所示

locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=12)
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
locmin = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,0.2,0.4,0.6,0.8,1,2,4,6,8,10 )) 
ax.xaxis.set_minor_locator(locmin)
ax.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

请注意,我将此限制为每十年包含4个小刻度(使用8个刻度也是同样可能的,但在本例中会使轴过度拥挤)。还要注意-,这可能是这里的关键-
subs
参数给出了放置刻度的基数的整数幂的倍数(请参见),它给出了一个20年而不是一年的列表


最后,我们需要对次要记号使用
NullFormatter()
,以避免出现任何记号标签。

在这两种情况下
subs
包含太多条目,主要的只需要
(1.0,)
,次要的应该
subs=np.arange(2,10)*.1
@tac这里的解决方案需要使用
subs
超过二十年。这基本上就是让它工作的整个想法。现在,在2.0.0版和2.0.2版之间似乎发生了一些变化,因此这里介绍的这种解决方法不再有效,而使用
subs
的更直观的方法在十年内也只能再次发挥作用。@BrandonDube I测试了“matplotlib 2.0.2或更高版本的解决方案”使用matplotlib 2.1.2,它工作正常。@ImportantanceOfBeingernest您从pip或Conda获得MPL了吗?我发现Conda的MPL被Qt后端破坏了,所以这可能是continuum的变化。自从换成了pip的MPL后,我就再也没有尝试过了。@BrandonDube这不应该取决于你从哪里得到MPL。如果您有mpl和qt方面的问题,这与此处的问题无关。