Home > Backend Development > Python Tutorial > 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?

Susan Sarandon
Release: 2024-10-24 06:58:30
Original
925 people have browsed it

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])
Copy after login

This generates ticks on the abscissa axis as follows:

0.0     0.5     1.0     1.5     2.0
+1e3
Copy after login

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
Copy after login

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

The above is the detailed content of How to Eliminate Relative Shift in Matplotlib\'s Tick Labels for Large Numbers?. For more information, please follow other related articles on the PHP Chinese website!

source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template