"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 Preserve Numerical Accuracy in Matplotlib Axis Tick Labels?

How to Preserve Numerical Accuracy in Matplotlib Axis Tick Labels?

Published on 2024-11-07
Browse:858

How to Preserve Numerical Accuracy in Matplotlib Axis Tick Labels?

Customizing Matplotlib Axis Tick Labels for Numerical Accuracy

When using Matplotlib library in Python for plotting simple x-y datasets, it's common to encounter axis values switching from standard numerical format to scientific notation with exponential form upon zooming in on specific graph sections. This can be undesirable, as it obscures the original values.

To prevent this behavior and retain the original numerical formatting, it's necessary to adjust the axis tick label formatting. By default, Matplotlib uses a ScalerFormatter for tick labels. This formatter may utilize a constant shift, resulting in scientific notation when dealing with very small fractional changes in visible values.

To disable this constant shift and force standard numerical formatting, the following code can be employed:

import matplotlib.pyplot as plt

plt.plot(np.arange(0, 100, 10)   1000, np.arange(0, 100, 10))
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.draw()

For cases where scientific notation is altogether undesirable, the following code can be used:

ax.get_xaxis().get_major_formatter().set_scientific(False)

Alternatively, global control over this behavior can be achieved via the axes.formatter.useoffset rcparam. By altering this parameter, it's possible to enforce either standard numerical formatting or scientific notation uniformly across all axes tick labels.

This customization ensures that numerical accuracy is maintained even when zooming in on graphs, providing users with a more intuitive and precise representation of their data.

Release Statement This article is reprinted at: 1729514295 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