2017-07-25 62 views
1

在軸刻度標記在matplotlib,有兩種可能的偏移量的:因子因子和偏移軸標籤

enter image description here

在右下角1e-8將是一個「因素」,1.441249698e1將是一個「轉變」。

這裏有很多答案展示瞭如何操縱兩者

我想只是刪除班次似乎無法弄清楚如何去做。所以matplotlib應該只允許縮放我的軸,但不能移動零點。 有沒有簡單的方法來實現這種行爲?

+0

實際上並不清楚如何在問題的情況下沒有可能轉換的情況下如何標記標記。你能否明確說出你想在軸上顯示哪些數字以及下面哪個數字(作爲因子)? – ImportanceOfBeingErnest

+0

@ImportanceOfBeingErnest的確,對此抱歉。在這個y軸的例子中,我希望它只是在所有的座標軸上顯示1.1,並將1e-2顯示爲係數。或者,只有一個單軸以1.1和1e-2作爲因子(即在兩種情況下都表明小差異的分辨率是不相關的)。 – Wolpertinger

回答

0

您可以修復顯示在軸上的數量級,如this question所示。這個想法是繼承通常的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() 

enter image description here

請記住,這個情節是不準確的,你不應該使用它在任何類型的報表,你的手給其他人的,因爲他們不知道如何interprete它。