您当前的 x 轴只有 10 个值,在 0 到 10 之间的线性空间中等距。您需要更多值,并且在对数空间中等距。例如,在 10 -3和 10 1np.logspace(-3, 1, 100)
之间创建 100 个 x 值。
如果您更改基数plt.loglog(x, y, basex=np.e, basey=np.e)
,则轴将以 的幂显示,并带有刻度e
。如果您不更改底数,则默认使用熟悉的 10 次方。请注意,更改底数不会更改转换,只会更改刻度的位置。
import numpy as np
import matplotlib.pyplot as plt
x = np.logspace(-3, 1, 100)
y = x ** 2 / (np.exp(x) + 1)
plt.loglog(x, y)
plt.autoscale(enable=True, axis='x', tight=True) # optionally set a tight x-axis
plt.show()

PS:为了避免科学记数法,这篇文章建议:
import matplotlib.ticker as ticker
plt.gca().yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: f'{y:g}'))
plt.gca().xaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: f'{y:g}'))
或者,强制十进制表示法也用于更多小数:
plt.gca().yaxis.set_major_formatter(
ticker.FuncFormatter(lambda y, _: ('{{:.{:1d}f}}'.format(int(np.maximum(-np.log10(y),0)))).format(y)))