为什么在 matplotlib 中调整 scilimits 不能正常工作? [英] Why doesn't adjusting scilimits in matplotlib work properly?

查看:86
本文介绍了为什么在 matplotlib 中调整 scilimits 不能正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码绘制具有相同 scilimits 的图:

from matplotlib import rcrc('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(样式='sci',轴='x',scilimits =(5,5),useMathText = True)plt.ticklabel_format(style ='sci',axis ='y',scilimits =(5,5),useMathText = True)plt.xticks(字体大小=14)plt.yticks(字体大小=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',花式框=真,阴影=假,ncol=1,点数=1,字体大小=16)

我原以为 axis='x', scilimits=(5,5) 会帮助我的 x 轴成为 10**5,但是,它产生 10**6.你能帮我找出我做错的地方吗?

解决方案

以下解决方案基于类似的

I am trying to make a plot with the same scilimits using the following code:

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)

I was expecting that axis='x', scilimits=(5,5) would help my x-axis to be 10**5, however, it yields 10**6.Can you please help me to figure out where I am doing wrong?

解决方案

The following solution is based on a similar post by the matplotlib wizard ImportanceOfBeingErnest ;)

Here you just apply the modification to the x-axis since the y-axis values are already below 10^6.

Please upvote the original post on the above link if you find it helpful.

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

这篇关于为什么在 matplotlib 中调整 scilimits 不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆