Skip to content

Log-scale ticker fails at 1e-323 #18800

Closed
@pmli

Description

@pmli

Bug report

Bug summary

It seems that log-scale plotting fails whenever 1e-323 is used as a tick.

Code for reproduction

import matplotlib.pyplot as plt
plt.semilogy([1, 2], [1, 2e-323])
plt.show()

or

import matplotlib.pyplot as plt
plt.semilogy([1, 2, 3], [1, 2e-323, 0])
plt.show()

2e-323 can be replaced with 3e-323, 4e-323, ..., 10e-323 (replacing it with 1e-323 or 11e-323 makes it work).

Actual outcome

# long traceback
  File "/some_path/matplotlib/lib/matplotlib/ticker.py", line 1157, in __call__
    coeff = round(x / b ** exponent)
OverflowError: cannot convert float infinity to integer

Expected outcome

A plot that is similar to when 2e-323 is replaced with 1e-323 or 11e-323.

Debugger result

The debugger shows that the issue is in LogFormatter.__call__ when x == 1e-323:

def __call__(self, x, pos=None):
    # docstring inherited
    if x == 0.0:  # Symlog
        return '0'

    x = abs(x)                                             # x = 1e-323
    b = self._base                                         # b = 10.0
    # only label the decades
    fx = math.log(x) / math.log(b)                         # fx = -323.0051853474518
    is_x_decade = is_close_to_int(fx)                      # is_x_decade = False
    exponent = round(fx) if is_x_decade else np.floor(fx)  # exponent = -324.0
    coeff = round(x / b ** exponent)                       # b ** exponent == 0.0

Matplotlib version

  • Operating system: Linux
  • Matplotlib version: current master branch
  • Matplotlib backend: TkAgg
  • Python version: 3.7.8

Installed from source (using pip). The same issue appears also in 3.3.2.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions