[英]matplotlib: format axis offset-values to whole numbers or specific number
我有一個 matplotlib 圖,我正在繪制始終稱為納秒 (1e-9) 的數據。 在 y 軸上,如果我有幾十納秒的數據,即。 在 44e-9 中,軸上的值顯示為 4.4,偏移量為 +1e-8。 無論如何強制軸顯示 44 和 +1e-9 偏移?
我的 x 軸也是如此,其中軸顯示 +5.54478e4,我寧願它顯示 +55447 的偏移量(整數,沒有小數 - 這里的值以天為單位)。
我嘗試了一些這樣的事情:
p = axes.plot(x,y)
p.ticklabel_format(style='plain')
對於 x 軸,但這不起作用,盡管我可能使用它不正確或誤解了文檔中的某些內容,有人可以指出我正確的方向嗎?
謝謝,喬納森
我嘗試用格式化程序做一些事情,但還沒有找到任何解決方案......:
myyfmt = ScalarFormatter(useOffset=True)
myyfmt._set_offset(1e9)
axes.get_yaxis().set_major_formatter(myyfmt)
和
myxfmt = ScalarFormatter(useOffset=True)
myxfmt.set_portlimits((-9,5))
axes.get_xaxis().set_major_formatter(myxfmt)
附帶說明一下,我實際上對“偏移量”對象實際所在的位置感到困惑……它是主要/次要刻度的一部分嗎?
我遇到了完全相同的問題,這些行解決了問題:
from matplotlib.ticker import ScalarFormatter
y_formatter = ScalarFormatter(useOffset=False)
ax.yaxis.set_major_formatter(y_formatter)
一個更簡單的解決方案是簡單地自定義刻度標簽。 拿這個例子:
from pylab import *
# Generate some random data...
x = linspace(55478, 55486, 100)
y = random(100) - 0.5
y = cumsum(y)
y -= y.min()
y *= 1e-8
# plot
plot(x,y)
# xticks
locs,labels = xticks()
xticks(locs, map(lambda x: "%g" % x, locs))
# ytikcs
locs,labels = yticks()
yticks(locs, map(lambda x: "%.1f" % x, locs*1e9))
ylabel('microseconds (1E-9)')
show()
注意在 y 軸的情況下,我如何將值乘以1e9
然后在 y 標簽中提到該常量
編輯
另一種選擇是通過手動將其文本添加到繪圖頂部來偽造指數乘數:
locs,labels = yticks()
yticks(locs, map(lambda x: "%.1f" % x, locs*1e9))
text(0.0, 1.01, '1e-9', fontsize=10, transform = gca().transAxes)
編輯2
您也可以以相同的方式格式化 x 軸偏移值:
locs,labels = xticks()
xticks(locs, map(lambda x: "%g" % x, locs-min(locs)))
text(0.92, -0.07, "+%g" % min(locs), fontsize=10, transform = gca().transAxes)
你必須ScalarFormatter
來做你需要的...... _set_offset
只是添加一個常量,你想設置ScalarFormatter.orderOfMagnitude
。 不幸的是,手動設置orderOfMagnitude
不會做任何事情,因為當調用ScalarFormatter
實例來格式化軸刻度標簽時,它會被重置。 它不應該這么復雜,但我找不到一種更簡單的方法來做你想做的事......這是一個例子:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter, FormatStrFormatter
class FixedOrderFormatter(ScalarFormatter):
"""Formats axis ticks using scientific notation with a constant order of
magnitude"""
def __init__(self, order_of_mag=0, useOffset=True, useMathText=False):
self._order_of_mag = order_of_mag
ScalarFormatter.__init__(self, useOffset=useOffset,
useMathText=useMathText)
def _set_orderOfMagnitude(self, range):
"""Over-riding this to avoid having orderOfMagnitude reset elsewhere"""
self.orderOfMagnitude = self._order_of_mag
# Generate some random data...
x = np.linspace(55478, 55486, 100)
y = np.random.random(100) - 0.5
y = np.cumsum(y)
y -= y.min()
y *= 1e-8
# Plot the data...
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'b-')
# Force the y-axis ticks to use 1e-9 as a base exponent
ax.yaxis.set_major_formatter(FixedOrderFormatter(-9))
# Make the x-axis ticks formatted to 0 decimal places
ax.xaxis.set_major_formatter(FormatStrFormatter('%0.0f'))
plt.show()
這產生了類似的東西:
而默認格式如下所示:
希望那有所幫助!
編輯:對於它的價值,我也不知道偏移標簽所在的位置......手動設置它會稍微容易一些,但我無法弄清楚如何這樣做......我明白了必須有比所有這些更簡單的方法。 不過,它有效!
類似於 Amro 的回答,您可以使用 FuncFormatter
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
# Generate some random data...
x = np.linspace(55478, 55486, 100)
y = np.random.random(100) - 0.5
y = np.cumsum(y)
y -= y.min()
y *= 1e-8
# Plot the data...
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y, 'b-')
# Force the y-axis ticks to use 1e-9 as a base exponent
ax.yaxis.set_major_formatter(FuncFormatter(lambda x, pos: ('%.1f')%(x*1e9)))
ax.set_ylabel('microseconds (1E-9)')
# Make the x-axis ticks formatted to 0 decimal places
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: '%.0f'%x))
plt.show()
添加了set_scientific(False)
后,Gonzalo 的解決方案開始為我工作:
ax=gca()
fmt=matplotlib.ticker.ScalarFormatter(useOffset=False)
fmt.set_scientific(False)
ax.xaxis.set_major_formatter(fmt)
正如評論和本答案中所指出的,可以通過執行以下操作來全局關閉偏移量:
matplotlib.rcParams['axes.formatter.useoffset'] = False
我認為更優雅的方法是使用股票代碼格式化程序。 以下是 xaxis 和 yaxis 的示例:
from pylab import *
from matplotlib.ticker import MultipleLocator, FormatStrFormatter
majorLocator = MultipleLocator(20)
xFormatter = FormatStrFormatter('%d')
yFormatter = FormatStrFormatter('%.2f')
minorLocator = MultipleLocator(5)
t = arange(0.0, 100.0, 0.1)
s = sin(0.1*pi*t)*exp(-t*0.01)
ax = subplot(111)
plot(t,s)
ax.xaxis.set_major_locator(majorLocator)
ax.xaxis.set_major_formatter(xFormatter)
ax.yaxis.set_major_formatter(yFormatter)
#for the minor ticks, use no labels; default NullFormatter
ax.xaxis.set_minor_locator(minorLocator)
對於第二部分,無需再次手動重置所有刻度,這是我的解決方案:
class CustomScalarFormatter(ScalarFormatter):
def format_data(self, value):
if self._useLocale:
s = locale.format_string('%1.2g', (value,))
else:
s = '%1.2g' % value
s = self._formatSciNotation(s)
return self.fix_minus(s)
xmajorformatter = CustomScalarFormatter() # default useOffset=True
axes.get_xaxis().set_major_formatter(xmajorformatter)
顯然,您可以將格式字符串設置為您想要的任何內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.