如何在轴上放置10种格式的功率,而不是1e-5?

时间:2014-02-24 13:12:59

标签: python matplotlib

如果我们使用科学记数法绘制数据,您通常会在轴上注明1e{+b}。我想提出10^{b}而不是1e{+b}的注释。有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:1)

是的,你可以使用matplotlib.ticker.FuncFormatter,试试这个:

import matplotlib.pyplot as plt 
import matplotlib.ticker as mtick
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)
xs = np.logspace(1,10,10)

ax.plot(xs,range(10))
ax.set_xscale('log')

#the next line is generally wrong, it works just as an example
#ax.xaxis.set_major_formatter(mtick.FuncFormatter(lambda value,pos: ("$10^{%d}$" % pos) ))

#this way change the ticks formats
#import math
#ax.xaxis.set_major_formatter(mtick.FuncFormatter(lambda v,_: ("$10^{%d}$" % math.log(v,10)) ))

#maybe this is what you want
ax.xaxis.set_major_formatter(mtick.ScalarFormatter(useMathText=True))

plt.show()

以上示例将呈现为乳胶,但如果您希望显示“^”符号,请在函数上使用"10^{%d}" % pos