首页 > 解决方案 > Matplotlib secondary_xaxis 无法格式化

问题描述

我正在尝试以对数刻度将“反向”第二个 x 轴转换为 x 轴。secondary_xaxis工作正常,但我不能像往常一样格式化刻度。这是一个错误还是我错过了什么?

这就是我所做的:

import matplotlib.pyplot as plt
import numpy as np

fig, axs = plt.subplots(figsize=(0.6*4,0.6*3))
y = np.random.rand(10)
x = np.linspace(30,200,num=10)

p = plt.plot(x, y, marker='o', markersize=3)
plt.xscale("log")

def m2rho(m):
    return 1 / m * 1000

def rho2m(rho):
    return 1 / rho / 1000

secax = axs.secondary_xaxis('top', functions=(m2rho, rho2m))

from matplotlib.ticker import ScalarFormatter, NullFormatter
for axis in [axs.xaxis, secax.xaxis]:
    axis.set_major_formatter(ScalarFormatter())
    axis.set_minor_formatter(NullFormatter())
axs.set_xticks([50, 100, 200])
secax.set_xticks([20,10,5])

plt.tight_layout()
plt.show()

导致:

顶部的 xtix 是错误的

所以基本上顶部的第二个轴应该以非科学数字显示刻度 20、10 和 5,但事实并非如此。

标签: pythonmatplotlibaxesmultiple-axes

解决方案


推荐阅读