Home  >  Article  >  Backend Development  >  How to Disable Axis Tick Relative Shift in Matplotlib?

How to Disable Axis Tick Relative Shift in Matplotlib?

DDD
DDDOriginal
2024-10-24 06:22:31650browse

How to Disable Axis Tick Relative Shift in Matplotlib?

Removing Axis Tick Relative Shift in Matplotlib

When dealing with graphs displaying numeric ranges spanning significant values, matplotlib assigns a relative shift syntax ( 1e3 in this case) to the axis ticks. For instance, with the plot:

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

The x-axis ticks might appear as:

0.0     0.5     1.0     1.5     2.0
+1e3

To eliminate the relative shift and obtain ticks like:

1000.0  1000.5  1001.0  1001.5  1002.0

Follow these steps:

<code class="python">plot([1000, 1001, 1002], [1, 2, 3])
gca().get_xaxis().get_major_formatter().set_useOffset(False)
draw()</code>

This technique involves retrieving the active axes, obtaining the x-axis axis object, and then accessing the major formatter. By setting the useOffset attribute to False, the relative shift is disabled.

Alternatively, in matplotlib versions 1.4 and later, you can modify the default behavior by adjusting the axes.formatter.useoffset parameter:

<code class="python">rcParams.update({'axes.formatter.useoffset': False})</code>

The above is the detailed content of How to Disable Axis Tick Relative Shift in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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