【问题标题】:Matplotlib: setting the Y-axis to none scientific notation AFTER changing the Y-axis to log scaleMatplotlib:将 Y 轴更改为对数刻度后,将 Y 轴设置为非科学记数法
【发布时间】:2020-06-26 08:55:38
【问题描述】:

我有一个简单的图表,其中对象axs 用于轴。在我将 Y 轴更改为对数刻度之前,格式只是常规数字。

在我将 Y 轴更改为对数刻度后,使用:axs.set_yscale('log') ... 然后尝试使用

将数字改回来

axs.set_yticklabels(['{:,}'.format(int(x)) for x in axs.get_yticks().tolist()])

它不起作用...标签仍然是科学记数法。

我只想要正常的数字。

我正在使用:

fig = plt.figure(figsize=(30, 15))
axs = fig.add_subplot(1, 1, 1)
axs.plot()

【问题讨论】:

  • herefrom matplotlib.ticker import ScalarFormatter; axs.yaxis.set_major_formatter(ScalarFormatter())
  • 谢谢。我试过了。我得到了同样的结果。
  • 是的,。我稍后会发布一个

标签: python matplotlib


【解决方案1】:

如上所述,here 您可以设置 ScalarFormatter 以省略科学记数法。还需要.set_scientific(False) 来抑制大数的科学记数法。

如果你正在处理负面权力,你可能需要axs.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, _: '{:g}'.format(y)))

from matplotlib import pyplot as plt
from matplotlib.ticker import ScalarFormatter

fig = plt.figure(figsize=(30, 15))
axs = fig.add_subplot(1, 1, 1)
axs.plot()
axs.set_ylim(100000, 100000000)
axs.set_yscale('log')
formatter = ScalarFormatter()
formatter.set_scientific(False)
axs.yaxis.set_major_formatter(formatter)
plt.show()

【讨论】:

  • 这适用于 10 的幂,但不适用于 50... 对于 50,它将显示 5 x 10^1 和 60 -> 6 x 10^1... 但是在 100 ,它显示 100 而不是 1 x 10^2... 与 400 相同... 4 x 10^2
  • 如果您能在问题中解释您的明确示例,将会有很大帮助。在这种情况下,您有主要和次要刻度。要同时更改次要刻度,您还可以设置 axs.yaxis.set_minor_formatter(formatter)
  • 做到了。未成年人。本来打算发布一个例子...但忙于为病毒囤积...对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-02
  • 2012-05-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多