为什么在matplotlib中调整scilimits不能正常工作?

时间:2018-12-23 23:18:57

标签: python matplotlib

我正在尝试使用以下代码使用相同的scilimits作图:

from matplotlib import rc
rc('text', usetex=True)
plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
plt.figure(figsize=(8, 8))
aaaa = [64673.753739998487, 47600.685099333175, 103536.00001368104, 72449.921543225209, 
     61508.505984112242, 70263.491595277388, 54348.281891183826, 49961.598403993477, 
     33835.286137921314, 34182.104988390645, 35539.090978564935, 69718.606491298371, 
     64032.096678612936, 52855.807603271372, 70679.662115378873, 91608.723258364189, 
     91600.042916546503, 82024.037261705904, 63732.304870187407, 47904.610195267909, 
     46001.244148731595, 24976.947952480623, 24592.553349545487, 27348.600652481313, 
     81136.751837311749, 49655.597812696637, 58389.606840344379, 52350.219450883087, 
     17098.39071276773, 36878.709818584044, 50013.871736403096, 35444.332307350232, 
     52980.143877805109, 47794.752894901736, 53415.497902966708, 87468.158494885298, 
     43987.111394756357, 52740.394838572705, 39854.41297787604, 14290.064757144555, 
     59774.501794076656, 74379.013185690623, 60637.406496348274, 27642.107019667237, 
     56442.905348738204, 12118.615765071288, 82440.158580033807, 25663.625231811915, 
     10678.714405095518, 4956.9098748649003, 29124.723882758833, 18746.727949554137, 
     7895.9466736771647, 23494.076724132057, 41322.088510975504, 40117.955865577132, 
     23621.902085664922, 50504.580577880901, 34827.296362983485, 9667.8131544045937, 
     29516.370019205577, 4958.6179626158646, 22072.187168100805, 10332.974318750123, 
     4750.2433278318049, 12963.348939660838]
bbbb = [1463060.7664889875, 1490667.9353886067, 2731854.306528457, 1426342.0116374588, 
  2083867.3081647172, 1995195.1159607177, 605631.4319293281, 1002276.8715411476, 
  689368.0650139145, 529666.5221268163, 543505.9613533892, 319234.8941877205, 
  1359750.9594256533, 773179.6592194682, 690644.7062404836, 892392.4537823396, 
  2153525.80346547, 1063883.5510123423, 971368.4472613719, 1175545.4059684786, 
  422568.1756412806, 497291.5363220389, 354329.0495590891, 481157.7096561746, 
  509541.2954844916, 636751.2599259235, 559092.7235128282, 383569.55369432206, 
  213130.5859858809, 116223.0655109612, 744259.9557510511, 354787.2969271489, 
  1044499.4434171829, 286877.1179750149, 166630.02501717687, 402163.62581247673, 
  302989.7801416834, 237494.47912775108, 367482.5393384159, 168331.76670558963, 
  423096.4255360829, 1214630.3577723764, 169831.30063169816, 213402.10004502145, 
  346942.76407560415, 255456.10035719394, 628616.4650582516, 164595.69059363392, 
  154831.84459743137, 72780.84627038662, 210376.77469373296, 116647.33026847115, 
  129570.58089614184, 282714.18605033203, 65219.39402292857, 307928.71853371797, 
  181462.35490009392, 225568.15926706092, 124674.92646231048, 173866.74045265373, 
  204237.15282636438, 58584.83339857978, 129355.98656862657, 55535.14076293081, 
  23301.53441103586, 38277.76101210561]



plt.plot(bbbb, aaaa, 'k*', label="$A$", markerfacecolor='none', markersize = 8)

plt.ticklabel_format(style='sci', axis='x', scilimits=(5,5), useMathText=True)
plt.ticklabel_format(style='sci', axis='y', scilimits=(5,5), useMathText=True)
plt.xticks(fontsize=14)
plt.yticks(fontsize=14)
plt.rc('font', size=14)
plt.xlabel(r'$\text{whateveronxaxis}$', fontsize=16, fontname='Times New Roman')
plt.grid()
plt.ylabel(r'$\text{whateveronyaxis}$', fontsize=16, fontname='Times New Roman')
leg = plt.legend(loc='best',
      fancybox=True, shadow=False, ncol=1, numpoints = 1, fontsize=16)

我期望axis='x', scilimits=(5,5)将使我的x轴成为10**5,但是它会产生10**6。您能帮我弄清楚我在哪里做错了吗?

enter image description here

1 个答案:

答案 0 :(得分:0)

以下解决方案基于matplotlib向导post的类似ImportanceOfBeingErnest;)

由于y轴值已经低于10 ^ 6,因此您只需将修改应用于x轴。

如果发现有帮助,请在上面的链接上对原始帖子进行投票。

from matplotlib import rc
import matplotlib.ticker 

fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(111)

# Your data here

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat="%1.1f", offset=True, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_orderOfMagnitude(self, nothing):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin, vmax):
        self.format = self.fformat
        if self._useMathText:
            self.format = '$%s$' % matplotlib.ticker._mathdefault(self.format)


plt.plot(bbbb, aaaa, 'k*', label="$A$", markerfacecolor='none', markersize = 8)

plt.ticklabel_format(style='sci', axis='y', scilimits=(5,5), useMathText=True)

# Setting the x-axis notation as 10^5
ax.xaxis.set_major_formatter(OOMFormatter(5, "%1.1f"))
ax.ticklabel_format(axis='x', style='sci', scilimits=(5,5))

# Remaining code here

enter image description here