def draw(self, device): device.pushContext(self.context) world = self.container.getWorld() matrix = kaplot.Matrix.scalebox_inverse(*world) p1 = kaplot.Vector(matrix * (0,0)) p2 = kaplot.Vector(matrix * (1,0)) p3 = kaplot.Vector(matrix * (0,1)) p4 = kaplot.Vector(matrix * (1,1)) plist = [p1, p2, p3, p4] x1 = min([p.x for p in plist]) x2 = max([p.x for p in plist]) y1 = min([p.y for p in plist]) y2 = max([p.y for p in plist]) x, xsub = kaplot.utils.subdivide(x1, x2, subticks=self.xsubticks, interval=self.xinterval, start=self.xstart, integer=self.xinteger, logarithmic=self.xlogarithmic) y, ysub = kaplot.utils.subdivide(y1, y2, subticks=self.ysubticks, interval=self.yinterval, start=self.ystart, integer=self.yinteger, logarithmic=self.ylogarithmic) if self.subgrid: xlist = xsub ylist = ysub else: xlist = x ylist = y for x in xlist: device.drawLine(x, y1, x, y2, gridsnap=True) for y in ylist: device.drawLine(x1, y, x2, y, gridsnap=True) device.popContext()
def _getXticks(self, world): (wx1, wy1), (wx2, wy2) = self.container.getWorld() world = self.container.getWorld() matrix = kaplot.Matrix.scalebox_inverse(*world) wp1 = matrix * self.p1 wp2 = matrix * self.p2 (wcsx1, wcsy1), (wcsx2, wcsy2) = self.container.getRange( self.wcs, self.transformation) wcsxlist, _ = self._getXticks2(wcsx1, wcsx2) wcsylist, __ = self._getXticks2(wcsy1, wcsy2) xticks = [] xvalues = [] wcs = self.wcs step = (wcsy2 - wcsy1) / self.N wcsyrange = arange(wcsy1, wcsy2 + step / 2., step) r = lambda x, y: self.transformation.reverse(x, y) for wcsx in wcsxlist: for wcsy1, wcsy2 in zip(wcsyrange[:-1], wcsyrange[1:]): p1 = wcs.to_pixel(*r(rnd(wcsx), rnd(wcsy1))) p2 = wcs.to_pixel(*r(rnd(wcsx), rnd(wcsy2))) if p1 is not None and p2 is not None: w1 = kaplot.Vector(p1) w2 = kaplot.Vector(p2) p = self.lineintersection(wp1, wp2, w1, w2) if p: #if (w1.y < wy1 and w2.y >= wy1) or (w2.y < wy1 and w1.y >= wy1): # todo, intersection xticks.append(p.x) xvalues.append(wcsx) #print xticks return array(xticks), array(xvalues), []
def getEdgeValues(self, world): matrix = kaplot.Matrix.scalebox_inverse(*world) wp1 = kaplot.Vector(matrix * self.p1) wp2 = kaplot.Vector(matrix * self.p2) if self.intersection == "x": return wp1.x, wp2.x else: return wp1.y, wp2.y
def draw(self, device): device.pushContext(self.context) size, units = kaplot.utils.splitDimension(self.offset) dy = self.y2 - self.y1 dx = self.x2 - self.x1 dxu, dyu = self.container.worldToSize(dx, dy, units) angle = math.atan2(dyu, dxu) #print "angle =", math.degrees(angle), dxu, dyu if self.halign is None: halign = kaplot.utils.getHalign(angle, 0) else: halign = self.halign if self.valign is None: valign = kaplot.utils.getValign(angle, 0) else: valign = self.valign #print halign, valign device.drawLine(self.x1, self.y1, self.x2, self.y2) d = kaplot.Vector(dxu, dyu) d = d.scale(size/d.length, size/d.length) #s = 1./d.length * size #print d, d.length, size #offset = kaplot.Matrix.scalebox_inverse(*self.container.getWorld()).no_translation() * d.scale(size, size) offsetx = self.container.sizeToWorld("%f%s" % (d.x, units)).x offsety = self.container.sizeToWorld("%f%s" % (d.y, units)).y #print "--", self.x2, self.y2 #print "offset", offsetx, offsety device.drawText(self.text, self.x2 + offsetx, self.y2 + offsety, halign, valign) device.popContext()
def getBBox(self): height, width = self.data2d.shape #p1, p2 = (0, 0), (width, height) #offset = kaplot.Vector(-0.5, -0.5) #p1, p2 = (0.5, 0.5), (width+0.5, height+0.5) p1, p2 = (0.5, 0.5), (width+0.5, height+0.5) offset = kaplot.Vector(-0.0, -0.0) return self.matrix * p1 + offset, self.matrix * p2 + offset
def viewportToSize(self, x, y, units="mm", viewport=((0, 0), (1, 1))): #size, units = kaplot.utils.splitDimension(size) x, y = kaplot.Matrix.scalebox_inverse(*viewport) * (x, y) px = x * (self.pixelWidth - 1) py = y * (self.pixelHeight - 1) sx = kaplot.utils.convertPixelsTo(px, units, dpi=self.dpi) sy = kaplot.utils.convertPixelsTo(py, units, dpi=self.dpi) return kaplot.Vector(sx, sy)
def subdiv(p1, p2, n=10): p1 = self.wcs.from_pixel(p1[0], p1[1]) p2 = self.wcs.from_pixel(p2[0], p2[1]) if not(p1 and p2): return [], [] p1 = kaplot.Vector(p1) p2 = kaplot.Vector(p2) x = numpy.zeros(n) + 0.0 y = numpy.zeros(n) + 0.0 v = kaplot.Vector(p2) - kaplot.Vector(p1) nminus1 = n - 1 for i in xrange(n): p = p1 + v.scale(float(i)/nminus1) pp = self.wcs.to_pixel(p.x, p.y) x[i] = pp[0] y[i] = pp[1] return (x, y)
def draw(self, device): device.pushContext(self.context) (wx1, wy1), (wx2, wy2) = self.container.getWorld() (wcsx1, wcsy1), (wcsx2, wcsy2) = self.getWcsRange() wcsxlist = kaplot.utils.subdivide(wcsx1, wcsx2, interval=self.xinterval, integer=self.xinteger, start=self.xstart) wcsylist = kaplot.utils.subdivide(wcsy1, wcsy2, interval=self.yinterval, integer=self.yinteger, start=self.ystart) #wcsxlist = range(-10, 10) #wcsylist = range(-10, 10) wcs = self.container.wcs step = (wcsx2 - wcsx1) / self.N wcsxrange = arange(wcsx1, wcsx2 + step / 2., step) step = (wcsy2 - wcsy1) / self.N wcsyrange = arange(wcsy1, wcsy2 + step / 2., step) r = lambda x, y: self.transformation.reverse(x, y) for wcsy in wcsylist: for wcsx1, wcsx2 in zip(wcsxrange[:-1], wcsxrange[1:]): p1 = wcs.to_pixel(*r(rnd(wcsx1), rnd(wcsy))) p2 = wcs.to_pixel(*r(rnd(wcsx2), rnd(wcsy))) if p1 is not None and p2 is not None: w1 = kaplot.Vector(p1) w2 = kaplot.Vector(p2) device.drawLine(w1.x, w1.y, w2.x, w2.y) #print (w1 + w2).length for wcsx in wcsxlist: for wcsy1, wcsy2 in zip(wcsyrange[:-1], wcsyrange[1:]): p1 = wcs.to_pixel(*r(rnd(wcsx), rnd(wcsy1))) p2 = wcs.to_pixel(*r(rnd(wcsx), rnd(wcsy2))) if p1 is not None and p2 is not None: w1 = kaplot.Vector(p1) w2 = kaplot.Vector(p2) #device.drawLine(rnd(w1.x), rnd(w1.y), rnd(w2.x), rnd(w2.y)) device.drawLine(w1.x, w1.y, w2.x, w2.y) #print rnd(w1.x), rnd(w1.y), rnd(w2.x), rnd(w2.y) #print wcsx, wcsy1, wcsy2 #print device.popContext()
def draw(self, device): device.pushContext(self.context) document = self.getDocument() width, height = document.sizeToViewport(self.size, device.getViewport()) width, height = device.getWorldMatrix().no_translation().inverse() * kaplot.Vector(width, height) hwidth = width #/ 200 hheight = height #/ 200 xneg, xpos = None, None if self.xerr is not None: xneg = self.xerr xpos = self.xerr if self.xpos is not None: xpos = self.xpos if self.xneg is not None: xneg = self.xneg if xneg is not None and xpos is not None: for x, y, xneg, xpos in zip(self.x, self.y, xneg, xpos): device.drawLine(x-xneg, y, x+xpos, y) device.drawLine(x-xneg, y-hheight, x-xneg, y+hheight) device.drawLine(x+xpos, y-hheight, x+xpos, y+hheight) elif xneg is not None: for x, y, xneg in zip(self.x, self.y, xneg): device.drawLine(x-xneg, y, x, y) device.drawLine(x-xneg, y-hheight, x-xneg, y+hheight) elif xpos is not None: for x, y, xpos in zip(self.x, self.y, xpos): device.drawLine(x, y, x+xpos, y) device.drawLine(x+xpos, y-hheight, x+xpos, y+hheight) yneg, ypos = None, None if self.yerr is not None: yneg = self.yerr ypos = self.yerr if self.ypos is not None: ypos = self.ypos if self.yneg is not None: yneg = self.yneg if yneg is not None and ypos is not None: for x, y, y1, y2 in zip(self.x, self.y, yneg, ypos): device.drawLine(x, y-y1, x, y+y2) device.drawLine(x-hwidth, y-y1, x+hwidth, y-y1) device.drawLine(x-hwidth, y+y2, x+hwidth, y+y2) elif yneg is not None: for x, y, y1 in zip(self.x, self.y, yneg): device.drawLine(x, y-y1, x, y) device.drawLine(x-hwidth, y-y1, x+hwidth, y-y1) elif ypos is not None: for x, y, y2 in zip(self.x, self.y, ypos): device.drawLine(x, y, x, y+y2) device.drawLine(x-hwidth, y+y2, x+hwidth, y+y2) device.popContext()
def sizeToViewport(self, size, viewport=((0, 0), (1, 1))): size, units = kaplot.utils.splitDimension(size) sizePixels = kaplot.utils.convertToPixels(float(size), units, dpi=self.dpi) (x1, y1), (x2, y2) = viewport width = float(x2 - x1) height = float(y2 - y1) sizeX = sizePixels / (self.pixelWidth - 1) / width sizeY = sizePixels / (self.pixelHeight - 1) / height return kaplot.Vector(sizeX, sizeY)
def _getYticks(self, world): #(wx1, wy1), (wx2, wy2) = self.container.getWorld() world = self.container.getWorld() matrix = kaplot.Matrix.scalebox_inverse(*world) wp1 = matrix * self.p1 wp2 = matrix * self.p2 (wcsx1, wcsy1), (wcsx2, wcsy2) = self.container.getRange( self.wcs, self.transformation) wcsxlist, _ = self._getXticks2(wcsx1, wcsx2) wcsylist, __ = self._getXticks2(wcsy1, wcsy2) yticks = [] yvalues = [] wcs = self.wcs step = (wcsx2 - wcsx1) / self.N wcsxrange = arange(wcsx1, wcsx2 + step / 2., step) #print self.container.getWorld() #print self.container.getRange() r = lambda x, y: self.transformation.reverse(x, y) for wcsy in wcsylist: for wcsx1, wcsx2 in zip(wcsxrange[:-1], wcsxrange[1:]): p1 = wcs.to_pixel(*r(rnd(wcsx1), rnd(wcsy))) p2 = wcs.to_pixel(*r(rnd(wcsx2), rnd(wcsy))) if p1 is not None and p2 is not None: w1 = kaplot.Vector(p1) w2 = kaplot.Vector(p2) #sprint w1.x, w2.x, wx1 p = self.lineintersection(wp1, wp2, w1, w2) if p: #if (w1.x < wx1 and w2.x >= wx1) or (w2.x < wx1 and w1.x >= wx1): # todo, intersection #import pdb; pdb.set_trace() #print "tick", w1.y, rnd(w1.y), wcsx1, wcsx2 yticks.append(p.y) #+(w2.x-w1.x)/2) yvalues.append(wcsy) #print "yticks", yticks return array(yticks), array(yvalues), []
def __getBBox(self): height, width = self.data2d.shape p1, p2 = (0, 0), (width, height) #offset = kaplot.Vector(0.5, 0.5) offset = kaplot.Vector(0, 0) #matrix = \ # kaplot.Matrix.translate(-0.5, -0.5) *\ # self.matrix *\ # kaplot.Matrix.translate(0.5, 0.5) height,width = self.data2d.shape (x1, y1), (x2, y2) = self.resize #print "~~~", resize, width, height #kaplot.Matrix.translate(-0.5, -0.5) *\ matrix = \ kaplot.Matrix.translate(x1, y1) *\ kaplot.Matrix.scale((x2-x1)/float(width+1), (y2-y1)/float(height+1)) *\ kaplot.Matrix.translate(0.5, 0.5) return matrix * p1 + offset, matrix * p2 + offset
def draw(self, device): device.pushContext(self.context) granularity = 2 # 'horizontal' lines breakra = [] if self.wcs.divergent: declist = range(-90+15-1, 92-15+1, 15) #ralist = range(-180, 181, 1) # +? else: declist = range(-90, 92, 15) ralist = range(-180, 180+granularity+1, granularity) # +? #declist = range(-90, 90, 15) sigma = 0.0001 cracklimit = 5.0 cracklimitlarge = 30.0 for dec in declist: #break xlist = [] ylist = [] decorg = dec for ra1org, ra2org in zip(ralist[:-1], ralist[1:]): if self.transformation: ra1, dec1 = self.transformation.reverse(ra1org, decorg) ra2, dec2 = self.transformation.reverse(ra2org, decorg) else: dec1 = decorg dec2 = decorg ra1 = ra1org ra2 = ra2org p1 = self.wcs.to_pixel(ra1, dec1) p2 = self.wcs.to_pixel(ra1+sigma, dec1) p3 = self.wcs.to_pixel(ra2-sigma, dec2) p4 = self.wcs.to_pixel(ra2, dec2) if not(p1 and p2 and p3 and p4): device.drawPolyLine(xlist, ylist) xlist, ylist = [], [] else: #if False and self.transformation: # p1 = self.transformation(p1[0], p1[1]) # p2 = self.transformation(p2[0], p2[1]) # p3 = self.transformation(p3[0], p3[1]) # p4 = self.transformation(p4[0], p4[1]) p1 = kaplot.Vector(p1) p2 = kaplot.Vector(p2) p3 = kaplot.Vector(p3) p4 = kaplot.Vector(p4) xlist.append(p1.x) ylist.append(p1.y) if (p2-p1).length > cracklimit: # crack in the first bit device.drawPolyLine(xlist, ylist) xlist, ylist = [], [] breakra.append(ra1org) elif (p4-p3).length > cracklimit: # crack at the end xlist.append(p3.x) ylist.append(p3.y) device.drawPolyLine(xlist, ylist) xlist, ylist = [p4.x], [p4.y] breakra.append(ra1org) breakra.append(ra2org) elif (p1-p4).length > cracklimitlarge: device.drawPolyLine(xlist, ylist) xlist, ylist = [p4.x], [p4.y] breakra.append(ra1org) breakra.append(ra2org) device.drawPolyLine(xlist, ylist) # 'vertical' lines if self.wcs.divergent: declist = range(-90+15, 92-15+granularity, granularity) #ralist = range(-180, 181, 1) # +? else: declist = range(-90, 90+granularity, granularity) #declist = range(-90, 90+1, 1) ralist = range(-180, 180+1, 15) # +? lines = [] for ra in ralist: xlist = [] ylist = [] #ra = ((ra +180) % 360) - 180 raorg = ra # we don't want to draw the same line twice, so we do a # check to see if it's on the same location # this happens because we go from -180 to 180, which mostly # overlaps, but not always! line = [] for dec in [-80, 45, 0, 45, 80]: if self.transformation: ra, dec = self.transformation.reverse(raorg, dec) result = self.wcs.to_pixel(ra, dec) if result: x, y = result line.append(x) line.append(y) line = numpy.array(line, numpy.Float) found = False for prevline in lines: if len(line) > 0 and \ len(line) == len(prevline) and \ max(abs(prevline - line)) < 0.01: found = True break if found: continue lines.append(line) # end of line check for dec1, dec2 in zip(declist[:-1], declist[1:]): if self.transformation: ra1, dec1 = self.transformation.reverse(raorg, dec1) ra2, dec2 = self.transformation.reverse(raorg, dec2) else: ra1 = raorg ra2 = raorg p1 = self.wcs.to_pixel(ra1, dec1) p2 = self.wcs.to_pixel(ra1, dec1+sigma) p3 = self.wcs.to_pixel(ra2, dec2-sigma) p4 = self.wcs.to_pixel(ra2, dec2) if not(p1 and p2 and p3 and p4): device.drawPolyLine(xlist, ylist) xlist, ylist = [], [] else: #if False and self.transformation: # p1 = self.transformation(p1[0], p1[1]) # p2 = self.transformation(p2[0], p2[1]) # p3 = self.transformation(p3[0], p3[1]) # p4 = self.transformation(p4[0], p4[1]) p1 = kaplot.Vector(p1) p2 = kaplot.Vector(p2) p3 = kaplot.Vector(p3) p4 = kaplot.Vector(p4) xlist.append(p1.x) ylist.append(p1.y) if (p2-p1).length > cracklimit: # crack in the first bit device.drawPolyLine(xlist, ylist) xlist, ylist = [], [] elif (p4-p3).length > cracklimit: # crack at the end xlist.append(p3.x) ylist.append(p3.y) device.drawPolyLine(xlist, ylist) xlist, ylist = [p4.x], [p4.y] xlist, ylist = [], [] elif (p1-p4).length > cracklimitlarge: device.drawPolyLine(xlist, ylist) xlist, ylist = [p4.x], [p4.y] xlist, ylist = [], [] device.drawPolyLine(xlist, ylist) device.popContext()
def windowToViewport(self, x, y, viewport=((0, 0), (1, 1))): v = kaplot.Vector( float(x) / (self.pixelWidth - 1), float(y) / (self.pixelHeight - 1)) return kaplot.Matrix.scalebox_inverse(*viewport).inverse() * v
def _plot(self, device): device.pushContext(self.context) ralist = range(-180, 181, 15) declist = range(-90, 91, 15) import numpy for ra1, ra2 in zip(ralist[:-1], ralist[1:]): for dec1, dec2 in zip(declist[:-1], declist[1:]): sigma = 0.0001 def subdiv(p1, p2, n=10): p1 = self.wcs.from_pixel(p1[0], p1[1]) p2 = self.wcs.from_pixel(p2[0], p2[1]) if not(p1 and p2): return [], [] p1 = kaplot.Vector(p1) p2 = kaplot.Vector(p2) x = numpy.zeros(n) + 0.0 y = numpy.zeros(n) + 0.0 v = kaplot.Vector(p2) - kaplot.Vector(p1) nminus1 = n - 1 for i in xrange(n): p = p1 + v.scale(float(i)/nminus1) pp = self.wcs.to_pixel(p.x, p.y) x[i] = pp[0] y[i] = pp[1] return (x, y) p1 = self.wcs.to_pixel(ra1+sigma, dec1+sigma) p2 = self.wcs.to_pixel(ra2-sigma, dec1+sigma) p3 = self.wcs.to_pixel(ra2-sigma, dec2-sigma) p4 = self.wcs.to_pixel(ra1+sigma, dec2-sigma) if p1 and p2: x, y = subdiv(p1, p2) device.drawPolyLine(x, y) if p2 and p3: x, y = subdiv(p2, p3) device.drawPolyLine(x, y) #device.plotLine(p2, p3) if p3 and p4: x, y = subdiv(p3, p4) device.drawPolyLine(x, y) #device.plotLine(p3, p4) if p4 and p1: x, y = subdiv(p4, p1) device.drawPolyLine(x, y) #device.plotLine(p4, p1) if False: p1 = self.projection.forward(ra1+sigma, dec1+sigma) p2 = self.projection.forward(ra1+sigma, dec2-sigma) if p1 and p2: device.plotLine(p1, p2) v1 = kaplot.Vector(p1) v2 = kaplot.Vector(p2) if (v1-v2).length > 120: print "jump down", ra1, dec1, "to", ra1, dec2 import pdb #pdb.set_trace() p1 = self.projection.forward(ra1-sigma, dec1-sigma) p2 = self.projection.forward(ra1-sigma, dec2-sigma) if p1 and p2: device.plotLine(p1, p2) p1 = self.projection.forward(ra1+sigma, dec1+sigma) p2 = self.projection.forward(ra2-sigma, dec1+sigma) if p1 and p2: device.plotLine(p1, p2) if (v1-v2).length() > 120: print "jump right", ra1, dec1, "to", ra2, dec1 import pdb #pdb.set_trace() device.popContext()
def _getYticks(self, world): matrix = kaplot.Matrix.scalebox_inverse(*world) #.inverse() v1 = kaplot.Vector(matrix * (0, 0)).y v2 = kaplot.Vector(matrix * (0, 1)).y t, sub = self._getYticks2(v1, v2) return t, t, sub
def _getXticks(self, world): matrix = kaplot.Matrix.scalebox_inverse(*world) v1 = kaplot.Vector(matrix * (0, 0)).x v2 = kaplot.Vector(matrix * (1, 0)).x t, sub = self._getXticks2(v1, v2) return t, t, sub
def viewportToWindow(self, x, y, viewport=((0, 0), (1, 1))): x, y = kaplot.Matrix.scalebox_inverse(*viewport) * (x, y) v = kaplot.Vector( float(x) * (self.pixelWidth - 1), float(y) * (self.pixelHeight - 1)) return v
def __init__(self, container, location="left", interval=None, integer=False, start=None, ticks=4, subticks=3, logarithmic=False, ticklength="3mm", labeloffset="1mm", linestyle="normal", linewidth="1px", intersects=None, halign=None, valign=None, intersection=None, spacing="1mm", **kwargs): super(Axis, self).__init__(container, linestyle=linestyle, linewidth=linewidth, **kwargs) self.interval = interval self.integer = integer self.start = start self.ticks = ticks self.subticks = subticks self.logarithmic = logarithmic self.intersection = intersection if location == "bottom": self.p1 = kaplot.Vector(0, 0) self.p2 = kaplot.Vector(1, 0) self.halign = "center" self.valign = "top" self.tickdir = kaplot.Vector(0, -1) if intersection is None: self.intersection = "x" elif location == "right": self.p1 = kaplot.Vector(1, 0) self.p2 = kaplot.Vector(1, 1) self.halign = "left" self.valign = "center" self.tickdir = kaplot.Vector(1) if intersection is None: self.intersection = "y" elif location == "top": self.p1 = kaplot.Vector(1, 1) self.p2 = kaplot.Vector(0, 1) self.halign = "center" self.valign = "bottom" self.tickdir = kaplot.Vector(0, 1) if intersection is None: self.intersection = "x" elif location == "left": self.p1 = kaplot.Vector(0, 1) self.p2 = kaplot.Vector(0, 0) self.halign = "right" self.valign = "center" self.tickdir = kaplot.Vector(-1, 0) if intersection is None: self.intersection = "y" else: raise Exception, "unkown location", location self.location = location self.ticklength = ticklength self.labeloffset = labeloffset self.spacing = spacing self.bounds = (0., 0.), (0., 0.) self.drawLabels = True
def rndv(v): x, y = v return kaplot.Vector(rnd(x), rnd(y))
def draw(self, device): device.pushContext(self.context) hsize, vsize = self.size.split(",") hsize, vsize = hsize.strip(), vsize.strip() hsizex = self.getDocument().sizeToViewport(hsize, self.container.borderViewport).x vsizey = self.getDocument().sizeToViewport(vsize, self.container.borderViewport).y size = kaplot.Vector(hsizex, vsizey) edgespacing = self.getDocument().sizeToViewport(self.edgespacing, self.container.borderViewport) hloc, vloc = self.location.split(",") hloc, vloc = hloc.strip(), vloc.strip() hlocs = {"left":0+edgespacing.x, "center":0.5-size.x/2., "right":1-size.x-edgespacing.x} vlocs = {"bottom":0+size.y+edgespacing.y, "center":0.5+size.y/2., "top":1-edgespacing.y} if hloc not in hlocs: raise Exception, "horizontal location should be 'right', 'left' or 'center', not %r" % hloc if vloc not in vlocs: raise Exception, "vertical location should be 'top', 'bottom' or 'center', not %r" % hloc origin = kaplot.Vector(hlocs[hloc], vlocs[vloc]) device.pushWorld(((0, 0), (1,1))) #print origin #print size #print edgespacing colorbarimage = kaplot.utils.createColorbar(self.direction, 256) height, width = colorbarimage.shape #viewport = (origin.x, origin.y-size.y), (origin.x+size.y, origin.y) #m = kaplot.Matrix.scalebox_inverse(*viewport) * device.getViewportMatrix() #m = device.getViewportMatrix().inverse() * kaplot.Matrix.scalebox_inverse(*viewport) #vp = m * (0, 0), m * (1, 1) #m = kaplot.Matrix.translate(origin.x, origin.y) * \ m = kaplot.Matrix.translate(origin.x, origin.y-size.y) *\ kaplot.Matrix.scale(size.x/width, size.y/height) *\ kaplot.Matrix.translate(0.5, 0.5) # * \ #kaplot.Matrix.translate(0.5, 0.5) #kaplot.Matrix.translate(-0.5, -0.5) *\ #print origin.x, origin.y-size.y #print height, width #device.pushWorld(((0.5, 0.5), (width+0.5, height+0.5))) #device.pushViewport(vp) device.drawIndexedImage(colorbarimage, self.colormap, matrix=m) if self.direction in ["right", "left"]: device.pushContext({"color":self.markercolor}) for marker in self.markers: x = (marker - self.datamin) / (self.datamax - self.datamin) device.drawLine(origin.x + x*size.x, origin.y, origin.x+x*size.x, origin.y-size.y) print "MARKER", marker, self.datamin, self.datamax, x device.popContext() xlist = [origin.x, origin.x + size.x, origin.x + size.x, origin.x] ylist = [origin.y, origin.y, origin.y - size.y, origin.y - size.y] device.drawPolyLine(xlist, ylist, close=True, gridsnap=True) #device.popViewport() #device.popWorld() ticksize = "3mm" labeloffset = "1mm" ticksizevp = self.getDocument().sizeToViewport(ticksize, self.container.borderViewport) labeloffsetvp = self.getDocument().sizeToViewport(labeloffset, self.container.borderViewport) #dmin = self.image.data2d.min() #dmax = self.image.data2d.max() #self.levels = [dmin, dmin + (dmax-dmin)/2., dmax] levels = numpy.array(self.levels, dtype=float) if self.direction in ["right", "left"]: if self.labelposition == "top": for level in list(levels): x = (level - self.datamin) / (self.datamax - self.datamin) if x >= 0 and x <= 1: nx = origin.x+x*size.x if self.direction == "left": nx = 1 - nx device.drawLine(nx, origin.y, nx, origin.y-ticksizevp.y) device.drawText("%g" % level, nx, origin.y-max(0, -ticksizevp.y)+labeloffsetvp.y, "center", "bottom") if self.label: device.drawText(self.label, origin.x + size.x / 2, origin.y-size.y-labeloffsetvp.y, halign="center", valign="top") if self.labelposition == "bottom": for level in list(levels): x = (level - self.datamin) / (self.datamax - self.datamin) if x >= 0 and x <= 1: nx = origin.x+x*size.x if self.direction == "left": nx = 1 - nx device.drawLine(nx, origin.y-size.y, nx, origin.y+ticksizevp.y-size.y) device.drawText("%g" % level, nx, origin.y-size.y+max(0, -ticksizevp.y)-labeloffsetvp.y, "center", "top") if self.label: device.drawText(self.label, origin.x + size.x / 2, origin.y+labeloffsetvp.y, halign="center", valign="bottom") if self.direction in ["up", "down"]: if self.labelposition == "left": for level in list(levels): y = (level - self.datamin) / (self.datamax - self.datamin) if y >= 0 and y <= 1: ny = origin.y-size.y*(1-y) if self.direction == "down": ny = 1 - ny device.drawLine(origin.x, ny , origin.x+ticksizevp.x, ny) device.drawText("%g" % level, origin.x-max(0, -ticksizevp.x)-labeloffsetvp.x, ny, "right", "center") if self.label: device.drawText(self.label, origin.x + size.x + labeloffsetvp.x, origin.y - size.y / 2, halign="center", valign="bottom", textangle=-numpy.pi/2) if self.labelposition == "right": for level in list(levels): y = (level - self.datamin) / (self.datamax - self.datamin) if y >= 0 and y <= 1: ny = origin.y-size.y*(1-y) if self.direction == "down": ny = 1 - ny device.drawLine(origin.x+size.x, ny, origin.x+size.x-ticksizevp.x, ny) device.drawText("%g" % level, origin.x+size.x+max(0, -ticksizevp.x)+labeloffsetvp.x, ny, "left", "center") if self.label: device.drawText(self.label, origin.x - labeloffsetvp.x, origin.y - size.y / 2, halign="center", valign="bottom", textangle=numpy.pi/2) device.popWorld() if False: colorbarimage = kaplot.utils.createColorbar(self.direction, 256) height, width = colorbarimage.shape m = kaplot.Matrix.scalebox_inverse(*viewport) * device.getViewportMatrix() m = device.getViewportMatrix() * kaplot.Matrix.scalebox_inverse(*viewport) vp = m * (0, 0), m * (1, 1) #device.pushViewport(vp) device.pushWorld(((0.5, 0.5), (width+0.5, height+0.5))) device.drawIndexedImage(colorbarimage, self.colormap) device.popWorld() clipping = device.getClipping() device.setClipping(False) device.pushWorld(((0, 0), (1, 1))) device.drawPolyLine([0, 1, 1, 0], [0, 0, 1, 1], close=True, gridsnap=True) ticksize = "3mm" ticksizevp = self.getPlot().sizeToViewport(ticksize, viewport) labeloffset = "1mm" labeloffsetvp = self.getPlot().sizeToViewport(labeloffset, viewport) ticksizevp = self.getPlot().sizeToViewport(ticksize, viewport) if self.direction in ["right", "left"]: if self.labelposition == "top": for level in list(self.levels) + [self.datamin, self.datamax]: x = (level - self.datamin) / (self.datamax - self.datamin) if x >= 0 and x <= 1: device.drawLine(x, 0, x, 1+ticksizevp.y) device.drawText("%g" % level, x, 1+ticksizevp.y+labeloffsetvp.y, "center", "bottom") elif self.direction in ["down", "up"]: if self.labelposition == "left": for level in list(self.levels) + [self.datamin, self.datamax]: y = (level - self.datamin) / (self.datamax - self.datamin) if y >= 0 and y <= 1: device.drawLine(-ticksizevp.x, y, 1, y) device.drawText("%g" % level, -ticksizevp.x-labeloffsetvp.x, y, "center", "bottom") device.setClipping(clipping) device.popWorld() device.popViewport() device.popContext()