如何格式化双 x 轴和 y 轴上的小刻度,这两个轴都是对数缩放的?

How to format minor ticks on twin x- and y- axes, both of which are logarithmically scaled?

我想要显示两种不同比例的数据。 Matplotlib 文档(例如 this one)包含一些将 x 轴或 y 轴(不是两者)成对的示例,并且这些示例中的 none 展示了如何操作 x 轴和 y 轴的小刻度当对数缩放时。

这是我的问题:如何格式化双 x 轴和双 y 轴上的小刻度,这两个轴都是对数缩放的?在下面的示例中,传统的 xy 轴是线性缩放的(由下标 i 表示),而 alternate/mirror/twin xy 轴是对数缩放的(由下标 j 表示)。函数 twinboth 是从 similar question. Also, I am using LogLocator instead of AutoMinorLocator, as suggested in .

借用的
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

## linear scale parameters
m, b = 0.5, np.pi
xi = np.arange(1, 11, 1).astype(int)
noisei = np.random.uniform(low=-0.5, high=0.5, size=xi.size)
yi = m * xi + b + noisei

## log scale parameters
xj = 2 ** xi
noisej = 2 ** noisei
yj = 2 ** yi

def twinboth(ax):
    # Alternately, we could do `newax = ax._make_twin_axes(frameon=False)`
    newax = ax.figure.add_subplot(ax.get_subplotspec(), frameon=False)
    newax.xaxis.set(label_position='top')
    newax.yaxis.set(label_position='right', offset_position='right')
    newax.yaxis.get_label().set_rotation(-90) # Optional...
    newax.yaxis.tick_right()
    newax.xaxis.tick_top()
    return newax

## initialize figure and linear axes
fig, axes = plt.subplots(nrows=2, ncols=2)
for axi in axes.ravel():
    ## plot linear
    axi.scatter(xi, yi, color='r', alpha=0.5, label='linear')
    axi.grid(color='k', alpha=0.3, linestyle=':')
    ## get twinned logarithmic axes
    axj = twinboth(axi)
    axj.set_xscale('log', basex=2)
    axj.set_yscale('log', basey=2)
    ## whole number instead of sci-notation
    axj.xaxis.set_major_formatter(ticker.ScalarFormatter())
    axj.yaxis.set_major_formatter(ticker.ScalarFormatter())
    ## attempt log-scaled minor ticks on twin axes
    axj.xaxis.set_minor_locator(ticker.LogLocator(base=2))
    axj.yaxis.set_minor_locator(ticker.LogLocator(base=2))
    axj.xaxis.set_minor_formatter(ticker.ScalarFormatter())
    axj.yaxis.set_minor_formatter(ticker.ScalarFormatter())
    ## plot log-scale on twin axes
    axj.scatter(xj, yj, color='b', alpha=0.5, label='log')

## get legend handles/labels from last iteration of for-loop above (avoid duplicates)
handlesi, labelsi = axi.get_legend_handles_labels()
handlesj, labelsj = axj.get_legend_handles_labels()
handles = handlesi + handlesj
labels = labelsi + labelsj
fig.subplots_adjust(bottom=0.2, wspace=0.3, hspace=0.3)
fig.legend(handles=handles, labels=labels, loc='lower center', mode='expand', ncol=2)

plt.show()
plt.close(fig)

此代码生成下图,其中没有小刻度。

你的次要刻度被你的主要刻度隐藏了。

axj.set_xscale('log', basex=2)
axj.set_yscale('log', basey=2)

将 "twin axes" 的刻度设置为对数,因此使用对数主刻度 (4, 16, 64, 256, 1024)。

axj.xaxis.set_minor_locator(ticker.LogLocator(base=2))
axj.yaxis.set_minor_locator(ticker.LogLocator(base=2))

这个,在对数位置添加小刻度,这将是 4, 16, 64, 256, 1024 ;和你的专业一样。

你想要做的是:

axi.xaxis.set_minor_locator(ticker.LogLocator(base=2))
axi.yaxis.set_minor_locator(ticker.LogLocator(base=2))

向您的线性轴添加次要对数刻度,或者:

axj.xaxis.set_minor_locator(ticker.LinearLocator())
axj.yaxis.set_minor_locator(ticker.LinearLocator())

在对数轴上添加次要的线性刻度。