Menu

[r4612]: / branches / transforms / mpl1 / scratch.py  Maximize  Restore  History

Download this file

211 lines (153 with data), 5.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class AggPath(Path):
agg_fillcolor = mtraits.ColorAgg(RendererAgg.blue)
agg_strokecolor = mtraits.ColorAgg(RendererAgg.black)
agg_path = mtraits.PathAgg
def __init__(self, path):
self.strokecolor = path.strokecolor
self.fillcolor = path.fillcolor
self.alpha = path.alpha
self.linewidth = path.linewidth
self.antialiased = path.antialiased
self.pathdata = path.pathdata
self.affine = path.affine
path.sync_trait('strokecolor', self)
path.sync_trait('fillcolor', self)
path.sync_trait('alpha', self)
path.sync_trait('linewidth', self)
path.sync_trait('antialiased', self)
path.sync_trait('pathdata', self)
path.sync_trait('affine', self)
def _pathdata_changed(self, olddata, newdata):
print 'pathdata changed'
MOVETO, LINETO, CLOSEPOLY = Path.MOVETO, Path.LINETO, Path.CLOSEPOLY
agg_path = agg.path_storage()
codes, verts = newdata
N = len(codes)
for i in range(N):
x, y = verts[i]
code = codes[i]
if code==MOVETO:
agg_path.move_to(x, y)
elif code==LINETO:
agg_path.line_to(x, y)
elif code==CLOSEPOLY:
agg_path.close_polygon()
self.agg_path = agg_path
def _fillcolor_changed(self, oldcolor, newcolor):
self.agg_fillcolor = self.color_to_rgba8(newcolor)
def _strokecolor_changed(self, oldcolor, newcolor):
c = self.color_to_rgba8(newcolor)
print 'stroke change: old=%s, new=%s, agg=%s, ret=%s'%(
oldcolor, newcolor, self.agg_strokecolor, c)
self.agg_strokecolor = c
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)
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)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.