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

Skip to content

Commit f04f566

Browse files
committed
restored unit support for ellipses -- and added examples/units/ellipse_with_units.py
svn path=/trunk/matplotlib/; revision=3954
1 parent 4b508c0 commit f04f566

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Compare the ellipse generated with arcs versus a polygonal approximation
3+
"""
4+
from basic_units import cm
5+
import numpy as npy
6+
from matplotlib import patches
7+
from pylab import figure, show
8+
9+
xcenter, ycenter = 0.38*cm, 0.52*cm
10+
#xcenter, ycenter = 0., 0.
11+
width, height = 1e-1*cm, 3e-1*cm
12+
angle = -30
13+
14+
theta = npy.arange(0.0, 360.0, 1.0)*npy.pi/180.0
15+
x = 0.5 * width * npy.cos(theta)
16+
y = 0.5 * height * npy.sin(theta)
17+
18+
rtheta = angle*npy.pi/180.
19+
R = npy.array([
20+
[npy.cos(rtheta), -npy.sin(rtheta)],
21+
[npy.sin(rtheta), npy.cos(rtheta)],
22+
])
23+
24+
25+
x, y = npy.dot(R, npy.array([x, y]))
26+
x += xcenter
27+
y += ycenter
28+
29+
fig = figure()
30+
ax = fig.add_subplot(211, aspect='auto')
31+
ax.fill(x, y, alpha=0.2, facecolor='yellow', edgecolor='yellow', linewidth=1, zorder=1)
32+
33+
e1 = patches.Ellipse((xcenter, ycenter), width, height,
34+
angle=angle, linewidth=2, fill=False, zorder=2)
35+
36+
ax.add_patch(e1)
37+
38+
ax = fig.add_subplot(212, aspect='equal')
39+
ax.fill(x, y, alpha=0.2, facecolor='green', edgecolor='green', zorder=1)
40+
e2 = patches.Ellipse((xcenter, ycenter), width, height,
41+
angle=angle, linewidth=2, fill=False, zorder=2)
42+
43+
44+
ax.add_patch(e2)
45+
46+
#fig.savefig('ellipse_compare.png')
47+
fig.savefig('ellipse_compare')
48+
49+
show()

lib/matplotlib/patches.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ def get_verts(self):
820820
[npy.sin(rtheta), npy.cos(rtheta)],
821821
])
822822

823+
x = self.convert_xunits(x)
824+
y = self.convert_yunits(y)
823825

824826
x, y = npy.dot(R, npy.array([x, y]))
825827
x += xcenter
@@ -845,9 +847,6 @@ def draw(self, renderer):
845847
if self._hatch:
846848
gc.set_hatch(self._hatch )
847849

848-
offset = self.offset
849-
850-
851850

852851
if not hasattr(renderer, 'draw_path'):
853852
verbose.report('patches.Ellipse renderer does not support path drawing; falling back on vertex approximation for nonlinear transformation')
@@ -856,15 +855,23 @@ def draw(self, renderer):
856855

857856

858857
x, y = self.center
858+
x = self.convert_xunits(x)
859+
y = self.convert_yunits(y)
860+
859861
theta = self.angle * npy.pi/180.
860862
T = npy.array([
861863
[1, 0, x],
862864
[0, 1, y],
863865
[0, 0, 1]])
864866

867+
w, h = self.width/2, self.height/2.
868+
w = self.convert_xunits(w)
869+
h = self.convert_yunits(h)
870+
871+
865872
S = npy.array([
866-
[self.width/2., 0, 0],
867-
[0, self.height/2., 0],
873+
[w, 0, 0],
874+
[0, h, 0],
868875
[0, 0, 1]])
869876

870877

0 commit comments

Comments
 (0)