gpt4 book ai didi

python - Matplotlib 在半学图中禁用指数表示法

转载 作者:太空宇宙 更新时间:2023-11-04 04:57:27 25 4
gpt4 key购买 nike

假设我有以下代码

import matplotlib.pyplot as plt
plt.figure()
plt.semilogy([0,1],[20,90])
plt.show()

创建下图:

enter image description here

我想禁用 y 轴上的科学记数法(所以我想要 20、30、40、60,而不是 2x10^1 等)

我已经看过 this线程,我尝试添加

import matplotlib.ticker
plt.gca().get_yaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter())
plt.gca().get_yaxis().get_major_formatter().set_scientific(False)
plt.gca().get_yaxis().get_major_formatter().set_useOffset(False)

但不影响结果图。我正在使用 python 3.5.3 和 matplotlib 2.1.0。我错过了什么?

最佳答案

由于 y 轴上的刻度小于十年,因此它们是次要刻度,而不是主要刻度。因此,您需要将 minor formatter 设置为 ScalarFormatter

plt.gca().yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())

Complete example:

import matplotlib.pyplot as plt
import matplotlib.ticker

plt.figure()
plt.semilogy([0,1],[20,90])
plt.gca().yaxis.set_minor_formatter(matplotlib.ticker.ScalarFormatter())
plt.show()

enter image description here

关于python - Matplotlib 在半学图中禁用指数表示法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46729344/

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