簡體   English   中英

如何使用matplotlib設置偏移量

[英]How to set offset with matplotlib

我正在嘗試刪除matplotlib自動放置在圖形上的偏移量。 例如,使用以下代碼:

x=np.array([1., 2., 3.])
y=2.*x*1.e7
MyFig = plt.figure()
MyAx = MyFig.add_subplot(111)
MyAx.plot(x,y)

我得到以下結果(很抱歉,我無法發布圖像):y軸的刻度為2、2.5、3,...,6,在y軸的頂部帶有唯一的“ x10 ^ 7”。

我想從軸的頂部刪除“ x10 ^ 7”,並使其與每個刻度一起出現(2x10 ^ 7、2.5x10 ^ 7等)。 如果我很好地理解了其他主題中的內容,則必須使用use_Offset變量。 因此,我嘗試了以下操作:

MyFormatter = MyAx.axes.yaxis.get_major_formatter()
MyFormatter.useOffset(False)
MyAx.axes.yaxis.set_major_formatter(MyFormatter)

沒有任何成功(結果不變)。 難道我做錯了什么? 我該如何改變這種行為? 還是我要手動設置刻度?

在此先感謝您的幫助!

您可以使用FuncFormatterticker模塊,請你格式化ticklabels:

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

x=np.array([1., 2., 3.])
y=2.*x*1.e7

MyFig = plt.figure()
MyAx = MyFig.add_subplot(111)

def sci_notation(x, pos):
    return "${:.1f} \\times 10^{{6}}$".format(x / 1.e7)

MyFormatter = FuncFormatter(sci_notation)

MyAx.axes.yaxis.set_major_formatter(MyFormatter)

MyAx.plot(x,y)

plt.show()

在此處輸入圖片說明


附帶說明; 出現在軸頂部的“ x10 ^ 7”值不是偏移量,而是科學計數法中使用的一個因素。 可以通過調用MyFormatter.use_scientific(False)禁用此行為。 數字將顯示為小數。

偏移是你必須 (或減)的tickvalues而不是繁殖 ,因為后者是一個規模的值。

供參考,行

MyFormatter.useOffset(False)

應該

MyFormatter.set_useOffset(False)

因為第一個是bool (只能具有值TrueFalse ),這意味着它不能作為方法調用。 后者是用於啟用/禁用偏移的方法。

暫無
暫無

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

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