Nik*_*hhi 6 python matplotlib formatter ticker
我想在轴上打印的值不是30000或7000000,而是30K或7M.这意味着为x <= 10 ^ 6添加K(kilo)后缀,为x> = 10 ^ 6添加M(兆)后缀.我怎样才能做到这一点?
当前代码段:
ax = pylab.gca()
formatter = matplotlib.ticker.FormatStrFormatter('%.f')
ax.xaxis.set_major_formatter(formatter)
Run Code Online (Sandbox Code Playgroud)
到目前为止,我遇到的最佳代码是:
ax = matplotlib.pyplot.gca()
mkfunc = lambda x, pos: '%1.1fM' % (x * 1e-6) if x >= 1e6 else '%1.1fK' % (x * 1e-3) if x >= 1e3 else '%1.1f' % x
mkformatter = matplotlib.ticker.FuncFormatter(mkfunc)
ax.yaxis.set_major_formatter(mkformatter)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2510 次 |
| 最近记录: |