主要网格线与LogLocator的行为不正确?

huangapple go评论55阅读模式
英文:

major gridlines with LogLocator behave incorrectly?

问题

我试图在semilogx图中标记主要的网格线,使它们更粗。然而,下面的代码(SSCCE)只高亮显示每隔一个网格线。

import matplotlib.pyplot as plt
from matplotlib.ticker import (MultipleLocator, LogLocator)

# 配置
xValues = [0.1, 1, 10, 100, 1e3, 10e3, 100e3, 1e6, 10e6, 100e6]
yValues = [-70, -95, -135, -165, -175, -180, -180, -180, -180, -180]

# 绘图
fig = plt.figure(1, figsize=[10, 5], dpi=150)
ax = fig.subplots(1, 1)

plt.semilogx(xValues, yValues)
plt.minorticks_on()

ax.yaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_minor_locator(MultipleLocator(5))
ax.xaxis.set_major_locator(LogLocator(base=10.0))
ax.xaxis.set_minor_locator(LogLocator(base=10.0, subs=(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9), numticks=72))

plt.grid(True, axis='both', which='major', linestyle="-", linewidth=0.8, color=(0.6, 0.6, 0.6))
plt.grid(True, axis='both', which='minor', linestyle="-", linewidth=0.5, color=(0.9, 0.9, 0.9))

plt.tight_layout()
plt.show()

有没有一种好的方法来实现我想要的效果?(在图中,您可以看到x轴仅高亮显示每隔一个十年,而不是每十年一次)。由于轴标签也位于不同的高度,我认为主要网格线可能是错误的原因。

英文:

I am trying to mark the major gridlines of a semilogx plot with thicker lines. However, the code below (SSCCE) only highlights every second gridline.

import matplotlib.pyplot as plt
from matplotlib.ticker import (MultipleLocator, LogLocator)

# configuration
xValues = [0.1, 1, 10, 100, 1e3, 10e3, 100e3, 1e6, 10e6, 100e6]
yValues = [-70, -95,  -135, -165, -175, -180, -180, -180, -180, -180]

# plot 
fig = plt.figure(1, figsize=[10, 5], dpi=150)
ax = fig.subplots(1,1)

plt.semilogx(xValues, yValues)
plt.minorticks_on()

ax.yaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_minor_locator(MultipleLocator(5))
ax.xaxis.set_major_locator(LogLocator(base=10.0))
ax.xaxis.set_minor_locator(LogLocator(base=10.0,subs=(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9),numticks=72))

plt.grid(True, axis='both', which='major', linestyle="-", linewidth=0.8, color=(0.6, 0.6, 0.6))
plt.grid(True, axis='both', which='minor', linestyle="-", linewidth=0.5, color=(0.9, 0.9, 0.9))

plt.tight_layout()
plt.show()

Is there a good way to achieve what I want? (In the plot, you can see that the x axis only hightlights every second decade instead of every decade). Since the axis labels are also on different heights, I believe the reason is that the major gridlines are incorrect?

主要网格线与LogLocator的行为不正确?

答案1

得分: 1

看起来默认的 numticks 失效。

numticks:None 或 int,默认值:None
允许在给定轴上的最大刻度数。默认值为“None”将尝试智能选择,只要此定位器已分配给轴使用~.axis.Axis.get_tick_space,否则将退回到 9。

您可以尝试设置一个较高的数字(100np.inf):

ax.xaxis.set_major_locator(LogLocator(base=10.0, numticks=100))
ax.xaxis.set_minor_locator(LogLocator(base=10.0, numticks=100),
                           subs=(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9))

或者:

ax.xaxis.set_major_locator(LogLocator(base=10.0, numticks=np.inf))
ax.xaxis.set_minor_locator(LogLocator(base=10.0, numticks=np.inf), 
                           subs=(0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9))

主要网格线与LogLocator的行为不正确?

英文:

It looks like the default numticks fails.

> numticks : None or int, default: None
> The maximum number of ticks to allow on a given axis. The default
> of None will try to choose intelligently as long as this
> Locator has already been assigned to an axis using
> ~.axis.Axis.get_tick_space, but otherwise falls back to 9.

You can try to set a high number (100 or np.inf):

ax.xaxis.set_major_locator(LogLocator(base=10.0, numticks=100))
ax.xaxis.set_minor_locator(LogLocator(base=10.0, numticks=100),
                           subs=(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))

Or:

ax.xaxis.set_major_locator(LogLocator(base=10.0, numticks=np.inf))
ax.xaxis.set_minor_locator(LogLocator(base=10.0, numticks=np.inf), 
                           subs=(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9))

主要网格线与LogLocator的行为不正确?

huangapple
  • 本文由 发表于 2023年3月9日 18:45:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定