📄 np_plot.py
字号:
print '%stics: axinc=%s incs=%s/%s/%s' % (ds, axinc, repr(smallinc), repr(medinc), repr(biginc)) val = minval adj = 0 pos = org # get the first tic to a sensible value baseadj = minval%smallinc if baseadj: val = minval + smallinc - baseadj # offset the tics adj = dir*int(baseadj*scalef) pos = org - adj + dir*axinc if trace: print '%stics baseadj=%s val=%s adj=%s pos=%s' % (ds, repr(baseadj), repr(val), repr(adj), repr(pos)) if dir == -1: ticstart = self.xorg else: ticstart = self.yorg smalltic = 4 medtic = 7 bigtic = 10 bigfont = ("helvetica", 8) medfont = ("helvetica", 6) savepos = pos saveval = val printsmalls = 0 printed = 0 # establish min places min_places = 0 while keepgoing(dir, pos, org, dim): min_places = max([pretty(val, 0)[1], min_places]) pos = pos+(dir*axinc) val = val+smallinc passes = 0 while printed < 2 and passes < 3: pos = savepos val = saveval while keepgoing(dir, pos, org, dim): if trace: print '%stics val=%s pos=%s label=%s val%%biginc=%s' % (ds, repr(val), repr(pos), repr(val/fact), repr(val%biginc)) if near_nuff(val, biginc) or val == 0.0: ticend = ticstart + (dir*bigtic) font=bigfont printed += 1 elif near_nuff(val, medinc) or medinc == biginc: ticend = ticstart + (dir*medtic) font = medfont printed += 1 else: ticend = ticstart + (dir*smalltic) font = medfont if printsmalls: printed += 1 if unit in ['dec', 's', 'us', 'ns']: ticstr = pretty(float(val)/fact, min_places)[0] else: ticstr = time_pretty(float(val)/fact, unit) if dir == 1: # x-axis LINE(pos, ticstart, pos, ticend, fill=self.fgcol, width=width) TEXT(pos, ticend+2, text=ticstr, fill=self.fgcol, anchor = N, font=font) else: LINE(ticstart, pos, ticend, pos, fill=self.fgcol, width=width, tag='yt') TEXT(ticend-2, pos, text=ticstr, fill=self.fgcol, anchor = E, font=font, tag='yt') pos = pos+(dir*axinc) val = val+smallinc printsmalls += 1 passes += 1 return lab ############################################################################### def draw(self): # print 'top is %d' % (self.plot.is_top[-1]) topset = None for s in self.sets: #print '#%d %d %d' % (s.indx, s.curr_indices[-1][0], s.curr_indices[-1][1]) #print 'Set', s if s.indx == self.plot.is_top[-1] \ and s.curr_indices[-1][1] >= s.curr_indices[-1][0]: topset = s #print 'topset=%d' % (s.indx) continue if s.curr_indices[-1][1] >= s.curr_indices[-1][0] and not s.hidden[-1]: self.draw_set(s) #print 'drawing set %d' % (s.indx) self.order.append(s.indx) #print 'append %d' % (s.indx) if topset: indx = topset.indx self.draw_set(topset) self.order.append(indx) # print 'drawing topset %d' % (indx) else: if self.order: indx = self.order[-1] else: indx = 0 self.plot.topset.set(indx) self.plot.is_top[-1] = indx self.draw_decs() ############################################################################### def draw_decs(self): for d in self.plot.decs: type = d.type if type == XLINE: for x in d.coords: if self.xmin < x < self.xmax: x -= self.xmin xc = x*self.xscalef + self.xorg self.canv.create_line(xc, self.yorg, xc, self.yorg-self.yht, fill=d.col, width=1, tags=('decs')) elif type == YLINE: for y in d.coords: if self.ymin < y < self.ymax: y -= self.ymin yc = self.yorg - y*self.yscalef self.canv.create_line(self.xorg, yc, self.xorg+self.xwi, yc, fill=d.col, width=1, tags=('decs')) ############################################################################### def undraw_decs(self): dels = self.canv.find_withtag('decs') for d in list(dels): self.canv.delete(d) ############################################################################### def redraw_decs(self): self.undraw_decs() self.draw_decs()############################################################################## def get_bktsz(self, set): if set.bktsz: return set.bktsz else: start = (set.data[-1][0] - set.data[0][0])/len(set.data) last = set.data[0][0] for p in set.data[1:]: start = min(start, p[0]-last) last = p[0] set.bktsz = start return start ############################################################################### def draw_set(self, set): # print 'draw-set', set.indx, set.hidden if set.hidden[-1]: return # print 'in draw set' i1, i2 = set.curr_indices[-1] #print 'i1=' + repr(i1) + ' i2=' + repr(i2) if i1 == None: return #col = 'blue' style = self.style bounds = self.bounds xmin = self.xmin ymin = self.ymin xscalef = self.xscalef yscalef = self.yscalef xorg = self.xorg yorg = self.yorg ytop = self.bounds[2] xtop = self.bounds[1] collist = self.plot.collist clen = len(collist) LINE = self.canv.create_line #if self.mode & (MODE_HIST | MODE_PDF): if self.mode & MODE_HIST: width = max(1, abs(int((self.get_bktsz(set)*xscalef) - 2))) if self.mode & MODE_RANGES: lshift = width/2 + 4 else: lshift = 0 else: width = self.plotlinewidth lshift = 0 s = set col = s.col try: tag = s.tg except AttributeError: # only one set tag = '' data = s.data i1, i2 = s.curr_indices[-1] #print 'i1=' + repr(i1) + ' i2=' + repr(i2) if i1 == None: return lastx = None #print 'xorg=%d yorg=%d' % (self.xorg, self.yorg) # connect to previous point if (style & STYLE_LINES and i1 != 0): xv = data[i1-1][0] - xmin yv = data[i1-1][1] - ymin lastx = int(xv*xscalef)+xorg lasty = yorg - int(yv*yscalef) # drawing error bars? eb = 0 if (style & STYLE_EBARS): if 'SD' in s.fields or 'SE' in s.fields: if style == STYLE_EBARS_SD: ebdi = s.fields.index('SD') elif style == STYLE_EBARS_SE: ebdi = s.fields.index('SE') if len(data[0]) > ebdi: eb = 1 else: style = STYLE_POINTS for p in data[i1:i2+1]: xv = p[0] - xmin yv = p[1] - ymin x = int(xv*xscalef)+xorg y = yorg - int(yv*yscalef) if col == None: contag = p[2] thiscol = collist[int(contag%clen)] else: thiscol = col if (style & STYLE_LINES): if lastx != None: try: x0, y0, x1, y1 = endpoints(lastx, lasty, x, y, bounds) LINE(x0, y0, x1, y1, fill=thiscol, width=width, tags=(tag, 'li')) LINE(x,y,x,y, fill=thiscol, tags=(tag, 'li', 'ctr')) except Nodraw: pass lastx = x lasty = y if (style & STYLE_POINTS) and inside(x, y, bounds): LINE(x-2,y,x+2,y,fill=thiscol, width=width, tags=(tag, 'pnt')) pp = LINE(x,y-2,x,y+2,fill=thiscol, width=width, tags=(tag, 'pnt')) LINE(x,y,x,y, fill=thiscol, tags=(tag, 'pnt', 'ctr')) if style == STYLE_BARS: x -= lshift if x - (width/2) > xtop: break y = MIN(MAX(y, ytop), yorg) thiswidth = width while x-(thiswidth/2) < xorg: x += 1 thiswidth -= 2 while x+(thiswidth/2) > xtop: x -= 1 thiswidth -= 2 pp = LINE(x, yorg, x, y, fill=thiscol, width=thiswidth, tags=('bar', tag)) LINE(x,y,x,y, fill=thiscol, tags=(tag, 'bar', 'ctr')) if (style & STYLE_EBARS) and eb: ebl = int(p[ebdi]*yscalef) ytop = y+ebl ybott = y-ebl LINE(x,ytop,x,ybott,fill=thiscol, width=width, tags=(tag, 'eb')) LINE(x-2,ytop,x+2,ytop,fill=thiscol, width=width, tags=(tag, 'eb')) LINE(x-2,y,x+2,y,fill=thiscol, width=width, tags=(tag, 'eb')) LINE(x-2,ybott,x+2,ybott,fill=thiscol, width=width, tags=(tag, 'eb')) LINE(x,y,x,y, fill=thiscol, tags=(tag, 'eb', 'ctr')) # connect to following point if (style & STYLE_LINES and i2 != s.len-1): xv = data[i2+1][0] - xmin yv = data[i2+1][1] - ymin x = int(xv*xscalef)+xorg y = yorg - int(yv*yscalef) contag = data[i2+1][2] thiscol = collist[int(contag%clen)] try: x0, y0, x1, y1 = endpoints(lastx, lasty, x, y, bounds) LINE(x0, y0, x1, y1, fill=thiscol, width=width, tags=(tag, 'li')) except Nodraw: pass############################################################################## def undraw_set(self, set): # print 'undraw_set', set.indx, set.tg t = set.tg c = self.canv dels = c.find_withtag(t) for d in list(dels): c.delete(d) ############################################################################## #############################################################################class np_Plot: def __init__(self, sets, path, standalone='yes', title = '', xlab='', ylab='', raw_data=0, showlist=None, top=None, style=STYLE_POINTS, mode=MODE_TS, width=B_WI, height=B_HT, delay_activation='no', decorations=[], zero_origin=1): # print 'plot' self.path = path self.standalone = standalone self.wi = width self.ht = height if standalone == 'yes': self.root = Tk() else: self.root = Toplevel() self.root.geometry('%dx%d' % (width, height)) self.root.minsize(width, height) self.root.maxsize(width, height) #self.root.grab_set() tstr = 'np_plot ' + path + ' - ' + title self.root.winfo_toplevel().title(tstr) # remember stuff between loading functions self.fnspec = None self.raw_data = raw_data self.sets = sets self.dtype = sets[0].type self.showlist = showlist self.make_collist() posstop = 0 # activation flags for style errorbars self.ebse = DISABLED self.ebsd = DISABLED for s in sets: s.initialise() s.indx = sets.index(s) if s.tag != None: s.col = self.collist[s.tag%len(self.collist)] else: s.col = None if showlist and not showlist.count(s.indx): s.hidden = [1] else: s.hidden = [0] posstop = s.indx if 'SE' in s.fields: self.ebse = NORMAL if 'SD' in s.fields: self.ebsd = NORMAL if top == None: top = posstop # remember stuff between loading sets self.add_set_def_fields = self.sets[0].fields self.add_set_fields = [] self.add_set_dir = os.path.dirname(path) if not self.add_set_dir: self.add_set_dir = os.getcwd() self.topset = IntVar() self.topset.set(top) self.is_top = [top] self.newtop = top self.delset = IntVar() self.delset.set(-1) self.title = title self.sspec = '' self.xlab = xlab self.ylab = ylab self.style = style self.mode = mode self.raisefn = self.raiseit self.hidefn = self.hideit self.scolfn = self.scol #
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -