如何让Matplotlib绘图中Y轴大数值完整显示而非科学计数法形式?
Got it, this is a common quirk with Matplotlib—it automatically switches to scientific notation when it thinks numbers are too large to display neatly, even if the actual value range is tiny. Here are a couple of straightforward ways to fix this:
Method 1: Use a Formatter to Display Full Integers
You can explicitly set a formatter for the Y-axis to show complete integer values, giving you full control over label appearance:
import matplotlib.pyplot as plt # Your original scatter plot code x = [0,1,2,3,4,5] y = [10001,10002,10003,10004,10005,10006] plt.scatter(x, y) # Force Y-axis to show full integer values plt.gca().yaxis.set_major_formatter('{:d}'.format) plt.show()
Method 2: Use StrMethodFormatter for More Flexibility
If you might work with decimal values later, or want finer control over formatting, use matplotlib.ticker.StrMethodFormatter:
import matplotlib.pyplot as plt from matplotlib.ticker import StrMethodFormatter x = [0,1,2,3,4,5] y = [10001,10002,10003,10004,10005,10006] plt.scatter(x, y) # Format Y-axis labels as integers (adjust .0f to .2f if you need decimals) plt.gca().yaxis.set_major_formatter(StrMethodFormatter('{x:.0f}')) plt.show()
Method 3: Turn Off Scientific Notation Directly
You can also disable scientific notation entirely for the Y-axis with ticklabel_format:
import matplotlib.pyplot as plt x = [0,1,2,3,4,5] y = [10001,10002,10003,10004,10005,10006] plt.scatter(x, y) # Disable scientific notation for Y-axis plt.ticklabel_format(axis='y', style='plain') plt.show()
Bonus: Force Exact Y-Ticks
If you want the Y-axis to explicitly label every data point's value (10001 through 10006), add this line:
plt.yticks(y)
This ensures every value in your dataset is visible on the axis, which is especially helpful when your value range is this narrow.
内容的提问来源于stack exchange,提问作者lauriane.g

