1 回答
TA贡献1777条经验 获得超3个赞
您可以根据数据中的最大值动态设置限制:
def grabMaxExponent(values, precision=1):
"""Given a list of numericals, returns the exponent of the max
value in scientific notation, adjusted to have no sig figs in
decimal places.
e.g. 190 > 1.9E+02 > 19E+01
returns exponent (1)"""
# Grab max value and convert to scientific notation
value = format(max(values), f"5.{precision}E")
# Pull exponent
a,m = value.split('E+')
a,b = a.split('.')
a,b,m = map(int, (a,b,m))
# If significant figures non-zero, increase exponent to bring them before decimal point
if b > 0:
m-=precision
return m
m = grabMaxExponent(y)
# Set scilimits to m
plt.ticklabel_format(axis='y', style='sci', scilimits=(m,m))
https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.ticklabel_format.html
添加回答
举报