Visitar URL original
Add 'binary' keyword argument to EngFormatter by czarnota · Pull Request #17315 · matplotlib/matplotlib · GitHub
The Wayback Machine - https://web.archive.org/web/20260106120502/https://github.com/matplotlib/matplotlib/pull/17315
Skip to content

Conversation

@czarnota
Copy link

@czarnota czarnota commented May 3, 2020

When plotting number of bits or bytes, the base for the unit prefix
should not be 1000, but 1024. If the 'binary' argument is set to True,
the EngFormatter will use 1024 as a base, otherwise it will use 1000.

Use case:
When plotting network throughput values I needed EngFormatter to use 1024 as a base.
I am sharing the code maybe it will be useful

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

When plotting number of bits or bytes, the base for the unit prefix
should not be 1000, but 1024. If the 'binary' argument is set to True,
the EngFormatter will use 1024 as a base, otherwise it will use 1000.
@afvincent
Copy link
Contributor

Thanks @czarnota for the PR! I only had time for a quick overlook at the code, but I find the base**base_pow separation to be a pretty neat idea to handle the different cases.

I would slightly disagree with the statement “When plotting number of bits or bytes, the base for the unit prefix should not be 1000, but 1024”, as it may actually not be the case in some fields (e.g. HDD storage capacity or data rate values actually usually use decimal prefixes). The Wikipedia entry dedicated to binary prefixes give a reasonable overview on this topic.

As you pointed out, there may however be a need for being able to choose between decimal and binary prefixes, as well as between base 10 and and base 2 (or one could argue between base 1000 and base 1024 in the peculiar case of engineering notation). Thus I wonder if one should not rather use two different arguments to handle all (possibly relevant) cases:

  1. decimal prefixes (k, M, G, etc.) in base 10³
  2. decimal prefixes (k,M, G, etc.) in base 2¹⁰
  3. binary prefixes (Ki, Mi, Gi, etc.) in base 2¹⁰

I am not sure that anyone might even want to use the last combination (but I might be wrong), which would be
4. binary prefixes (Ki, Mi, Gi, etc.) in base 10³

One may consider something like introducing the following named arguments:

  • prefix in {'decimal' (default), 'binary'} (or in {'SI' (default), 'IEC'} but this may be less meaningful names to most users…)
  • base in {10 (default), 2} (or in {1000 (default), 1024} depending on what the consensus would be.)

instead of a single boolean argument binary that covers only cases 1. and 2.

Nevertheless, there would still be an issue with binary prefixes: as far as I can tell, binary prefixes only cover prefixes for values > 1 (e.g. Ki, Mi, Gi, etc.). I have no idea how one should treat the prefixes for values < 1.

In a nutshell, I like the idea of being able to choose between the decimal and the binary systems but I would also like other people's inputs on what kind of new arguments the public API of EngFormatter should accept to handle this.

@timhoffm
Copy link
Member

timhoffm commented May 4, 2020

I did not have the time to check this in detail. However, it feels like an ad-hoc addition to EngFormatter. Also, technically the prefix for 1024 should be 'Ki', not 'k'. It's probably better to make a dedicated BinaryFormatter (or BinaryEngFormatter) instead.

@tacaswell tacaswell added this to the v3.4.0 milestone May 4, 2020
@czarnota
Copy link
Author

czarnota commented May 7, 2020 •

Thank you @afvincent for a very comprehensive review

I think that this case:

  1. binary prefixes (Ki, Mi, Gi, etc.) in base 10³

Is in my opinion not correct. Ki, Mi, Gi are by definition prefixes for base 2¹⁰

So all new use cases are for 2¹⁰, which could be a reason to create a separate class for this as @timhoffm suggested.

I think that the BinaryEngFormatter is a good name. The BinaryFormatter might suggest that the numbers will be converted to binary. Another shorter name for the formatter could be CsFormatter (Cs - Computer Science)

Also for:

as far as I can tell, binary prefixes only cover prefixes for values > 1 (e.g. Ki, Mi, Gi, etc.). I have no idea how one should treat the prefixes for values < 1.

I think there are 3 options:
a) Use no prefix
b) Fall back to decimal prefixes
c) Add option to choose between a) and b) (this would be another reason for a separate class)

@QuLogic QuLogic modified the milestones: v3.4.0, v3.5.0 Jan 21, 2021
jklymak
jklymak previously approved these changes May 8, 2021
Copy link
Member

@jklymak jklymak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This actually seems fine to me. Its relatively self-contained and some folks may find it helpful.

@jklymak
Copy link
Member

jklymak commented May 11, 2021

@afvincent did you have time to weigh in again on this one?

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should go into a separate BinaryEngFormatter class, possibly marking it experimental. This allows us all the freedom of additional arguments. Having additional arguments on EngFormatter that would only apply to the binary=True case would be poor API.

@jklymak
Copy link
Member

jklymak commented May 14, 2021

I agree with @timhoffm. OTOH we sat on this for a long time. @czarnota hopefully you will come back to look at this. And please do ping us for reviews if this is something you would strongly like in the library. Thanks!

@jklymak jklymak dismissed their stale review May 14, 2021 14:49

Agree, should be its own subclass

@jklymak jklymak marked this pull request as draft May 14, 2021 14:50
@QuLogic QuLogic modified the milestones: v3.5.0, v3.6.0 Aug 23, 2021
@tacaswell tacaswell deleted the branch matplotlib:master October 20, 2021 19:53
@tacaswell tacaswell closed this Oct 20, 2021
@QuLogic QuLogic removed this from the v3.6.0 milestone Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants