如何使用 matplotlib 以指数方式缩放 Y 轴 [英] How can I exponentially scale the Y axis with matplotlib
问题描述
我正在尝试创建一个带有指数(?)Y 轴的 matplotlib 图,就像我在下面模拟的那个假图一样.对于我的数据,我想在它们接近最大Y值时散布这些值.而且我想在Y接近零时压缩这些值.
所有正常的日志"示例都做相反的事情:它们在远离零时压缩值.这当然是日志"所做的.我该如何创建指数缩放?
从 matplotlib 3.1 开始,您可以通过
轻松定义任何自定义比例ax.set_yscale('function', functions=(forward, inverse))
另请参见
I'm trying to create a matplotlib plot with an exponential(?) Y axis like the fake one I've mocked up below. For my data I want to spread the values out as they approach the max Y value. And I'd like to compress the values as Y gets close to zero.
All the normal 'log' examples do the opposite: they compress values as they get away from zero. Which is what 'log' does of course. How can I create an exponential(?) scaling instead?
From matplotlib 3.1 onwards you can define any custom scale easily via
ax.set_yscale('function', functions=(forward, inverse))
Also see https://matplotlib.org/3.1.1/gallery/scales/scales.html
In this case, e.g.:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1, 40, 100)
y = np.linspace(1, 4, 100)
fig, ax = plt.subplots()
ax.plot(x, y)
exp = lambda x: 10**(x)
log = lambda x: np.log(x)
# Set y scale to exponential
ax.set_yscale('function', functions=(exp, log))
ax.set(xlim=(1,40), ylim=(1,4))
ax.set_yticks([1, 3, 3.5, 3.75, 4.0])
plt.show()
这篇关于如何使用 matplotlib 以指数方式缩放 Y 轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!