0

I have a plot where the y-axis offset is 5.223e1. I'd rather have it just say 52.23. Can this be done?

Unlike this question: prevent scientific notation in matplotlib.pyplot, I am interested in removing the scientific notation from the offset itself, not the entire axis label. I want to keep the offset, just not have it be in scientific notation.

Mastiff
  • 2,101
  • 2
  • 23
  • 38
  • [Seems similar](https://stackoverflow.com/questions/3677368/matplotlib-format-axis-offset-values-to-whole-numbers-or-specific-number). – BigBen Jul 15 '20 at 02:14
  • ^ Though there are many answers there that apparently think the question was about turning the offset off, instead of keeping it and just changing the format. – BigBen Jul 15 '20 at 02:21
  • [Also similar](https://stackoverflow.com/questions/31517156/adjust-exponent-text-after-setting-scientific-limits-on-matplotlib-axis). – BigBen Jul 15 '20 at 02:30

1 Answers1

2

I think this can only be done by subclassing and defining your own formatting function.
Minimal example:

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

class my_ScalarFormatter(ScalarFormatter):
    def format_data(self, value):
        return f'{value:.2f}'

fig,(ax1,ax2) = plt.subplots(ncols=2)
ax2.yaxis.set_major_formatter(my_ScalarFormatter())
ax1.set_ylim(52.23, 52.231)
ax2.set_ylim(52.23, 52.231)
ax1.set_title('Default Formatter')
ax2.set_title('Custom Formatter')
plt.show()

enter image description here

Stef
  • 28,728
  • 2
  • 24
  • 52