简体   繁体   English

matplotlib轴标签的偏移量的因数和偏移

[英]Factors and shifts in offsets for matplotlib axes labels

On the axes tick labels in matplotlib, there are two kinds of possible offsets: factors and shifts : 在matplotlib中的刻度线标签上,有两种可能的偏移量: factorshifts

在此处输入图片说明

In the lower right corner 1e-8 would be a "factor" and 1.441249698e1 would be a "shift". 在右下角1e-8将是一个“因子”,而1.441249698e1将是一个“移位”。

There are a lot of answers here showing how to manipulate both of them : 这里有很多答案说明了如何同时操作它们

I would like to just remove the shifts and can't seem to figure out how to do it. 我只想删除这些变化,似乎无法弄清楚该如何做。 So matplotlib should only be allowed to scale my axis, but not to move the zero point. 因此,只应允许matplotlib缩放轴,而不能移动零点。 Is there a simple way to achieve this behaviour? 是否有一种简单的方法来实现此行为?

You can fix the order of magnitude to show on the axis as shown in this question . 您可以固定数量级以显示在轴上,如本问题所示。 The idea is to subclass the usual ScalarFormatter and fix the order of magnitude to show. 这个想法是将通常的ScalarFormatter子类化,并固定要显示的数量级。 Then setting the useOffset to False will prevent showing some offset, but still shows the factor. 然后将useOffset设置为False将阻止显示一些偏移,但仍显示该因子。

Thwe format "%1.1f" will show only one decimal place. 格式"%1.1f"将仅显示小数点后一位。 Finally using a MaxNLocator allows to set the maximum number of ticks on the axes. 最后,使用MaxNLocator可以设置轴上的最大刻度数。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat="%1.1f", offset=False, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_orderOfMagnitude(self, nothing):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin, vmax):
        self.format = self.fformat
        if self._useMathText:
            self.format = '$%s$' % self.format

x = [0.6e-8+14.41249698, 3.4e-8+14.41249698]
y = [-7.7e-11-1.110934954e-2, -0.8e-11-1.110934954e-2]

fig, ax = plt.subplots()
ax.plot(x,y)

y_formatter = OOMFormatter(-2, "%1.1f")
ax.yaxis.set_major_formatter(y_formatter)
x_formatter = OOMFormatter(1, "%1.1f")
ax.xaxis.set_major_formatter(x_formatter)

ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(2))
ax.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(2))

plt.show()

在此处输入图片说明

Keep in mind that this plot is inaccurate and you shouldn't use it in any kind of report you hand to other people as they wouldn't know how to interprete it. 请记住,这种情节是不准确的,您不应该在向其他人提交的任何报告中使用它,因为他们不知道如何解释它。

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

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