From: <jd...@us...> - 2007-07-18 21:30:16
|
Revision: 3567 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3567&view=rev Author: jdh2358 Date: 2007-07-18 14:30:12 -0700 (Wed, 18 Jul 2007) Log Message: ----------- playing around with mpl1 api Modified Paths: -------------- trunk/matplotlib/mpl1/mpl1.py trunk/matplotlib/mpl1/mtraits.py Added Paths: ----------- trunk/matplotlib/mpl1/scratch.py trunk/matplotlib/mpl1/test.py Modified: trunk/matplotlib/mpl1/mpl1.py =================================================================== --- trunk/matplotlib/mpl1/mpl1.py 2007-07-18 20:38:32 UTC (rev 3566) +++ trunk/matplotlib/mpl1/mpl1.py 2007-07-18 21:30:12 UTC (rev 3567) @@ -1,5 +1,4 @@ -from matplotlib.enthought.traits import HasTraits -import matplotlib.enthought.traits as traits +import enthought.traits.api as traits from matplotlib import agg import numpy as npy @@ -54,7 +53,7 @@ identity = Identity() -class Path(HasTraits): +class Path(traits.HasTraits): """ The path is an object that talks to the backends, and is an intermediary between the high level path artists like Line and @@ -102,6 +101,7 @@ def color_to_rgba8(self, color): if color is None: return None rgba = [int(255*c) for c in color.r, color.g, color.b, color.a] + return agg.rgba8(*rgba) # coordinates: @@ -178,7 +178,7 @@ self.scanlinebin = agg.scanline_bin() def add_path(self, pathid, path): - pathid = Renderer.add_path(self, pathid, path) + Renderer.add_path(self, pathid, path) self.aggpathd[pathid] = AggPath(path) def remove_path(self, pathid): @@ -274,6 +274,7 @@ path.verts = model(X) path.codes = codes path.fillcolor = None + path.strokecolor = color path.strokewidth = linewidth path.alpha = alpha path.antialiased = antialiased @@ -281,43 +282,49 @@ -class AxesCoords(HasTraits): +class AxesCoords(traits.HasTraits): xviewlim = mtraits.interval yviewlim = mtraits.interval affineview = mtraits.affine affineaxes = mtraits.affine affine = mtraits.affine + def _affineview_changed(self, old, new): - self.affine = npy.dot( - npy.dot(self.affineaxes, new), self.affinedata) + print 'affine view changed' + self.affine = npy.dot(self.affineaxes, new) def _affineaxes_changed(self, old, new): - self.affine = npy.dot( - npy.dot(new, self.affineview), self.affinedata) + print 'affine axes changed' + self.affine = npy.dot(new, self.affineview) - + def _xviewlim_changed(self, old, new): + print 'xviewlim changed' xmin, xmax = new scale = 1./(xmax-xmin) tx = -xmin*scale self.affineview[0][0] = scale self.affineview[0][-1] = tx - + self.affine = npy.dot(self.affineaxes, self.affineview) + print '\t', self.affine + def _yviewlim_changed(self, old, new): + print 'yviewlim changed' ymin, ymax = new scale = 1./(ymax-ymin) ty = -ymin*scale self.affineview[1][1] = scale self.affineview[1][-1] = ty - + self.affine = npy.dot(self.affineaxes, self.affineview) + print '\t', self.affine class Figure: def __init__(self): self.renderer = None self._pathid = 0 - self._pathd = dict() + self.pathd = dict() def add_path(self, path): id_ = self._pathid @@ -336,6 +343,7 @@ raise RuntimeError('call set_renderer renderer first') for pathid, path in self.pathd.items(): + print 'path', pathid, path.affine renderer.push_affine(path.affine) renderer.render_path(pathid) @@ -374,25 +382,12 @@ [0,0,1]], dtype=npy.float_) -xlim = mtraits.interval() -ylim1 = mtraits.interval() -ylim2 = mtraits.interval() -affineaxes = affine_axes([0.1, 0.1, 0.4, 0.4]) # lower, left quadrant - coords1 = AxesCoords() -coords1.xlim = xlim -coords1.ylim = ylim1 -print 'typedata', affineaxes.shape, affineaxes.dtype -coords1.affineaxes = affineaxes +coords1.affineaxes = affine_axes([0.55, 0.55, 0.4, 0.4]) # upper right quadrant -coords2 = AxesCoords() -coords2.xlim = xlim -coords2.ylim = ylim2 -coords2.affineaxes = affineaxes - fig = Figure() x = npy.arange(0, 10, 0.01) @@ -400,22 +395,32 @@ y2 = 10*npy.exp(-x) line1 = line(x, y1, color='blue', linewidth=2.0) -line1.affine = coords1.affime +line1.affine = coords1.affine -line2 = line(x, y2, color='red', linewidth=2.0) -line2.affine = coords1.affime - fig.add_path(line1) -fig.add_path(line2) +print 'before', line1.affine # update the view limits, all the affines should be automagically updated -xlim = 0,10 -ylim1 = -1.1, 1.1 -ylim2 = 0, 10 +coords1.xviewlim = 0, 10 +coords1.yviewlim = -1.1, 1.1 +print 'after', line1.affine -renderer = RendererAgg(600,400) -fig.set_renderer(renderer) -fig.draw() -print 'renderer affine', renderer.affine -renderer.show() + +if 0: + coords2 = AxesCoords() + coords2.xviewlim = coords1.xviewlim # share the x axis + coords2.affineaxes = affine_axes([0.1, 0.1, 0.4, 0.4]) # lower left quadrant + + + line2 = line(x, y2, color='red', linewidth=2.0) + line2.affine = coords2.affine + coords2.yviewlim = 0, 10 + fig.add_path(line2) + + +if 0: + renderer = RendererAgg(600,400) + fig.set_renderer(renderer) + fig.draw() + renderer.show() Modified: trunk/matplotlib/mpl1/mtraits.py =================================================================== --- trunk/matplotlib/mpl1/mtraits.py 2007-07-18 20:38:32 UTC (rev 3566) +++ trunk/matplotlib/mpl1/mtraits.py 2007-07-18 21:30:12 UTC (rev 3567) @@ -1,3 +1,17 @@ +""" +Install instructions for traits 2.0 + + rm -rf ~/dev/lib/python2.4/site-packages/enthought.* + + easy_install --install-dir=~/dev/lib/python2.4/site-packages --prefix=~/dev -f http://code.enthought.com/enstaller/eggs/source/unstable/ enthought.etsconfig enthought.util enthought.debug + + svn co https://svn.enthought.com/svn/enthought/branches/enthought.traits_2.0 enthought_traits + + cd enthought_traits/ + python setup.py install --prefix=~/dev + + +""" # Here is some example code showing how to define some representative # rc properties and construct a matplotlib artist using traits. # Because matplotlib ships with enthought traits already, you can run @@ -7,7 +21,7 @@ # below. import sys, os, re -import matplotlib.enthought.traits as traits +import enthought.traits.api as traits from matplotlib.cbook import is_string_like from matplotlib import colors as mcolors import numpy as npy @@ -86,7 +100,7 @@ def path_exists(ob, name, val): os.path.exists(val) -linestyles = ('-', '--', '-.', ':', 'steps', 'None') +linestyles = ('-', '--', '-.', ':', 'steps') TICKLEFT, TICKRIGHT, TICKUP, TICKDOWN = range(4) linemarkers = (None, '.', ',', 'o', '^', 'v', '<', '>', 's', '+', 'x', 'd', 'D', '|', '_', 'h', 'H', @@ -95,7 +109,7 @@ TICKRIGHT, TICKUP, TICKDOWN, - 'None') + ) linewidth = traits.Float(0.5) @@ -105,9 +119,10 @@ markersize = traits.Float(6) antialiased = flexible_true_trait alpha = traits.Range(0., 1., 0.) -interval = traits.Array('d', (2,)) -affine = traits.Array('d', (3,3)) -verts = traits.Array('d') -codes = traits.Array('b') +interval = traits.Array('d', (2,), npy.array([0.0, 1.0], npy.float_)) +affine = traits.Array('d', (3,3), + npy.array([[1,0,0],[0,1,0],[0,0,1]], npy.float_)) +verts = traits.Array('d', value=npy.array([[0,0],[0,0]], npy.float_)) +codes = traits.Array('b', value=npy.array([0,0], dtype=npy.uint8)) Added: trunk/matplotlib/mpl1/scratch.py =================================================================== --- trunk/matplotlib/mpl1/scratch.py (rev 0) +++ trunk/matplotlib/mpl1/scratch.py 2007-07-18 21:30:12 UTC (rev 3567) @@ -0,0 +1,150 @@ + +class Axis(Artist): + tickcolor = mtraits.color('black') + axiscolor = mtraits.color('black') + tickwidth = mtraits.linewidth(0.5) + viewlim = mtraits.interval + tickpath = mtraits.path + axispath = mtraits.path + + def __init__(self, figure): + self.figure = figure + self.pathids = set() + +class XAxis(Axis): + def __init__(self, figure, **kwargs): + Axis.__init__(self, figure, **kwargs) + + def set_ticks(self, yloc, ysize, ticks, fmt): + # we'll deal with locators, formatter and autoscaling later... + # todo, remove old paths + + for pathid in self.pathids: + self.figure.remove_path(pathid) + + codes = [] + verts = [] + tickmin = yloc-ysize/2. + tickmax = yloc+ysize/2. + for tick in ticks: + codes.append(Path.MOVETO) + verts.append((tick, tickmin)) + codes.append(Path.LINETO) + verts.append((tick, tickmax)) + + + path = Path() + path.verts = npy.array(verts) + path.codes = npy.array(codes) + path.strokecolor = self.tickcolor + path.fillcolor = None + path.linewidth = self.tickwidth + path.antialiased = False + + self.pathids.add(self.figure.add_path(path)) + + + xmin, xmax = self.viewlim + + # the axis line + codes = [] + verts = [] + codes.append(Path.MOVETO) + verts.append((xmin, yloc)) + codes.append(Path.LINETO) + verts.append((xmax, yloc)) + + path = Path() + path.verts = npy.array(verts) + path.codes = npy.array(codes) + path.strokecolor = self.axiscolor + path.fillcolor = None + path.antialiased = False + + self.pathids.add(self.figure.add_path(path)) + + +class YAxis: + def __init__(self, figure): + Axis.__init__(self, figure) + + def set_ticks(self, xloc, xsize, ticks, fmt): + + for pathid in self.pathids: + self.figure.remove_path(pathid) + + codes = [] + verts = [] + tickmin = yloc-ysize/2. + tickmax = yloc+ysize/2. + for tick in ticks: + codes.append(Path.MOVETO) + verts.append((tickmin, tick)) + codes.append(Path.LINETO) + verts.append((tickmax, tick)) + + + self.tickpath = path = Path() + path.verts = npy.array(verts) + path.codes = npy.array(codes) + path.strokecolor = self.tickcolor + path.fillcolor = None + path.linewidth = self.tickwidth + path.antialiased = False + + self.pathids.add(self.figure.add_path(path)) + + + ymin, ymax = self.viewlim + + # the axis line + codes = [] + verts = [] + codes.append(Path.MOVETO) + verts.append((xloc, ymin)) + codes.append(Path.LINETO) + verts.append((xloc, ymax)) + + self.axispath = path = Path() + path.verts = npy.array(verts) + path.codes = npy.array(codes) + path.strokecolor = self.axiscolor + path.fillcolor = None + path.antialiased = False + + self.pathids.add(self.figure.add_path(path)) + + + + + + +if 0: + ax1.set_ylim(-1.1, 1.1) + + xaxis = XAxis(ax1) + xaxis.set_ticks(0, 0.1, npy.arange(11.0), '%d') + + yaxis1 = YAxis(ax1) + yaxis1.set_ticks(-1.1, 0.2, npy.arange(-1.0, 1.1, 0.5), '%d') + yaxis1.axiscolor = line1.color + + yaxis2 = YAxis(ax1) + yaxis2.set_ticks(5.0, 0.2, npy.arange(-1.0, 1.1, 0.5), '%d') + + + + + + theta = 0.25*npy.pi # 45 degree axes rotation + #rotate_axes(ax1, theta) + + + r = npy.arange(0, 1, 0.01) + theta = r*4*npy.pi + X2 = npy.array([r,theta]).T + line2 = Line(X2, model=Polar()) + ax2.add_line(line2) + # currently cartesian + ax2.set_xlim(-1,1) + ax2.set_ylim(-1,1) Added: trunk/matplotlib/mpl1/test.py =================================================================== --- trunk/matplotlib/mpl1/test.py (rev 0) +++ trunk/matplotlib/mpl1/test.py 2007-07-18 21:30:12 UTC (rev 3567) @@ -0,0 +1,15 @@ +import numpy +from enthought.traits.api import HasTraits, Array +import mtraits + + +class Path(HasTraits): + """ + The path is an object that talks to the backends, and is an + intermediary between the high level path artists like Line and + Polygon, and the backend renderer + """ + strokecolor = mtraits.color('white') + +p = Path() +print 'strokecolor', p.strokecolor This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |