设置所有对数对数子图中的次要刻度线

2024-09-29 17:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个数字,在对数刻度上有两个子图。我想绘制小滴答声以及。即使我应用了不同的解决方案,从堆栈溢出,我的数字看起来不像我想要的

我修改的解决方案之一来自ImportanceOfBeingErnest,代码如下:

fig, ((ax1, ax2)) = plt.subplots(1, 2, figsize=(8, 5), sharey=True)

# First plot
ax1.loglog(PLOT1['X'], PLOT1['Y'], 'o',
         markerfacecolor='red', markeredgecolor='red', markeredgewidth=1,
         markersize=1.5, alpha=0.2)

ax1.set(xlim=(1e-4, 1e4), ylim=(1e-8, 1e2))
ax1.set_xscale("log"); ax1.set_yscale("log")
ax1.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
ax1.yaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25) 
ax1.xaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax1.xaxis.set_minor_locator(locmin)
ax1.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25) 
ax1.yaxis.set_major_locator(locmaj)
locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax1.yaxis.set_minor_locator(locmin)
ax1.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

ax1.set_xlabel('X values', fontsize=10, fontweight='bold')
ax1.set_ylabel('Y values', fontsize=10, fontweight='bold')

# Plot 2
ax2.loglog(PLOT2['X'], PLOT2['Y'], 'o',
         markerfacecolor='blue', markeredgecolor='blue', markeredgewidth=1,
         markersize=1.5, alpha=0.2)

ax2.set(xlim=(1e-4, 1e4), ylim=(1e-8, 1e2))
ax2.xaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))
ax2.yaxis.set_major_locator(matplotlib.ticker.LogLocator(base=10.0, numticks=25))

locmaj = matplotlib.ticker.LogLocator(base=10,numticks=25) 
ax2.xaxis.set_major_locator(locmaj)
ax2.yaxis.set_major_locator(locmaj)

locmin = matplotlib.ticker.LogLocator(base=10.0,subs=(0.2,0.4,0.6,0.8),numticks=25)
ax2.xaxis.set_minor_locator(locmin)
ax2.yaxis.set_minor_locator(locmin)

ax2.xaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())
ax2.yaxis.set_minor_formatter(matplotlib.ticker.NullFormatter())

ax2.set_xlabel('X values', fontsize=10, fontweight='bold')
ax2.set_ylabel('Y values', fontsize=10, fontweight='bold')
ax2.minorticks_on()

plt.show()

我得到的情节如下。如您所见,次记号只出现在ax1的x轴上

Resulting plot with missing minor ticks

如何设置子图和轴(x和y)中的副刻度

非常感谢


Tags: basematplotlibtickersetminorlocatormajorax1

热门问题