1
1
"""
2
- Abstract base classes definine the primitives that renderers and
2
+ Abstract base classes define the primitives that renderers and
3
3
graphics contexts must implement to serve as a matplotlib backend
4
4
"""
5
5
6
6
from __future__ import division
7
7
import sys
8
8
9
- from matplotlib import verbose
10
9
from cbook import is_string_like , enumerate , strip_math , Stack
11
10
from colors import colorConverter
12
11
from numerix import array , sqrt , pi , log , asarray , ones , Float
@@ -53,24 +52,22 @@ def points_to_pixels(self, points):
53
52
points to pixels = points * pixels_per_inch/72.0 * dpi/72.0
54
53
"""
55
54
return points
56
-
57
55
58
56
def get_text_extent (self , text ):
59
57
"""
60
58
Get the text extent in window coords
61
59
"""
62
60
return lbwh_to_bbox (0 ,0 ,1 ,1 ) # your values here
63
61
64
-
65
62
def draw_arc (self , gcEdge , rgbFace , x , y , width , height , angle1 , angle2 ):
66
63
"""
67
64
Draw an arc using GraphicsContext instance gcEdge, centered at x,y,
68
65
with width and height and angles from 0.0 to 360.0
69
66
70
67
If the color rgbFace is not None, fill the arc with it.
71
68
"""
72
- pass # derived must override
73
-
69
+ raise NotImplementedError
70
+
74
71
def draw_image (self , x , y , im , origin , bbox ):
75
72
"""
76
73
Draw the Image instance into the current axes; x is the
@@ -84,25 +81,21 @@ def draw_image(self, x, y, im, origin, bbox):
84
81
bbox is a matplotlib.transforms.BBox instance for clipping, or
85
82
None
86
83
"""
87
- print >> sys .stderr , 'This backend does not yet support images'
88
-
89
- def draw_line (self , gc , x1 , y1 , x2 , y2 ):
90
- """
91
- Draw a single line from x1,y1 to x2,y2
92
- """
93
- pass # derived must override
84
+ raise NotImplementedError
94
85
95
86
def _draw_marker (self , gc , path , x , y , trans ):
96
87
"""
97
88
This method is currently underscore hidden because the
98
89
draw_markers method is being used as a sentinel for newstyle
99
90
backend drawing
91
+
92
+ path - a list of path elements, see matplotlib.paths
100
93
101
- Draw the marker specified in path with graphics context gx at
94
+ Draw the marker specified in path with graphics context gc at
102
95
each of the locations in arrays x and y. trans is a
103
96
matplotlib.transforms.Transformation instance used to
104
97
transform x and y to display coords. It consists of an
105
- optional ) nonlinear component and an affine. You can access
98
+ optional nonlinear component and an affine. You can access
106
99
these two components as
107
100
108
101
if transform.need_nonlinear():
@@ -113,6 +106,106 @@ def _draw_marker(self, gc, path, x, y, trans):
113
106
"""
114
107
pass
115
108
109
+ def draw_line_collection (self , segments , transform , clipbox ,
110
+ colors , linewidths , linestyle , antialiaseds ,
111
+ offsets , transOffset ):
112
+ """
113
+ This is a function for optimized line drawing. If you need to
114
+ draw many line segments with similar properties, it is faster
115
+ to avoid the overhead of all the object creation etc. The
116
+ lack of total configurability is compensated for with
117
+ efficiency. Hence we don't use a GC and many of the line
118
+ props it supports. See matplotlib.collections for more
119
+ details
120
+
121
+ sements is a sequence of ( line0, line1, line2), where linen =
122
+ (x0, y0), (x1, y1), ... (xm, ym). Each line can be a
123
+ different length
124
+
125
+ transform is used to Transform the lines
126
+
127
+ clipbox is a xmin, ymin, width, height clip rect
128
+
129
+ colors is a tuple of RGBA tuples
130
+
131
+ linewidths is a tuple of linewidths
132
+
133
+ linestyle is an (offset, onoffseq) tuple or None,None for solid
134
+
135
+ antialiseds is a tuple of ones or zeros indicating whether the
136
+ segment should be aa or not
137
+
138
+ offsets, if not None, is a list of x,y offsets to translate
139
+ the lines by after transoff is used to transform the offset
140
+ coords
141
+
142
+ This function is intended to be overridden by the backend
143
+ level in extension code for backends that want fast line
144
+ collection drawing. Here is is implemented using native
145
+ backend calls and may be slow
146
+ """
147
+
148
+ gc = self .new_gc ()
149
+
150
+ i = 0
151
+ Nc = len (colors )
152
+ Nlw = len (linewidths )
153
+
154
+ Naa = len (antialiaseds )
155
+
156
+ usingOffsets = offsets is not None
157
+ Noffsets = 0
158
+ Nsegments = len (segments )
159
+ if usingOffsets :
160
+ Noffsets = len (offsets )
161
+
162
+ N = max (Noffsets , Nsegments )
163
+
164
+ gc .set_clip_rectangle (clipbox .get_bounds ())
165
+
166
+ if linestyle [0 ] is not None :
167
+ offset , seq = linestyle
168
+ gc .set_dashes ( offset , seq )
169
+
170
+ for i in xrange (N ):
171
+ x , y = zip (* segments [i % Nsegments ])
172
+ x , y = transform .numerix_x_y (array (x ), array (y ))
173
+
174
+ color = colors [i % Nc ]
175
+ rgb = color [0 ], color [1 ], color [2 ]
176
+ alpha = color [- 1 ]
177
+
178
+ gc .set_foreground ( rgb , isRGB = True )
179
+ gc .set_alpha ( alpha )
180
+ gc .set_linewidth ( linewidths [i % Nlw ] )
181
+
182
+ gc .set_antialiased ( antialiaseds [i % Naa ] )
183
+ if usingOffsets :
184
+ xo , yo = transOffset .xy_tup (offsets [i % Noffsets ])
185
+ x += xo
186
+ y += yo
187
+ self .draw_lines (gc , x , y )
188
+ i += 1
189
+
190
+ def draw_line (self , gc , x1 , y1 , x2 , y2 ):
191
+ """
192
+ Draw a single line from x1,y1 to x2,y2
193
+ """
194
+ raise NotImplementedError
195
+
196
+ def draw_lines (self , gc , x , y ):
197
+ """
198
+ x and y are equal length arrays, draw lines connecting each
199
+ point in x, y
200
+ """
201
+ raise NotImplementedError
202
+
203
+ def draw_point (self , gc , x , y ):
204
+ """
205
+ Draw a single point at x,y
206
+ """
207
+ raise NotImplementedError
208
+
116
209
def draw_poly_collection (
117
210
self , verts , transform , clipbox , facecolors , edgecolors ,
118
211
linewidths , antialiaseds , offsets , transOffset ):
@@ -144,7 +237,6 @@ def draw_poly_collection(
144
237
if clipbox is not None : gc .set_clip_rectangle (clipbox .get_bounds ())
145
238
146
239
for i in xrange (N ):
147
-
148
240
149
241
rf ,gf ,bf ,af = facecolors [i % Nface ]
150
242
re ,ge ,be ,ae = edgecolors [i % Nedge ]
@@ -167,6 +259,25 @@ def draw_poly_collection(
167
259
168
260
self .draw_polygon (gc , rgbFace , tverts )
169
261
262
+ def draw_polygon (self , gc , rgbFace , points ):
263
+ """
264
+ Draw a polygon using the GraphicsContext instance gc.
265
+ points is a len vertices tuple, each element
266
+ giving the x,y coords a vertex
267
+
268
+ If the color rgbFace is not None, fill the polygon with it
269
+ """
270
+ raise NotImplementedError
271
+
272
+ def draw_rectangle (self , gcEdge , rgbFace , x , y , width , height ):
273
+ """
274
+ Draw a non-filled rectangle using the GraphicsContext instance gcEdge,
275
+ with lower left at x,y with width and height.
276
+
277
+ If rgbFace is not None, fill the rectangle with it.
278
+ """
279
+ raise NotImplementedError
280
+
170
281
def draw_regpoly_collection (
171
282
self , clipbox , offsets , transOffset , verts , sizes ,
172
283
facecolors , edgecolors , linewidths , antialiaseds ):
@@ -223,140 +334,24 @@ def draw_regpoly_collection(
223
334
gc .set_antialiased ( antialiaseds [i % Naa ] )
224
335
#print 'verts', zip(thisxverts, thisyverts)
225
336
self .draw_polygon (gc , rgbFace , zip (thisxverts , thisyverts ))
226
-
227
-
228
-
229
-
230
- def draw_line_collection (self , segments , transform , clipbox ,
231
- colors , linewidths , linestyle , antialiaseds ,
232
- offsets , transOffset ):
233
- """
234
- This is a function for optimized line drawing. If you need to
235
- draw many line segments with similar properties, it is faster
236
- to avoid the overhead of all the object creation etc. The
237
- lack of total configurability is compensated for with
238
- efficiency. Hence we don't use a GC and many of the line
239
- props it supports. See matplotlib.collections for more
240
- details
241
-
242
- sements is a sequence of ( line0, line1, line2), where linen =
243
- (x0, y0), (x1, y1), ... (xm, ym). Each line can be a
244
- different length
245
-
246
- transform is used to Transform the lines
247
-
248
- clipbox i
1241
s a xmin, ymin, width, height clip rect
249
337
250
- colors is a tuple of RGBA tuples
251
-
252
- linewidths is a tuple of linewidths
253
-
254
- linestyle is an (offset, onoffseq) tuple or None,None for solid
255
-
256
- antialiseds is a tuple of ones or zeros indicating whether the
257
- segment should be aa or not
258
-
259
- offsets, if not None, is a list of x,y offsets to translate
260
- the lines by after transoff is used to transform the offset
261
- coords
262
-
263
- This function is intended to be overridden by the backend
264
- level in extension code for backends that want fast line
265
- collection drawing. Here is is implemented using native
266
- backend calls and may be slow
267
- """
268
-
269
- gc = self .new_gc ()
270
-
271
- i = 0
272
- Nc = len (colors )
273
- Nlw = len (linewidths )
274
-
275
- Naa = len (antialiaseds )
276
-
277
- usingOffsets = offsets is not None
278
- Noffsets = 0
279
- Nsegments = len (segments )
280
- if usingOffsets :
281
- Noffsets = len (offsets )
282
-
283
- N = max (Noffsets , Nsegments )
284
-
285
- gc .set_clip_rectangle (clipbox .get_bounds ())
286
-
287
- if linestyle [0 ] is not None :
288
- offset , seq = linestyle
289
- gc .set_dashes ( offset , seq )
290
-
291
- for i in xrange (N ):
292
- x , y = zip (* segments [i % Nsegments ])
293
- x , y = transform .numerix_x_y (array (x ), array (y ))
294
-
295
- color = colors [i % Nc ]
296
- rgb = color [0 ], color [1 ], color [2 ]
297
- alpha = color [- 1 ]
298
-
299
- gc .set_foreground ( rgb , isRGB = True )
300
- gc .set_alpha ( alpha )
301
- gc .set_linewidth ( linewidths [i % Nlw ] )
302
-
303
- gc .set_antialiased ( antialiaseds [i % Naa ] )
304
- if usingOffsets :
305
- xo , yo = transOffset .xy_tup (offsets [i % Noffsets ])
306
- x += xo
307
- y += yo
308
- self .draw_lines (gc , x , y )
309
- i += 1
310
-
311
- def draw_lines (self , gc , x , y ):
312
- """
313
- x and y are equal length arrays, draw lines connecting each
314
- point in x, y
315
- """
316
- pass # derived must override
317
338
318
- def draw_point (self , gc , x , y ):
319
- """
320
- Draw a single point at x,y
321
- """
322
- pass # derived must override
323
-
324
- def draw_polygon (self , gc , rgbFace , points ):
325
- """
326
- Draw a polygon using the GraphicsContext instance gc.
327
- points is a len vertices tuple, each element
328
- giving the x,y coords a vertex
329
-
330
- If the color rgbFace is not None, fill the polygon with it
331
- """
332
- pass # derived must override
333
-
334
- def draw_rectangle (self , gcEdge , rgbFace , x , y , width , height ):
335
- """
336
- Draw a non-filled rectangle using the GraphicsContext instance gcEdge,
337
- with lower left at x,y with width and height.
338
-
339
- If rgbFace is not None, fill the rectangle with it.
340
- """
341
- pass # derived must override
342
-
343
- def strip_math (self , s ):
344
- return strip_math (s )
345
-
346
339
def draw_text (self , gc , x , y , s , prop , angle , ismath = False ):
347
340
"""
348
341
Draw the text.Text instance s at x,y (display coords) with font
349
342
properties instance prop at angle in degrees
350
343
"""
351
- pass
352
-
344
+ raise NotImplementedError
353
345
354
346
def new_gc (self ):
355
347
"""
356
348
Return an instance of a GraphicsContextBase
357
349
"""
358
350
return GraphicsContextBase ()
359
351
352
+ def strip_math (self , s ):
353
+ return strip_math (s )
354
+
360
355
361
356
class GraphicsContextBase :
362
357
"""An abstract base class that provides color, line styles, etc...
@@ -1317,4 +1312,3 @@ def zoom(self, *args):
1317
1312
self .mode = 'Zoom to rect mode'
1318
1313
1319
1314
self .set_message (self .mode )
1320
-
0 commit comments