如何在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.
因此,我在matplotlib中有一个对数对数图,我想避免在x轴上使用科学计数法.
So, I have a log-log plot in matplotlib, and I would like to avoid scientific notation on the x-axis.
这是我的代码:
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()
这是输出:
如您所见,x 轴上的数字仍采用科学计数法.我想将它们显示为 20、30、40……我尝试了所有可能的解决方案,但没有结果.
As you can see, the numbers on the x-axis are still in scientific notation. I would like to display them as 20, 30, 40... I tried every possible solution with no result.
非常感谢能为您提供帮助的所有人.
Thank you very much to everyone that will help.
NB.我不能使用 plt.loglog() 命令,因为我正在对数据进行一些曲线拟合,我需要这样.
NB. I can't use the plt.loglog() command, because I am doing some curve fitting on the data and I need it like that.
NB2.我注意到发生了一件很奇怪的事情:如果我将代码更改为yaxis.get_mayor_formatter()...,它可以在y轴上工作!只是在 x 上它不起作用.怎么可能?
NB2. I noticed a very weird thing happening: if I change the code to yaxis.get_mayor_formatter()..., it works on the y-axis! It is just on the x one that it's not working. How is it possible?
也许不清楚,但如果你看代码,有3种方法会影响x-ticks的显示: plt.rc('axes.formatter', useoffset=False)
, ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
和 ax.xaxis.get_major_formatter().set_scientific(False)
.根据我的发现,这是3种方法,它们都应该一个人完成,但事实并非如此.当然,我也一一尝试了它们,而不是一起尝试.
maybe it is not clear, but if you look at the code, there are 3 methods that should affect the display of the x-ticks: plt.rc('axes.formatter', useoffset=False)
, ax.xaxis.set_major_formatter(mticker.ScalarFormatter())
and ax.xaxis.get_major_formatter().set_scientific(False)
. They are 3 methods that should all do the trick alone, according to what I found around, but they don't. Of course I also tried them one by one and not all together.
推荐答案
这些是x轴上的次要刻度线(即它们不是10的整数次幂),而不是主要刻度线. matplotlib
自动确定是否应标记主要或次要价格变动–在这种情况下,因为您没有在x范围内显示任何主要价格变动,因此将对次要价格变动进行标记.所以,你需要使用 set_minor_formatter
方法:
Those are minor ticks on the x-axis (i.e. they are not on integer powers of 10), not major ticks. matplotlib
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). So, you need to use the set_minor_formatter
method:
ax.xaxis.set_minor_formatter(mticker.ScalarFormatter())
它在 y 轴上起作用的原因是因为这些刻度是主要刻度(即 10 的整数次幂),而不是次刻度.
The reason it works on the y-axis is because those ticks are major ticks (i.e. on integer powers of 10), not minor ticks.
这篇关于如何在matplotlib对数-对数图上删除科学计数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!