"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Eliminate Relative Shift in Matplotlib\'s Tick Labels for Large Numbers?

How to Eliminate Relative Shift in Matplotlib\'s Tick Labels for Large Numbers?

Published on 2024-11-07
Browse:784

How to Eliminate Relative Shift in Matplotlib\'s Tick Labels for Large Numbers?

Removing Relative Shift in Matplotlib Axis

Plotting against large numbers in Matplotlib can result in an axis with a relative shift for the tick labels. To illustrate, consider the following plot:

plot([1000, 1001, 1002], [1, 2, 3])

This generates ticks on the abscissa axis as follows:

0.0     0.5     1.0     1.5     2.0
 1e3

To eliminate the " 1e3" label and obtain tick labels of the form "1000.0", "1001.0", etc., follow these steps:

  1. Grab the current axes using gca().
  2. Obtain the x-axis axis object using get_xaxis().
  3. Retrieve the major formatter object using get_major_formatter().
  4. Set the useOffset attribute of the formatter to False using set_useOffset(False).
  5. Call draw() to update the plot.

Alternatively, in newer versions of Matplotlib (1.4 ), the default behavior can be modified via the axes.formatter.useoffset rcparam:

matplotlib.rcParams['axes.formatter.useoffset'] = False

By applying these methods, you can remove the relative shift in the axis and obtain tick labels in the desired format.

Release Statement This article is reprinted at: 1729722110 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

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