簡體   English   中英

matplotlib軸標簽的偏移量的因數和偏移

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

在matplotlib中的刻度線標簽上,有兩種可能的偏移量: factorshifts

在此處輸入圖片說明

在右下角1e-8將是一個“因子”,而1.441249698e1將是一個“移位”。

這里有很多答案說明了如何同時操作它們

我只想刪除這些變化,似乎無法弄清楚該如何做。 因此,只應允許matplotlib縮放軸,而不能移動零點。 是否有一種簡單的方法來實現此行為?

您可以固定數量級以顯示在軸上,如本問題所示。 這個想法是將通常的ScalarFormatter子類化,並固定要顯示的數量級。 然后將useOffset設置為False將阻止顯示一些偏移,但仍顯示該因子。

格式"%1.1f"將僅顯示小數點后一位。 最后,使用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()

在此處輸入圖片說明

請記住,這種情節是不准確的,您不應該在向其他人提交的任何報告中使用它,因為他們不知道如何解釋它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM