Skip to main content
 首页 » 编程设计

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

2025年01月19日13wayfarer

假设我有以下代码

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())

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()