Open
Description
Problem
I want to be able to generate this plot:
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...