📄 np_plot.py
字号:
return y ############################################################################## def scale(self): mmbase = self.sets[0] ibase = self.sets[0].curr_indices[-1][0] self.xmax = self.xmin = mmbase.data[ibase][0] #print ibase, mmbase.data[ibase] self.ymax = self.ymin = mmbase.data[ibase][1] style = self.plot.stylev_curr.get() for s in self.sets: #print data i1, i2 = s.curr_indices[-1] if i1 == None: continue data = s.data #s.printself() xmax = xmin = data[i1][0] ymax = ymin = data[i1][1] # allow for error bars? eb = 0 if (style & STYLE_EBARS) and ('SD' in s.fields or 'SD' in s.fields): if style == STYLE_EBARS_SD: ebdi = s.fields.index('SD') elif style == STYLE_EBARS_SE: ebdi = s.fields.index('SE') #print 'ebdi', ebdi if len(data[i1]) > ebdi: eb = 1 ymin = data[i1][1]-data[i1][ebdi] ymax = data[i1][1]+data[i1][ebdi] llen = 0 for p in data[i1:i2+1]: llen = llen + 1 x = p[0] y = p[1] if not int(x) == x: self.xfloats = 1 else: p[0] = float(x) if not int(y) == y: self.yfloats = 1 else: p[1] = float(y) xmax = MAX(xmax, x) xmin = MIN(xmin, x) if eb: ymax = max(ymax, y+p[ebdi]) ymin = min(ymin, y-p[ebdi]) else: ymax = MAX(ymax, y) ymin = MIN(ymin, y) self.xmax = MAX(self.xmax, xmax) self.xmin = MIN(self.xmin, xmin) self.ymax = MAX(self.ymax, ymax) self.ymin = MIN(self.ymin, ymin) if self.mode == MODE_PDF: ymin = 0.0 if self.zero_origin: self.ymin = min(0.0, self.ymin) if self.mode & MODE_HIST and self.mode & MODE_RANGES: self.xmin = self.xmin - self.get_bktsz(s) xrange = float(self.xmax - self.xmin) if xrange == 0.0: xrange = float(self.xmax/10) try: self.xscalef = float(self.xwi/xrange) except ZeroDivisionError: xrange = 10.0 self.xscalef = float(self.xwi/xrange) yrange = float(self.ymax - self.ymin) if yrange == 0: yrange = float(self.ymax/10) try: self.yscalef = float(self.yht/yrange) except ZeroDivisionError: yrange = 10.0 self.yscalef = float(self.yht/yrange) self.xrange = xrange self.yrange = yrange # print 'scale x: max=%.3f min=%.3f scalef=%f y: max=%.3f min=%.3f scalef=%f' % (self.xmax, self.xmin, self.xscalef, self.ymax, self.ymin, self.yscalef)############################################################################## def use_scaledata(self): s = self.scaledata self.xmax = s[0] self.xmin = s[1] self.ymax = s[2] self.ymin = s[3] self.xscalef = s[4] self.yscalef = s[5] self.xrange = s[6] self.yrange = s[7] self.xfloats = s[8] self.yfloats = s[9]############################################################################## def draw_axes(self): LINE = self.canv.create_line width = self.axislinewidth TEXT = self.canv.create_text if self.mode == MODE_PDF: xlab = self.ylab ylab = 'Probability\nof occurence' elif self.mode == MODE_CDF: xlab = self.ylab ylab = 'Cumulative\nProbability\nof occurence' else: xlab = self.xlab ylab = self.ylab xlab = self.draw_xtics(xlab) ylab = self.draw_ytics(ylab) # y axis LINE(self.xorg, self.yorg, self.xorg, self.yorg-self.yht, fill=self.fgcol, width=width) TEXT(self.xorg, self.yorg-self.yht-15, text=ylab, fill=self.fgcol, anchor = SE, font=LABELFONT) # x axis LINE(self.xorg, self.yorg, self.xorg+self.xwi, self.yorg, fill=self.fgcol, width=width) TEXT(self.xorg+self.xwi, self.yorg+20, text=xlab, fill=self.fgcol, anchor = NE, font=LABELFONT) ############################################################################## def calc_ticfacts(self, ticsep, scalef, range, floats): try: t = ticsep/scalef except OverflowError: print 'ticfacts except: range=%s scalef=%s' % (repr(range), repr(scalef)) inc = range axinc = inc*scalef return (axinc, inc) #print 'ticfacts: range%s scalef%s' % (repr(range), repr(scalef)) #print 't=%.3f' % (t) #inc = 1.0000000000000 inc = 0.000001 while inc < t: inc = inc*10 lastdelta = abs(inc - t) lastinc = inc biginc = inc for scale in [2,4,10]: thisinc = inc/scale delta = abs(thisinc - t) if delta > lastdelta: break lastdelta = delta lastinc = thisinc smallinc = lastinc if smallinc == biginc/4 or smallinc == biginc/10: medinc = biginc/2 else: medinc = biginc if smallinc == 0: smallinc = medinc = biginc = 1 #? axinc = smallinc*scalef return (axinc, smallinc, medinc, biginc) ############################################################################## def calc_ticfacts_time(self, ticsep, scalef, range, floats, unit): #print 'calc_ticfacts_time: unit \'%s\' range=%s' % (unit, repr(range)) scales1 = [2, 4, 10] scales2 = [2, 4, 12] scales3 = [2, 4, 24] inc1 = 0.000000001 tunits = [('', 1, None), ('ns', 1000, scales1), ('us', 1000, scales1, inc1), ('ms', 1000, scales1, inc1), ('s', 60, scales1), ('m', 60, scales2), ('h', 24, scales2), ('days', 1, scales3)] start = [units[0] for units in tunits].index(unit) unit = tunits[start][0] scales = tunits[start][2] fact = 1.0 while tunits[start][1] > 1: newfact = tunits[start][1] if range < fact*newfact: break start += 1 unit = tunits[start][0] scales = tunits[start][2] fact = fact*newfact #print 'calc_ticfacts_time: unit %s, fact=%s, scales ' % (unit, repr(fact)), #print scales try: t = ticsep/scalef except OverflowError: print 'ticfacts_time except: range=%s scalef=%s' % (repr(range), repr(scalef)) inc = range xinc = inc*scalef return (xinc, inc) #print 'ticfacts_time: range=%s scalef=%s' % (repr(range), repr(scalef)) #print 't=%.3f' % (t) inc = 0.000000001*fact while inc < t: #print 'inc %s' % (repr(inc)) inc = inc*10 #print 'inc %s' % (repr(inc)) lastdelta = abs(inc - t) lastinc = inc biginc = inc for scale in scales: thisinc = inc/scale delta = abs(thisinc - t) if delta > lastdelta: break lastdelta = delta lastinc = thisinc #print 'lastinc %s' % (repr(lastinc)) smallinc = lastinc if smallinc == biginc/scales[1] or smallinc == biginc/scales[-1]: medinc = biginc/2 else: medinc = smallinc if smallinc == 0: smallinc = medinc = biginc = 1 #? axinc = smallinc*scalef #print 'axinc=%s, incs=%s/%s/%s , unit=\'%s\'' % (repr(axinc), repr(smallinc), repr(medinc), repr(biginc), unit) return (axinc, smallinc, medinc, biginc, fact, unit) ############################################################################### def parse_lab(self, lab): if lab.count('%'): i = lab.index('%') if lab[i+1] != '%': return (lab[:i], lab[i+1:]) #else: #return (lab[:i] + lab[i+1:], 'dec') return (lab, 'dec') ############################################################################### def draw_ytics(self, lab): return self.draw_tics(-1, self.ymin, self.yrange, self.yscalef, self.yorg, self.min_ytic_ht, self.yfloats, self.yht, lab) ############################################################################### def draw_xtics(self, lab): return self.draw_tics(1, self.xmin, self.xrange, self.xscalef, self.xorg, self.min_xtic_wi, self.xfloats, self.xwi, lab, trace=0) ############################################################################### def draw_tics(self, dir, minval, range, scalef, org, ticsep, floats, dim, lab, trace=0): def keepgoing(dir, pos, org, dim): if dir == 1 and pos <= org+dim: return 1 elif dir == -1 and pos >= org-dim: return 1 else: return 0 def near_nuff(val, inc): if fabs(fmod(val, inc)) < inc/100: return 1 else: return 0 def pretty(v, min_places): def ppr(v, val, places): fmt = '%%.%df' % (places) # print 'printing', fmt % (val), '%d places v=%.10f' % (places, v) # pretty print tic values #print min_places ## if int(v) == v:## #print 'int', v## #print 'printing %d' % (int(v))## return ('%d' % (int(v)), 0) SF = 3 savev = v places = 0 blanks = 0 sf = 0 reps = 0 last_trailer = None #print 'start printing %.10f'% (v) while modf(v)[0]: #print 'upping', #ppr(v, savev, places) places += 1 v *= 10 ip = modf(v)[1] if ip != 0: sf += 1 # got a s.f. if not ip%10: blanks += 1 # this place doesn't add to precision if (blanks == SF and sf >= SF) or blanks >= 10: # enough s.f. and precision #print 'blanks break' break if last_trailer != None and ip%10 == last_trailer: reps += 1 if reps >= SF and sf >= SF: #print 'reps break' break last_trailer = ip%10 places = max([min_places, places]) #print 'endup', #ppr(v, savev, places) fmt = '%%.%df' % (places) str = fmt % (savev) # consume any trailing zeros while str[-1] == '0' and places > min_places: places -=1 fmt = '%%.%df' % (places) str = fmt % (savev) #print 'final', #ppr(v, savev, places) #print 'xxxxxx' return (str, places) def time_pretty(v, unit): # print 'time', v, unit if unit == 'h' or unit == 'm': minor = (v*60)%60 elif unit == 'days': minor = (v*24)%24 v = floor(v) if minor: return '%d' % (minor) else: return '%.0f' % (v) # # Start main function # if dir == 1: ds = 'X' else: ds = 'Y' #print ds if trace: print '%stics: minval=%s maxval=%s range=%s\nfloats %d org %d' \ % (ds, repr(minval), repr(minval+range), repr(range), floats, org) LINE = self.canv.create_line width = self.axislinewidth TEXT = self.canv.create_text lab, unit = self.parse_lab(lab) if unit == 'dec': fact = 1 axinc, smallinc, medinc, biginc = self.calc_ticfacts(ticsep, scalef, range, floats) else: axinc, smallinc, medinc, biginc, fact, unit \ = self.calc_ticfacts_time(ticsep, scalef, range, floats, unit) lab += unit if trace:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -