简体   繁体   中英

How to prevent scientific notation in the axis offset in matplotlib?

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.

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()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM