1

我有一个带有两个对数刻度的子图的图。我也想绘制次要刻度。即使我从 Stack Overflow 应用了不同的解决方案,我的身材看起来并不像我想要的那样。

我修改的解决方案之一来自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()

我得到的情节如下。如您所见,次要刻度仅出现在 x 轴上ax1

缺少次要刻度的结果图

如何在两个子图中和两个轴(x 和 y)中设置次要刻度?

太感谢了。

4

0 回答 0