Skip to content

formatting of values in plot_confusion_matrix #16127

@amueller

Description

@amueller

I'm clearly overlooking something because I'm very confused.
Right now the default for values_format is .2g with seems not ideal as it formats 110 as 1.1+e01:

"{:.2g}".format(110)

'1.1e+02'

Changing the precision doesn't really help.
There's a pretty easy way to fix this, but not with any standard python formatting from what I can see:
https://stackoverflow.com/questions/4626338/python-float-formatting-like-g-but-with-more-digits

Here's a comparison:

from math import log10
for numbers in [1, 12, 123, 1234, 12345, 123456, 1234567, 12345678, 123456789]:
    print("digits: ", len(str(numbers)))
    print(".2g:", "{:.2g}".format(numbers))
    print(".5g:", "{:.5g}".format(numbers))
    print("why:", "{:.2g}".format(numbers) if log10(numbers) > 7 else "{:d}".format(numbers))

The last one is always shorter and more readable than .2g as it uses the scientific notation only when doing so makes the overall representation shorten.

I think having this as the default would be much more user friendly, though I feel this should be a python built-in. Right now the default is None so we could just replace the None behavior by this.

Wdyt?

cc @thomasjpfan

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions