Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3145747

Browse files
committed
Added ellipse rotation
svn path=/trunk/matplotlib/; revision=2740
1 parent 2a1c6a5 commit 3145747

10 files changed

Lines changed: 51 additions & 17 deletions

File tree

API_CHANGES

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Renderer.draw_arc now takes an additional parameter, rotation.
2+
It specifies to draw the artist rotated in degrees anti-
3+
clockwise. It was added for rotated ellipses.
4+
15
Renamed Figure.set_figsize_inches to Figure.set_size_inches to
26
better match the get method, Figure.get_size_inches.
37

examples/ellipse_demo.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pylab import *
2+
from matplotlib.patches import Ellipse
3+
4+
NUM = 250
5+
6+
ells = [Ellipse(rand(2)*10, rand(), rand(), rand()*360) for i in xrange(NUM)]
7+
8+
a = subplot(111)
9+
for e in ells:
10+
a.add_artist(e)
11+
e.set_clip_box(a.bbox)
12+
e.set_alpha(rand())
13+
e.set_facecolor(rand(3))
14+
15+
xlim(0, 10)
16+
ylim(0, 10)
17+
18+
savefig('ellipse_demo')
19+
show()

lib/matplotlib/backend_bases.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ def close_group(self, s):
3333
"""
3434
pass
3535

36-
def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2):
36+
def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2, rotation):
3737
"""
3838
Draw an arc using GraphicsContext instance gcEdge, centered at x,y,
3939
with width and height and angles from 0.0 to 360.0
4040
0 degrees is at 3-o'clock
4141
positive angles are anti-clockwise
42+
draw rotated 'rotation' degrees anti-clockwise about x,y
4243
4344
If the color rgbFace is not None, fill the arc with it.
4445
"""

lib/matplotlib/backends/backend_agg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def __init__(self, width, height, dpi):
137137
if __debug__: verbose.report('RendererAgg.__init__ done',
138138
'debug-annoying')
139139

140-
def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2):
140+
def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2, rotation):
141141
"""
142142
Draw an arc centered at x,y with width and height and angles
143143
from 0.0 to 360.0
@@ -150,7 +150,7 @@ def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2):
150150
"""
151151
if __debug__: verbose.report('RendererAgg.draw_arc', 'debug-annoying')
152152
self._renderer.draw_ellipse(
153-
gcEdge, rgbFace, x, y, width/2, height/2) # ellipse takes radius
153+
gcEdge, rgbFace, x, y, width/2, height/2, rotation) # ellipse takes radius
154154

155155

156156
def draw_line(self, gc, x1, y1, x2, y2):

lib/matplotlib/backends/backend_cairo.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,24 @@ def _fill_and_stroke (self, ctx, fill_c):
128128
#_.ctx.restore() # revert to the default attributes
129129

130130

131-
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2):
131+
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2, rotation):
132132
if _debug: print '%s.%s()' % (self.__class__.__name__, _fn_name())
133133
# draws circular arcs where width=height
134134
# FIXME
135135
# to get a proper arc of width/height you can use translate() and
136136
# scale(), see draw_arc() manual page
137-
radius = (height + width) / 4
137+
138+
#radius = (height + width) / 4
138139
ctx = gc.ctx
139-
ctx.new_path()
140-
ctx.arc (x, self.height - y, radius,
141-
angle1 * numx.pi/180.0, angle2 * numx.pi/180.0)
140+
ctx.save()
141+
ctx.rotate(rotation)
142+
ctx.scale(width / 2.0, height / 2.0)
143+
ctx.arc(0.0, 0.0, 1.0, 0.0, 2*numx.pi)
144+
ctx.restore()
145+
146+
#ctx.new_path()
147+
#ctx.arc (x, self.height - y, radius,
148+
# angle1 * numx.pi/180.0, angle2 * numx.pi/180.0)
142149
self._fill_and_stroke (ctx, rgbFace)
143150

144151

lib/matplotlib/backends/backend_ps.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,16 @@ def _get_font_ttf(self, prop):
309309
font.set_size(size, 72.0)
310310
return font
311311

312-
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2):
312+
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2, rotation):
313313
"""
314314
Draw an arc centered at x,y with width and height and angles
315315
from 0.0 to 360.0
316316
317317
If gcFace is not None, fill the arc slice with it. gcEdge
318318
is a GraphicsContext instance
319319
"""
320-
ps = '%s ellipse' % _nums_to_str(angle1, angle2,
321-
0.5*width, 0.5*height, x, y)
320+
ps = '%f %f translate\n%f rotate\n%f %f translate\n%s ellipse' % \
321+
(x, y, rotation, -x, -y, _nums_to_str(angle1, angle2, 0.5*width, 0.5*height, x, y))
322322
self._draw_ps(ps, gc, rgbFace, "arc")
323323

324324
def _rgba(self, im):

lib/matplotlib/backends/backend_svg.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ def open_group(self, s):
122122
def close_group(self, s):
123123
self._svgwriter.write('</g>\n')
124124

125-
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2):
125+
def draw_arc(self, gc, rgbFace, x, y, width, height, angle1, angle2, rotation):
126126
"""
127127
Ignores angles for now
128128
"""
129-
details = 'cx="%f" cy="%f" rx="%f" ry="%f"' % (x,self.height-y,width/2,height/2)
129+
details = 'cx="%f" cy="%f" rx="%f" ry="%f" transform="rotate(%f %f %f)"' % \
130+
(x, self.height-y, width/2.0, height/2.0, -rotation, x, self.height-y)
130131
self._draw_svg_element('ellipse', details, gc, rgbFace)
131132

132133
def draw_image(self, x, y, im, bbox):

lib/matplotlib/lines.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def _draw_circle(self, renderer, gc, xt, yt, point=False):
615615
else:
616616
for (x,y) in zip(xt, yt):
617617
renderer.draw_arc(gc, rgbFace,
618-
x, y, w, w, 0.0, 360.0)
618+
x, y, w, w, 0.0, 360.0, 0.0)
619619

620620

621621

lib/matplotlib/patches.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,11 +524,12 @@ class Ellipse(Patch):
524524
"""
525525
A scale-free ellipse
526526
"""
527-
def __init__(self, xy, width, height, **kwargs):
527+
def __init__(self, xy, width, height, angle=0.0, **kwargs):
528528
Patch.__init__(self, **kwargs)
529529

530530
self.center = array(xy, Float)
531531
self.width, self.height = width, height
532+
self.angle = angle
532533

533534
x,y = self.center
534535
l,r = x-width/2.0, x+width/2.0
@@ -565,7 +566,7 @@ def draw(self, renderer):
565566
width = tverts[3,0] - tverts[1,0]
566567
height = tverts[2,1] - tverts[4,1]
567568

568-
renderer.draw_arc(gc, rgbFace, tverts[0,0], tverts[0,1], width, height, 0.0, 360.0)
569+
renderer.draw_arc(gc, rgbFace, tverts[0,0], tverts[0,1], width, height, 0.0, 360.0, self.angle)
569570

570571
class Circle(Ellipse):
571572
"""

src/_backend_agg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ RendererAgg::draw_rectangle(const Py::Tuple & args) {
372372
Py::Object
373373
RendererAgg::draw_ellipse(const Py::Tuple& args) {
374374
_VERBOSE("RendererAgg::draw_ellipse");
375-
args.verify_length(6);
375+
args.verify_length(7);
376376

377377
GCAgg gc = GCAgg(args[0], dpi);
378378
facepair_t face = _get_rgba_face(args[1], gc.alpha);
@@ -382,6 +382,7 @@ RendererAgg::draw_ellipse(const Py::Tuple& args) {
382382
double y = Py::Float( args[3] );
383383
double w = Py::Float( args[4] );
384384
double h = Py::Float( args[5] );
385+
double rot = Py::Float( args[6] );
385386

386387
set_clipbox_rasterizer(gc.cliprect);
387388

0 commit comments

Comments
 (0)