简体   繁体   English

"如何在 matplotlib 对数图上删除科学记数法"

[英]How to remove scientific notation on a matplotlib log-log plot

I know that this question has been asked before, but I tried all the possible solutions and none of them worked for me.我知道以前有人问过这个问题,但是我尝试了所有可能的解决方案,但没有一个对我有用。

So, I have a log-log plot in matplotlib, and I would like to avoid scientific notation on the x-axis.所以,我在 matplotlib 中有一个对数图,我想避免在 x 轴上使用科学记数法。

This is my code:这是我的代码:

from numpy import array, log, pi
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import matplotlib.ticker as mticker

plt.rc('axes.formatter', useoffset=False)

tc = array([7499680.0, 12508380.0, 23858280.0, 34877020.0, 53970660.0, 89248580.0, 161032860.0, 326814160.0, 784460200.0])

theta = array([70, 60, 50, 45, 40, 35, 30, 25, 20])

plt.scatter(theta,tc)

ax=plt.gca()

ax.set_xscale('log')
ax.set_yscale('log')

ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
ax.xaxis.get_major_formatter().set_scientific(False)
ax.xaxis.get_major_formatter().set_useOffset(False)

plt.show()

Those are minor ticks on the x-axis (ie they are not on integer powers of 10), not major ticks.这些是 x 轴上的次要刻度(即它们不是 10 的整数幂),而不是主要刻度。 matplotlib<\/code> automatically detemines if it should label the major or minor ticks - in this case because you don't have any major ticks displayed in the x range, the minor ticks are being labelled). matplotlib<\/code>自动确定它是否应该标记主要或次要刻度 - 在这种情况下,因为您没有在 x 范围内显示任何主要刻度,所以正在标记次要刻度)。 So, you need to use the set_minor_formatter<\/code> method:因此,您需要使用set_minor_formatter<\/code>方法:

ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())

The following can be used as a workaround ( original answer<\/a> ):以下可以用作解决方法(原始答案<\/a>):

from matplotlib.ticker import StrMethodFormatter, NullFormatter
ax.yaxis.set_major_formatter(StrMethodFormatter('{x:.0f}'))
ax.yaxis.set_minor_formatter(NullFormatter())

If you want to set just the xaxis to no longer use scientific notation you need to change the fromatter and then you can set it to plain.如果您只想将 xaxis 设置为不再使用科学记数法,则需要更改 fromatter,然后您可以将其设置为 plain。

ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())
ax.ticklabel_format(style='plain', axis='x')

如果你想同时禁用偏移量和科学记数法,你可以使用ax.ticklabel_format(useOffset=False, style='plain')<\/code>

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM