我使用matplot
库在python
中处理绘图。我要生成的数字非常大,所以轴上的刻度也是很大的数字,占用了很多空间。我试图将它们表示为一个异能(例如,我希望有一个100000000,而不是10^8)。我使用了command:ax.ticklabel_format(style='sci', axis='x', scilimits=(0,4))
,但是这个命令只创建了如下内容
有没有其他的解决方案可以让绘图的刻度为:1x10^4,2x10^4,等等,或者在标签刻度的末尾写入值1e4作为10^4?
发布于 2016-04-08 00:36:14
您可以使用matplotlib.ticker
模块,并将ax.xaxis.set_major_formatter
设置为FuncFormatter
。
例如:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
plt.rcParams['text.usetex'] = True
fig,ax = plt.subplots(1)
x = y = np.arange(0,1.1e4,1e3)
ax.plot(x,y)
def myticks(x,pos):
if x == 0: return "$0$"
exponent = int(np.log10(x))
coeff = x/10**exponent
return r"${:2.0f} \times 10^{{ {:2d} }}$".format(coeff,exponent)
ax.xaxis.set_major_formatter(ticker.FuncFormatter(myticks))
plt.show()
注意,这使用LaTeX
格式化(text.usetex = True
)在刻度标签中呈现指数。还要注意区分LaTeX
大括号和python格式字符串大括号所需的双花括号。
发布于 2016-04-08 00:37:25
可能有更好的解决方案,但是如果您知道每个xtick的值,也可以手动命名它们。下面是一个示例:http://matplotlib.org/examples/ticks_and_spines/ticklabels_demo_rotation.html
https://stackoverflow.com/questions/36480077
复制相似问题