Home > Backend Development > Python Tutorial > How to Eliminate Relative Shift in Matplotlib Axis Labels?

How to Eliminate Relative Shift in Matplotlib Axis Labels?

Barbara Streisand
Release: 2024-10-24 07:15:30
Original
1098 people have browsed it

How to Eliminate Relative Shift in Matplotlib Axis Labels?

Eliminating Relative Shift in Matplotlib Axis

When plotting data involving large numbers, it's common to encounter an axis with relative shift, resulting in ticks with a fractional component accompanied by a magnitude indicator (e.g., " 1e3"). This can be unintuitive, especially when dealing with smaller datasets.

To resolve this issue, Matplotlib provides a straightforward solution that involves configuring the major formatter object on the x-axis:

1

2

3

<code class="python">plot([1000, 1001, 1002], [1, 2, 3])

gca().get_xaxis().get_major_formatter().set_useOffset(False)

draw()</code>

Copy after login
Copy after login

By setting useOffset to False, the formatter is instructed to display the tick values without the relative shift. This results in cleaner axis labels, as seen in the following code:

1

2

3

<code class="python">plot([1000, 1001, 1002], [1, 2, 3])

gca().get_xaxis().get_major_formatter().set_useOffset(False)

draw()</code>

Copy after login
Copy after login

This code will produce an axis with tick values like this:

1

1000.0  1000.5  1001.0  1001.5  1002.0

Copy after login

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

1

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

Copy after login

The above is the detailed content of How to Eliminate Relative Shift in Matplotlib Axis Labels?. 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