matplotlib 对数轴:仅显示 10 的幂

Aay*_*ena 5 python plot matplotlib

我有一个双对数图,x 轴范围从 10^9 到 10^12。(这是我第一次发帖,所以我无法发布我的情节的图片)

我想更改 x 和 y 轴,以便仅显示 10 的幂。x 轴上的数字类似于 9、10、11、12。

我用过matplotlib.ticker.LogFormatterExponent(base=10.0, labelOnlyBase=True),但它并不能完全完成工作。有什么建议么?

Imp*_*est 7

LogFormatterExponent(base=10.0, labelOnlyBase=True)按预期工作。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker

x = 10**np.linspace(8.5,12.6)
y = np.sin(x)

fig,ax = plt.subplots()
ax.scatter(x,y)
ax.set_xscale('log')
ax.set_xlabel("Quantity [$10^{x}]$")

logfmt = matplotlib.ticker.LogFormatterExponent(base=10.0, labelOnlyBase=True)
ax.xaxis.set_major_formatter(logfmt)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


DrV*_*DrV 3

在半对数图上以线性比例绘制 X 数据会那么容易吗?

plt.semilogy(np.log10(x), y)
Run Code Online (Sandbox Code Playgroud)

然后你将得到 10 的幂的 X 尺度。

例如:

import numpy as np
import matplotlib.pyplot as plt

# create some data
x = 10**np.linspace(0,9,100)
y = np.sqrt(100 + x)

# plot the figure
fig = plt.figure()
ax = fig.add_subplot(111)
ax.semilogy(np.log10(x), y)

ax.set_xlabel("$10^x$")
ax.set_ylabel("$\sqrt{100 + x}$")
Run Code Online (Sandbox Code Playgroud)

这给出:

在此输入图像描述