Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

""" 

Ellipses 

""" 

#***************************************************************************** 

# Copyright (C) 2010 Vincent Delecroix <20100.delecroix@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# 

# This code is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 

# General Public License for more details. 

# 

# The full text of the GPL is available at: 

# 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

from __future__ import print_function 

from __future__ import absolute_import 

 

from .primitive import GraphicPrimitive 

from sage.plot.misc import options, rename_keyword 

from sage.plot.colors import to_mpl_color 

from math import sin, cos, sqrt, pi, fmod 

 

class Ellipse(GraphicPrimitive): 

""" 

Primitive class for the ``Ellipse`` graphics type. See ``ellipse?`` for 

information about actually plotting ellipses. 

 

INPUT: 

 

- ``x,y`` - coordinates of the center of the ellipse 

 

- ``r1, r2`` - radii of the ellipse 

 

- ``angle`` - angle 

 

- ``options`` - dictionary of options 

 

EXAMPLES: 

 

Note that this construction should be done using ``ellipse``:: 

 

sage: from sage.plot.ellipse import Ellipse 

sage: Ellipse(0, 0, 2, 1, pi/4, {}) 

Ellipse centered at (0.0, 0.0) with radii (2.0, 1.0) and angle 0.785398163397 

""" 

def __init__(self, x, y, r1, r2, angle, options): 

""" 

Initializes base class ``Ellipse``. 

 

TESTS:: 

 

sage: from sage.plot.ellipse import Ellipse 

sage: e = Ellipse(0, 0, 1, 1, 0, {}) 

sage: print(loads(dumps(e))) 

Ellipse centered at (0.0, 0.0) with radii (1.0, 1.0) and angle 0.0 

sage: ellipse((0,0),0,1) 

Traceback (most recent call last): 

... 

ValueError: both radii must be positive 

""" 

self.x = float(x) 

self.y = float(y) 

self.r1 = float(r1) 

self.r2 = float(r2) 

if self.r1 <= 0 or self.r2 <= 0: 

raise ValueError("both radii must be positive") 

self.angle = fmod(angle,2*pi) 

if self.angle < 0: self.angle += 2*pi 

GraphicPrimitive.__init__(self, options) 

 

def get_minmax_data(self): 

""" 

Returns a dictionary with the bounding box data. 

 

The bounding box is computed to be as minimal as possible. 

 

EXAMPLES: 

 

An example without an angle:: 

 

sage: p = ellipse((-2, 3), 1, 2) 

sage: d = p.get_minmax_data() 

sage: d['xmin'] 

-3.0 

sage: d['xmax'] 

-1.0 

sage: d['ymin'] 

1.0 

sage: d['ymax'] 

5.0 

 

The same example with a rotation of angle `\pi/2`:: 

 

sage: p = ellipse((-2, 3), 1, 2, pi/2) 

sage: d = p.get_minmax_data() 

sage: d['xmin'] 

-4.0 

sage: d['xmax'] 

0.0 

sage: d['ymin'] 

2.0 

sage: d['ymax'] 

4.0 

""" 

from sage.plot.plot import minmax_data 

 

epsilon = 0.000001 

cos_angle = cos(self.angle) 

 

if abs(cos_angle) > 1-epsilon: 

xmax = self.r1 

ymax = self.r2 

elif abs(cos_angle) < epsilon: 

xmax = self.r2 

ymax = self.r1 

else: 

sin_angle = sin(self.angle) 

tan_angle = sin_angle / cos_angle 

sxmax = ((self.r2*tan_angle)/self.r1)**2 

symax = (self.r2/(self.r1*tan_angle))**2 

xmax = ( 

abs(self.r1 * cos_angle / sqrt(sxmax+1.)) + 

abs(self.r2 * sin_angle / sqrt(1./sxmax+1.))) 

ymax = ( 

abs(self.r1 * sin_angle / sqrt(symax+1.)) + 

abs(self.r2 * cos_angle / sqrt(1./symax+1.))) 

 

return minmax_data([self.x - xmax, self.x + xmax], 

[self.y - ymax, self.y + ymax], 

dict=True) 

 

def _allowed_options(self): 

""" 

Return the allowed options for the ``Ellipse`` class. 

 

EXAMPLES:: 

 

sage: p = ellipse((3, 3), 2, 1) 

sage: p[0]._allowed_options()['alpha'] 

'How transparent the figure is.' 

sage: p[0]._allowed_options()['facecolor'] 

'2D only: The color of the face as an RGB tuple.' 

""" 

return {'alpha':'How transparent the figure is.', 

'fill': 'Whether or not to fill the ellipse.', 

'legend_label':'The label for this item in the legend.', 

'legend_color':'The color of the legend text.', 

'thickness':'How thick the border of the ellipse is.', 

'edgecolor':'2D only: The color of the edge as an RGB tuple.', 

'facecolor':'2D only: The color of the face as an RGB tuple.', 

'rgbcolor':'The color (edge and face) as an RGB tuple.', 

'hue':'The color given as a hue.', 

'zorder':'2D only: The layer level in which to draw', 

'linestyle':"2D only: The style of the line, which is one of " 

"'dashed', 'dotted', 'solid', 'dashdot', or '--', ':', '-', '-.', " 

"respectively."} 

 

def _repr_(self): 

""" 

String representation of ``Ellipse`` primitive. 

 

TESTS:: 

 

sage: from sage.plot.ellipse import Ellipse 

sage: Ellipse(0,0,2,1,0,{})._repr_() 

'Ellipse centered at (0.0, 0.0) with radii (2.0, 1.0) and angle 0.0' 

""" 

return "Ellipse centered at (%s, %s) with radii (%s, %s) and angle %s"%(self.x, self.y, self.r1, self.r2, self.angle) 

 

def _render_on_subplot(self, subplot): 

""" 

Render this ellipse in a subplot. This is the key function that 

defines how this ellipse graphics primitive is rendered in matplotlib's 

library. 

 

TESTS:: 

 

sage: ellipse((0,0),3,1,pi/6,fill=True,alpha=0.3) 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: ellipse((3,2),1,2) 

Graphics object consisting of 1 graphics primitive 

""" 

import matplotlib.patches as patches 

from sage.plot.misc import get_matplotlib_linestyle 

 

options = self.options() 

p = patches.Ellipse( 

(self.x,self.y), 

self.r1*2.,self.r2*2.,self.angle/pi*180.) 

p.set_linewidth(float(options['thickness'])) 

p.set_fill(options['fill']) 

a = float(options['alpha']) 

p.set_alpha(a) 

ec = to_mpl_color(options['edgecolor']) 

fc = to_mpl_color(options['facecolor']) 

if 'rgbcolor' in options: 

ec = fc = to_mpl_color(options['rgbcolor']) 

p.set_edgecolor(ec) 

p.set_facecolor(fc) 

p.set_linestyle(get_matplotlib_linestyle(options['linestyle'],return_type='long')) 

p.set_label(options['legend_label']) 

z = int(options.pop('zorder', 0)) 

p.set_zorder(z) 

subplot.add_patch(p) 

 

def plot3d(self): 

r""" 

Plotting in 3D is not implemented. 

 

TESTS:: 

 

sage: from sage.plot.ellipse import Ellipse 

sage: Ellipse(0,0,2,1,pi/4,{}).plot3d() 

Traceback (most recent call last): 

... 

NotImplementedError 

""" 

raise NotImplementedError 

 

@rename_keyword(color='rgbcolor') 

@options(alpha=1, fill=False, thickness=1, edgecolor='blue', facecolor='blue', linestyle='solid', zorder=5, 

aspect_ratio=1.0, legend_label=None, legend_color=None) 

def ellipse(center, r1, r2, angle=0, **options): 

""" 

Return an ellipse centered at a point center = ``(x,y)`` with radii = 

``r1,r2`` and angle ``angle``. Type ``ellipse.options`` to see all 

options. 

 

INPUT: 

 

- ``center`` - 2-tuple of real numbers - coordinates of the center 

 

- ``r1``, ``r2`` - positive real numbers - the radii of the ellipse 

 

- ``angle`` - real number (default: 0) - the angle between the first axis 

and the horizontal 

 

OPTIONS: 

 

- ``alpha`` - default: 1 - transparency 

 

- ``fill`` - default: False - whether to fill the ellipse or not 

 

- ``thickness`` - default: 1 - thickness of the line 

 

- ``linestyle`` - default: ``'solid'`` - The style of the line, which is one 

of ``'dashed'``, ``'dotted'``, ``'solid'``, ``'dashdot'``, or ``'--'``, 

``':'``, ``'-'``, ``'-.'``, respectively. 

 

- ``edgecolor`` - default: 'black' - color of the contour 

 

- ``facecolor`` - default: 'red' - color of the filling 

 

- ``rgbcolor`` - 2D or 3D plotting. This option overrides 

``edgecolor`` and ``facecolor`` for 2D plotting. 

 

- ``legend_label`` - the label for this item in the legend 

 

- ``legend_color`` - the color for the legend label 

 

EXAMPLES: 

 

An ellipse centered at (0,0) with major and minor axes of lengths 2 and 1. 

Note that the default color is blue:: 

 

sage: ellipse((0,0),2,1) 

Graphics object consisting of 1 graphics primitive 

 

More complicated examples with tilted axes and drawing options:: 

 

sage: ellipse((0,0),3,1,pi/6,fill=True,alpha=0.3,linestyle="dashed") 

Graphics object consisting of 1 graphics primitive 

sage: ellipse((0,0),3,1,pi/6,fill=True,alpha=0.3,linestyle="--") 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: ellipse((0,0),3,1,pi/6,fill=True,edgecolor='black',facecolor='red') 

Graphics object consisting of 1 graphics primitive 

 

We see that ``rgbcolor`` overrides these other options, as this plot 

is green:: 

 

sage: ellipse((0,0),3,1,pi/6,fill=True,edgecolor='black',facecolor='red',rgbcolor='green') 

Graphics object consisting of 1 graphics primitive 

 

The default aspect ratio for ellipses is 1.0:: 

 

sage: ellipse((0,0),2,1).aspect_ratio() 

1.0 

 

One cannot yet plot ellipses in 3D:: 

 

sage: ellipse((0,0,0),2,1) 

Traceback (most recent call last): 

... 

NotImplementedError: plotting ellipse in 3D is not implemented 

 

We can also give ellipses a legend:: 

 

sage: ellipse((0,0),2,1,legend_label="My ellipse", legend_color='green') 

Graphics object consisting of 1 graphics primitive 

""" 

from sage.plot.all import Graphics 

g = Graphics() 

 

# Reset aspect_ratio to 'automatic' in case scale is 'semilog[xy]'. 

# Otherwise matplotlib complains. 

scale = options.get('scale', None) 

if isinstance(scale, (list, tuple)): 

scale = scale[0] 

if scale == 'semilogy' or scale == 'semilogx': 

options['aspect_ratio'] = 'automatic' 

 

g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 

g.add_primitive(Ellipse(center[0],center[1],r1,r2,angle,options)) 

if options['legend_label']: 

g.legend(True) 

g._legend_colors = [options['legend_color']] 

if len(center)==2: 

return g 

elif len(center)==3: 

raise NotImplementedError("plotting ellipse in 3D is not implemented")