gpt4 book ai didi

python - matplotlib 轴上的千 (K) 和兆 (M) 后缀

转载 作者:太空狗 更新时间:2023-10-30 00:37:15 26 4
gpt4 key购买 nike

我想在轴上打印的值不是 30000 或 7000000,而是 30K 或 7M。这意味着为 x < 10^6 添加 K(千)后缀,为 x >= 10^6 添加 M(兆)后缀。我该怎么做?

当前代码片段:

ax = pylab.gca()
formatter = matplotlib.ticker.FormatStrFormatter('%.f')
ax.xaxis.set_major_formatter(formatter)

最佳答案

到目前为止,我写过的最好的代码是:

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)

关于python - matplotlib 轴上的千 (K) 和兆 (M) 后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6557282/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com