Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ENH]: ticker.EngFormatter: allow offset #28463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
doronbehar opened this issue Jun 25, 2024 · 4 comments
Open

[ENH]: ticker.EngFormatter: allow offset #28463

doronbehar opened this issue Jun 25, 2024 · 4 comments
Milestone

Comments

@doronbehar
Copy link
Contributor

Problem

I want to be able to generate this plot:

Screenshot from 2024-06-26 01-11-31

I managed to create this plot using the following code:

from itertools import count
from math import copysign

# https://pypi.org/project/prefixed/
from prefixed import Float

from matplotlib.ticker import ScalarFormatter

class FrequencyFormatter(ScalarFormatter):
    def format_data(self, value):
        # Add the Hz suffix
        return '{:H}Hz'.format(Float(value))
    def _set_order_of_magnitude(self):
        super()._set_order_of_magnitude()
        c = abs(self.orderOfMagnitude)
        # Search 0, -3, 3, -6, 6, -9, 9 etc until the best scientifically
        # prefixed oom is found, and then set self.orderOfMagnitude to it
        for sciOom in count(3,3):
            if c < sciOom:
                self.orderOfMagnitude = copysign(sciOom, self.orderOfMagnitude)
                break

# fig,ax...

ax.yaxis.set_major_formatter(FrequencyFormatter(
    # I'm not necessarily interested in this, but without this,
    # ScalarFormatter.get_offset doesn't use my own .format_data function to
    # format also the offset.
    useMathText=True
))

It is not ideal because it relies both upon modifying (the private) _set_order_of_magnitude method and the external library prefixed, which implements something already implemented in the EngFormatter.

I'm surprised that EngFormatter doesn't handle offsets so smartly like ScalarFormatter...

Where do you think this kind of enhancement would fit most? In EngFormatter with an additional __init__ argument? Or in a new formatter?

Proposed solution

Sort of proposed above...

@tacaswell
Copy link
Member

I think mirroring the ScalarFormatter offset functionality to EngFormatter is a reasonable thing to do.

I am pretty 👎🏻 on picking up a dependency though.

@doronbehar
Copy link
Contributor Author

I am pretty 👎🏻 on picking up a dependency though.

Yea sure, I was thinking about that the same, EngFormatter already includes most of the functionality of prefixed.Float, it's just that it wasn't accessible to me when I inherited functionality from ScalarFormatter, and I wanted a quick solution.

I think mirroring the ScalarFormatter offset functionality to EngFormatter is a reasonable thing to do.

OK I agree with that, in terms of the UX/UI of the library, I'd imagine this kind of usage:

ax.yaxis.set_major_formatter(EngFormatter(
	# Perhaps this should be the default value?
    useOffset=True
))

The dilemma I'm facing though when I'm thinking of implementing this feature, is the fact that most of the code that computes constantly the data's orderOfMagnitude and the offset, is located now in "private" methods of ScalarFormatter, and EngFormatter's parent class is Formatter which doesn't include that functionality.

Hence I'm not sure in to which class to put the functions that compute the order of magnitude and the offset. Perhaps I should make EngFormatter a child class of ScalarFormatter? what do you think?

@tacaswell
Copy link
Member

Rather than go down the complexity of OO inheritance choices, I suggest we pull the re-usable code out into def _top_level_function(...): ... in the module and have each of the classes call them (passing in what ever state they need to pass in).

doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
@doronbehar
Copy link
Contributor Author

It was pretty hard to implement this without subclassing ScalarFormatter, because the decision what offset to use is heavily reliant upon many variables which I don't want to pass to external static functions. I hope you'll like the implementation at #28495 .

doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 1, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 3, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
@tacaswell tacaswell added this to the v3.10.0 milestone Jul 3, 2024
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 4, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 5, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 5, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 9, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Jul 9, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Oct 12, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Oct 17, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
doronbehar added a commit to doronbehar/matplotlib that referenced this issue Oct 23, 2024
Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves matplotlib#28463.
ksunden pushed a commit that referenced this issue Oct 30, 2024
* ticker.ScalarFormatter: allow changing usetex like in EngFormatter

* ticker.EngFormatter: base upon ScalarFormatter

Allows us to use many order of magnitude and offset related routines
from ScalarFormatter, and removes a bit usetex related duplicated code.

Solves #28463.

* Fix small documentation issues from QuLogic's review

* Small comment fixups from review

* test_ticker.py: small cleanups after review

* ticker.py: small cleanups after review

* ticker.ScalarFormatter: Fix type hints & document new attributes

* engformatter_offset.rst: fix title

* engformatter related small fixes

* test_engformatter_offset_oom: parametrize center & noise directly
@tacaswell tacaswell modified the milestones: v3.10.0, v3.11.0 Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants