2013-02-27 89 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