Skip to content

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

Open
@doronbehar

Description

@doronbehar

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...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions