Formatting Axis Offset-Values for Whole Numbers or Specific Values
Question:
Consider a matplotlib figure where data values are expressed in nanoseconds (1e-9). If the y-axis displays values like "4.4 with 1e-8 offset," how can the axis be forced to display "44 with 1e-9 offset"? Similarly, if the x-axis displays values like " 5.54478e4," how can it be shown as " 55447" (whole number without decimal)?
Solution:
To achieve the desired formatting, use the following steps:
from matplotlib.ticker import ScalarFormatter
y_formatter = ScalarFormatter(useOffset=False)
ax.yaxis.set_major_formatter(y_formatter)
This will disable the automatic offset used by matplotlib when displaying scientific notation.
To display whole numbers in the offset without decimals, adjust the number of significant figures:
x_formatter = ScalarFormatter(useOffset=False, useMathText=True)
x_formatter.set_powerlimits((-9,5)) # Display numbers up to 5 significant figures
ax.xaxis.set_major_formatter(x_formatter)
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3