2013-02-27 85 views
8

對於在y軸上的每個刻度標記,我想改變: label -> 2^labelmatplotlib:變化YAXIS刻度標記

我繪製數 - 對數數據(基數爲2),但我想將標籤顯示原始數據值。

我知道我可以得到當前y標籤與 ylabels = plt.getp(plt.gca(), 'yticklabels')

這給了我一個清單: 其中每個<a list of 9 Text yticklabel objects><matplotlib.text.Text object at 0x...>

我看着文本對象的文件在http://matplotlib.org/users/text_props.html 但我仍然不確定在每個文本標籤中更改字符串的正確語法。

一旦我更改標籤,我可以把它們放在使用軸:

plt.setp(plt.gca(), 'yticklabels', ylabels)

+0

你使用類似'雙對數(X,Y,basex的結果= 2,basey = 2 )'?當我這樣做時,標籤已經以2^k的形式出現了。 – 2013-02-27 23:19:31

+0

@WarrenWeckesser不,它實際上是一個箱形圖 – Joe 2013-02-28 04:14:11

回答

8

如果你想這樣做,在一般情況下,你可以使用FuncFormatter(參見: matplotlib axis label formatimshow: labels as any arbitrary function of the image indicesMatplotlib set_major_formatter AttributeError

在你情況下,下面應該工作:

import matplotlib as mpl 
import matplotlib.pyplot as plt 

def mjrFormatter(x, pos): 
    return "$2^{{{0}}}$".format(x) 

def mjrFormatter_no_TeX(x, pos): 
    return "2^{0}".format(x) 

ax = plt.gca() 
ax.yaxis.set_major_formatter(mpl.ticker.FuncFormatter(mjrFormatter)) 
plt.draw() 

的absured {}轉義是新樣式的字符串frommating

+0

運行這個,我收到警告,我不知道如何解釋: – Joe 2013-02-28 21:19:55

+0

/usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1242: UserWarning:findfont:未找到字體系列['STIXSizeOneSym']。回到Bitstream Vera Sans (prop.get_family(),self.defaultFamily [fontext])) – Joe 2013-02-28 21:20:22

+0

/usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1252:UserWarning:findfont:Could not match:family = Bitstream Vera Sans:style = normal:variant = normal:weight = normal:stretch = normal:size = 12。返回/usr/share/fonts/thai-scalable/Waree-Oblique.ttf UserWarning) /usr/lib/python2.7/site-packages/matplotlib/font_manager.py:1242:UserWarning:findfont:Font family [' STIXSizeThreeSym']找不到。回到Bitstream Vera Sans (prop.get_family(),self.defaultFamily [fontext])) – Joe 2013-02-28 21:20:59