Do you know the automatic scientific notation form...
Criado em: 23 de junho de 2025
Criado em: 23 de junho de 2025
Do you know the automatic scientific notation formatting on the y-axis ticks of matplotlib pyplot? I want to force it to not showing, only shows like 0.1f in the y axis tick, if it is possible, I want to most aggresive method!!!
Below are several increasingly “aggressive” ways to force Matplotlib to stop switching your y-axis to scientific notation and instead show fixed-point labels such as 12.3
, 0.4
, etc. Pick the one that best matches how broadly you want to apply the rule.
pythonax.ticklabel_format(axis='y', style='plain', # turn off exponential formatting useOffset=False, # turn off the +1.23e4 offset line useMathText=False)
style='plain'
tells the default ScalarFormatter
to write every tick as a normal number, while useOffset=False
prevents the offset line that sometimes appears above the axis. stackoverflow.com
pythonfrom matplotlib.ticker import FormatStrFormatter ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f')) # one decimal place
FormatStrFormatter
bypasses the default formatter completely, so even very large or very small values are rendered with the string format you supply (%.1f
, %.2f
, {x:.3f}
, etc.). stackoverflow.com
pythonfrom matplotlib.ticker import ScalarFormatter fmt = ScalarFormatter() fmt.set_scientific(False) # don’t use 1.23e4 style fmt.set_useOffset(False) # don’t add +1e4 offset fmt.set_powerlimits((-100, 100)) # never switch to *10^k (very aggressive) ax.yaxis.set_major_formatter(fmt)
set_powerlimits((-100, 100))
widens the threshold far beyond typical data ranges so the formatter will not flip to exponential form. matplotlib.org
pythonimport matplotlib as mpl mpl.rcParams['axes.formatter.useoffset'] = False mpl.rcParams['axes.formatter.limits'] = (-100, 100) # global version of powerlimits
Put those two lines once, early in your session (or in your matplotlibrc
) and all subsequent figures will suppress scientific notation unless you explicitly override it. sqlpey.com
Problem | Fix |
---|---|
Labels overlap after switching format | Rotate or nudge with plt.setp(ax.get_yticklabels(), rotation=0, ha='right') or use a MaxNLocator . |
You only need this for one axis | Apply any approach to ax.xaxis or ax.yaxis individually. |
Mixed log & linear plots | For a log axis use LogFormatter instead; the methods above apply to linear axes. |
With these tools—especially option 3 or 4—you can completely eliminate scientific notation from your y-axis, no matter how extreme the underlying numbers.