python - Matplotlib:对于浮点来说值太小的对数刻度

标签 python matplotlib plot floating-point sympy

我想用 matplotlib 绘制一个具有非常小的值的图,以对数刻度显示。直到数字变得太小并且不适合 float 之前,这种方法都可以正常工作。

我使用 SymPy 任意精度 float 表示值,但这些值显然在 matplotlib 内部转换为 Python 或 NumPy 机器 float 。

例如

>>> import sympy
>>> import matplotlib.pyplot as plt
>>> plt.plot([0, 1, 2], [sympy.Float('1e-20'), sympy.Float('1e-100'), sympy.Float('1e-700')])
[<matplotlib.lines.Line2D object at 0x11ac0c208>]
>>> plt.yscale('log')

产生plot

它应该显示的是 10^-700 处的第三个值(不是负无穷大)。

现在我对让 matplotlib 在内部使用 SymPy Floats 的希望很小(如果可能的话,请告诉我)。我想做的是向 matplotlib 提供该值的对数,我可以自己计算得很好,但仍然以对数刻度在 y 轴上显示该值的指数。

最佳答案

您可以进行对数计算,然后绘制数据,如果您想要相同的刻度标签,您可以使用FuncFormatter:

import sympy
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

x = [0, 1, 2]
y = [sympy.Float('1e-20'), sympy.Float('1e-100'), sympy.Float('1e-700')]

def log_formatter(x, pos):
    return "$10^{{{:d}}}$".format(int(x))

formatter = FuncFormatter(log_formatter)

fig, ax = plt.subplots()

y2 = list(map(lambda x:sympy.log(x, 10), y))
ax.plot(x, y2)
ax.yaxis.set_major_formatter(formatter)
ax.grid();

enter image description here

编辑

要添加对数刻度小刻度,您可以创建一个定位器:

import sympy
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter, Locator

x = [0, 1, 2]
y = [sympy.Float('1e-20'), sympy.Float('1e-100'), sympy.Float('1e-700')]

def log_formatter(x, pos):
    return "$10^{{{:d}}}$".format(int(x))

class LogMinorLocator(Locator):
    def __call__(self):
        majorlocs = self.axis.get_majorticklocs()
        step = majorlocs[1] - majorlocs[0]
        res = majorlocs[:, None] + np.log10(np.linspace(1, 0.1, 10)) * step
        return res.ravel()

formatter = FuncFormatter(log_formatter)

fig, ax = plt.subplots(figsize=(12, 8))
y2 = list(map(lambda x:sympy.log(x, 10), y))
ax.plot(x, y2)
ax.minorticks_on()
ax.yaxis.set_major_formatter(formatter)
ax.yaxis.set_minor_locator(LogMinorLocator())
ax.grid();

这是输出:

enter image description here

关于python - Matplotlib:对于浮点来说值太小的对数刻度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44211066/

相关文章:

matplotlib 中的动画与分散和使用 set_offsets : Autoscale of figure is not working

python - 如何在 SHAP 汇总图上绘制特定特征?

python - 使用 pytest.main() 调用 pytest 时等效于 `-s`

Python Pylab 子图错误?

python - 定位彩条 - Matplotlib

python - PyQt 中嵌入的 Matplotlib 交互式图形

python - 在 Matplotlib 中删除楔形极坐标图周围的空间

python - 如何使用 python 中的门和键从这个迷宫算法中获取路径?

python - 正则表达式:捕获可选的开始或结束组/子组

Python/numpy棘手的切片问题