Matplotlib 在半学图中禁用指数表示法
Matplotlib disable exponent notation in semilogy plots
假设我有以下代码
import matplotlib.pyplot as plt
plt.figure()
plt.semilogy([0,1],[20,90])
plt.show()
创建下图:
我想禁用 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())
完整示例:
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()
假设我有以下代码
import matplotlib.pyplot as plt
plt.figure()
plt.semilogy([0,1],[20,90])
plt.show()
创建下图:
我想禁用 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())
完整示例:
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()