本文介绍了使用对数轴时刻度标签中的小数点不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个带有 log y 轴和整数刻度标签的图,但充其量我得到的小数位数不一致.

I'm trying to create a plot with a log y-axis and integer tick labels, but at best I'm getting inconsistent decimals.

最简单的方法给出科学记数法:

The simplest approach gives scientific notation:

import matplotlib as mpl
import matplotlib.pyplot as plt

vals = [10, 20, 30]

plt.scatter(vals, vals)
ax = plt.gca()
ax.set_yscale('log')

将轴的主要格式器设置为标量仍会产生一些科学符号:

Setting the axis's major formatter to scalars still yields some scientific notation:

ax.yaxis.set_major_formatter(mpl.ticker.ScalarFormatter())

并在其上设置次要格式化程序会删除科学记数法,但会留下不一致的小数:

And setting the minor formatter on top of that removes scientific notation but leaves inconsistent decimals:

ax.yaxis.set_minor_formatter(mpl.ticker.ScalarFormatter())

如何获取所有整数刻度标签?

How can I get all integer tick labels?

推荐答案

例如,您可以使用 StrMethodFormatter 和python的 g 格式:

As an example, you could use a StrMethodFormatter with python's g format:

fmt = mpl.ticker.StrMethodFormatter("{x:g}")
ax.yaxis.set_major_formatter(fmt)
ax.yaxis.set_minor_formatter(fmt)

这篇关于使用对数轴时刻度标签中的小数点不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 09:14