Matplotlib 半对数图:范围大时小刻度线消失 [英] Matplotlib semi-log plot: minor tick marks are gone when range is large
问题描述
在制作半对数图(y 为对数)时,y 轴上的小刻度线(十年中有 8 个)会自动出现,但似乎当轴范围超过 10**10 时,它们就会消失.我尝试了很多方法强迫他们回来,但都无济于事.可能他们会离开大范围以避免过度拥挤,但应该有一个选择吗?
When making a semi-log plot (y is log), the minor tick marks (8 in a decade) on the y axis appear automatically, but it seems that when the axis range exceeds 10**10, they disappear. I tried many ways to force them back in, but to no avail. It might be that they go away for large ranges to avoid overcrowding, but one should have a choice?
推荐答案
matplotlib >= 2.0.2 的解决方案
让我们考虑以下示例
solution for matplotlib >= 2.0.2
Let's consider the following example
由这段代码产生:
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_params(axis='x', which='minor')
)失败.
The minor ticklabels are indeed gone and usual ways to show them (like plt.tick_params(axis='x', which='minor')
) fail.
然后第一步是在轴上显示 10 的所有幂,
The first step would then be to show all powers of 10 on the axis,
locmaj = matplotlib.ticker.LogLocator(base=10,numticks=12)
ax.xaxis.set_major_locator(locmaj)
诀窍是将 numticks
设置为等于或大于刻度数的数字(即在这种情况下为 12 或更高).
where the trick is to set numticks
to a number equal or larger the number of ticks (i.e. 12 or higher in this case).
然后,我们可以添加次要刻度标签
Then, we can add minor ticklabels as
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())
请注意,我将其限制为每十年包含 4 个次要刻度(使用 8 个同样可能,但在此示例中会使轴过度拥挤).另请注意,numticks
再次(非常不直观)等于或大于 12.
Note that I restricted this to include 4 minor ticks per decade (using 8 is equally possible but in this example would overcrowd the axes). Also note that numticks
is again (quite unintuitively) 12 or larger.
最后,我们需要对次要刻度使用 NullFormatter()
,以免它们出现任何刻度标签.
Finally we need to use a NullFormatter()
for the minor ticks, in order not to have any ticklabels appear for them.
以下适用于 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()
次要刻度标签确实消失了,显示它们的常用方法(如 plt.tick_params(axis='x', which='minor')
)失败.
The minor ticklabels are indeed gone and usual ways to show them (like plt.tick_params(axis='x', which='minor')
) fail.
然后第一步是在轴上显示 10 的所有幂,
The first step would then be to show all powers of 10 on the axis,
locmaj = matplotlib.ticker.LogLocator(base=10.0, subs=(0.1,1.0, ))
ax.xaxis.set_major_locator(locmaj)
然后,我们可以添加次要刻度标签
Then, we can add minor ticklabels as
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
参数,它给出了放置刻度的基数的整数幂的倍数(参见 documentation),给出了一个超过二十年的列表,而不是一个.
Note that I restricted this to include 4 minor ticks per decade (using 8 is equally possible but in this example would overcrowd the axes). Also note - and that may be the key here - that the subs
argument, which gives the multiples of integer powers of the base at which to place ticks (see documentation), is given a list ranging over two decades instead of one.
最后,我们需要对次要刻度使用 NullFormatter()
,以免它们出现任何刻度标签.
Finally we need to use a NullFormatter()
for the minor ticks, in order not to have any ticklabels appear for them.
这篇关于Matplotlib 半对数图:范围大时小刻度线消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!