简体   繁体   English

matplotlib中的轴偏移如何防止科学计数法?

[英]How to prevent scientific notation in the axis offset in matplotlib?

I have a plot where the y-axis offset is 5.223e1 .我有一个 plot ,其中 y 轴偏移为5.223e1 I'd rather have it just say 52.23 .我宁愿让它说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.与这个问题不同: 防止 matplotlib.pyplot 中的科学记数法,我有兴趣从偏移量本身中删除科学记数法,而不是整个轴 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 .我认为这只能通过子类化和定义自己的格式 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()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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