matplotlib 画图纵坐标使用科学计数法不同方法

2024-03-14  本文已影响0人  DUTZCS

使用matplotlib画图时候, 有时候经常想使用科学计数法, 并且将\times 10^n写在图的左上角,如图所示

注意红色框中的科学计数法

实现这种结果的目前我知道的有两种,一种是

from matplotlib import ticker

formatter = ticker.ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
formatter.set_powerlimits((-1, 1))# 不使用这种形式的数据范围$10^{-1} - 10^{1}$,如果想全用这种方式表示则可以设为(0,0)

ax.semilogy(list_var, ff)
ax.yaxis.set_major_formatter(formatter)
ax.yaxis.get_offset_text().set(size=20) #设置字:体大小

这种适用于semilogy以及plot这种形式的画图,有时候可能对plot支持不太好,可以用下面一种(更简单,不需要import ticker):

ax.plot(list_var, ff)
ax.ticklabel_format(style='sci', scilimits=(-1,2), axis='y',useMathText=True)
ax.yaxis.get_offset_text().set(size=20)

这种适用于使用plot形式的画图,不适用于semilogy。

上一篇下一篇

猜你喜欢

热点阅读