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

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

""" 

Points 

 

TESTS:: 

 

sage: E = EllipticCurve('37a') 

sage: P = E(0,0) 

sage: def get_points(n): return sum([point(list(i*P)[:2], size=3) for i in range(-n,n) if i != 0 and (i*P)[0] < 3]) 

sage: sum([get_points(15*n).plot3d(z=n) for n in range(1,10)]) 

Graphics3d Object 

""" 

 

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

# Copyright (C) 2006 Alex Clemesha <clemesha@gmail.com>, 

# William Stein <wstein@gmail.com>, 

# 2008 Mike Hansen <mhansen@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 sage.misc.decorators import options, rename_keyword 

from sage.plot.colors import to_mpl_color 

from sage.plot.primitive import GraphicPrimitive_xydata 

import collections 

 

 

# TODO: create _allowed_options for 3D point classes to 

# improve bad option handling in plot3d? 

class Point(GraphicPrimitive_xydata): 

""" 

Primitive class for the point graphics type. See point?, point2d? 

or point3d? for information about actually plotting points. 

 

INPUT: 

 

- xdata -- list of x values for points in Point object 

 

- ydata -- list of y values for points in Point object 

 

- options -- dict of valid plot options to pass to constructor 

 

EXAMPLES: 

 

Note this should normally be used indirectly via ``point`` and friends:: 

 

sage: from sage.plot.point import Point 

sage: P = Point([1,2],[2,3],{'alpha':.5}) 

sage: P 

Point set defined by 2 point(s) 

sage: P.options()['alpha'] 

0.500000000000000 

sage: P.xdata 

[1, 2] 

 

TESTS: 

 

We test creating a point:: 

 

sage: point((3,3)) 

Graphics object consisting of 1 graphics primitive 

""" 

def __init__(self, xdata, ydata, options): 

""" 

Initializes base class Point. 

 

EXAMPLES:: 

 

sage: P = point((3,4)) 

sage: P[0].xdata 

[3.0] 

sage: P[0].options()['alpha'] 

1 

""" 

self.xdata = xdata 

self.ydata = ydata 

GraphicPrimitive_xydata.__init__(self, options) 

 

def _allowed_options(self): 

""" 

Return the allowed options for the Point class. 

 

EXAMPLES:: 

 

sage: P = point((3,4)) 

sage: P[0]._allowed_options()['size'] 

'How big the point is (i.e., area in points^2=(1/72 inch)^2).' 

""" 

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

'faceted': 'If True color the edge of the point. (only for 2D plots)', 

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

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

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

'marker':'the marker symbol for 2D plots only (see documentation of plot() for details)', 

'markeredgecolor':'the color of the marker edge (only for 2D plots)', 

'rgbcolor':'The color as an RGB tuple.', 

'size': 'How big the point is (i.e., area in points^2=(1/72 inch)^2).', 

'zorder':'The layer level in which to draw'} 

 

def _plot3d_options(self, options=None): 

""" 

Translate 2D plot options into 3D plot options. 

 

EXAMPLES:: 

 

sage: A=point((1,1),size=22) 

sage: a=A[0];a 

Point set defined by 1 point(s) 

sage: b=a.plot3d() 

sage: b.size 

22 

sage: b=a.plot3d(size=3) 

sage: b.size 

3 

""" 

if options is None: 

options = dict(self.options()) 

options_3d = {} 

if 'size' in options: 

options_3d['size'] = options['size'] 

del options['size'] 

if options.pop('faceted', False): 

raise NotImplementedError("3D points can not be faceted.") 

for o in ('marker', 'markeredgecolor'): # remove 2D options 

if o in options: 

del options[o] 

 

options_3d.update(GraphicPrimitive_xydata._plot3d_options(self, options)) 

return options_3d 

 

def plot3d(self, z=0, **kwds): 

""" 

Plots a two-dimensional point in 3-D, with default height zero. 

 

INPUT: 

 

 

- ``z`` - optional 3D height above `xy`-plane. May be a list 

if self is a list of points. 

 

EXAMPLES: 

 

One point:: 

 

sage: A=point((1,1)) 

sage: a=A[0];a 

Point set defined by 1 point(s) 

sage: b=a.plot3d() 

 

One point with a height:: 

 

sage: A=point((1,1)) 

sage: a=A[0];a 

Point set defined by 1 point(s) 

sage: b=a.plot3d(z=3) 

sage: b.loc[2] 

3.0 

 

Multiple points:: 

 

sage: P=point([(0,0), (1,1)]) 

sage: p=P[0]; p 

Point set defined by 2 point(s) 

sage: q=p.plot3d(size=22) 

 

Multiple points with different heights:: 

 

sage: P=point([(0,0), (1,1)]) 

sage: p=P[0] 

sage: q=p.plot3d(z=[2,3]) 

sage: q.all[0].loc[2] 

2.0 

sage: q.all[1].loc[2] 

3.0 

 

Note that keywords passed must be valid point3d options:: 

 

sage: A=point((1,1),size=22) 

sage: a=A[0];a 

Point set defined by 1 point(s) 

sage: b=a.plot3d() 

sage: b.size 

22 

sage: b=a.plot3d(pointsize=23) # only 2D valid option 

sage: b.size 

22 

sage: b=a.plot3d(size=23) # correct keyword 

sage: b.size 

23 

 

TESTS: 

 

Heights passed as a list should have same length as 

number of points:: 

 

sage: P=point([(0,0), (1,1), (2,3)]) 

sage: p=P[0] 

sage: q=p.plot3d(z=2) 

sage: q.all[1].loc[2] 

2.0 

sage: q=p.plot3d(z=[2,-2]) 

Traceback (most recent call last): 

... 

ValueError: Incorrect number of heights given 

""" 

from sage.plot.plot3d.base import Graphics3dGroup 

from sage.plot.plot3d.shapes2 import point3d 

options = self._plot3d_options() 

options.update(kwds) 

zdata=[] 

if isinstance(z, list): 

zdata=z 

else: 

zdata=[z]*len(self.xdata) 

if len(zdata)==len(self.xdata): 

all = [point3d([(x, y, z) for x, y, z in zip(self.xdata, self.ydata, zdata)], **options)] 

if len(all) == 1: 

return all[0] 

else: 

return Graphics3dGroup(all) 

else: 

raise ValueError('Incorrect number of heights given') 

 

def _repr_(self): 

""" 

String representation of Point primitive. 

 

EXAMPLES:: 

 

sage: P=point([(0,0), (1,1)]) 

sage: p=P[0]; p 

Point set defined by 2 point(s) 

""" 

return "Point set defined by %s point(s)"%len(self.xdata) 

 

def __getitem__(self, i): 

""" 

Returns tuple of coordinates of point. 

 

EXAMPLES:: 

 

sage: P=point([(0,0), (1,1), (2,3)]) 

sage: p=P[0]; p 

Point set defined by 3 point(s) 

sage: p[1] 

(1.0, 1.0) 

""" 

return self.xdata[i], self.ydata[i] 

 

def _render_on_subplot(self,subplot): 

r""" 

TESTS: 

 

We check to make sure that :trac:`2076` is fixed by verifying all 

the points are red:: 

 

sage: point(((1,1), (2,2), (3,3)), rgbcolor=hue(1), size=30) 

Graphics object consisting of 1 graphics primitive 

""" 

options = self.options() 

 

#Convert the color to a hex string so that the scatter 

#method does not interpret it as a list of 3 floating 

#point color specifications when there are 

#three points. This is mentioned in the matplotlib 0.98 

#documentation and fixes #2076 

from matplotlib.colors import rgb2hex 

c = rgb2hex(to_mpl_color(options['rgbcolor'])) 

 

a = float(options['alpha']) 

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

s = int(options['size']) 

faceted = options['faceted'] #faceted=True colors the edge of point 

markeredgecolor = options['markeredgecolor'] 

 

scatteroptions={} 

if not faceted and markeredgecolor is None: 

scatteroptions['edgecolors'] = 'none' 

elif markeredgecolor is not None: 

scatteroptions['edgecolors'] = to_mpl_color( 

options.pop('markeredgecolor')) 

scatteroptions['marker'] = options.pop('marker') 

 

subplot.scatter(self.xdata, self.ydata, s=s, c=c, alpha=a, zorder=z, 

label=options['legend_label'], **scatteroptions) 

 

 

def point(points, **kwds): 

""" 

Returns either a 2-dimensional or 3-dimensional point or sum of points. 

 

INPUT: 

 

- ``points`` - either a single point (as a tuple), a list of 

points, a single complex number, or a list of complex numbers. 

 

For information regarding additional arguments, see either point2d? 

or point3d?. 

 

.. SEEALSO:: 

 

:func:`sage.plot.point.point2d`, :func:`sage.plot.plot3d.shapes2.point3d` 

 

EXAMPLES:: 

 

sage: point((1,2)) 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

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

Graphics3d Object 

 

:: 

 

sage: point([(0,0), (1,1)]) 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: point([(0,0,1), (1,1,1)]) 

Graphics3d Object 

 

Extra options will get passed on to show(), as long as they are valid:: 

 

sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) 

Graphics object consisting of 1 graphics primitive 

sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent 

 

TESTS: 

 

One can now use iterators (:trac:`13890`):: 

 

sage: point(iter([(1,1,1)])) 

Graphics3d Object 

sage: point(iter([(1,2),(3,5)])) 

Graphics object consisting of 1 graphics primitive 

""" 

if isinstance(points, collections.Iterator): 

points = list(points) 

 

try: 

return point2d(points, **kwds) 

except (ValueError, TypeError): 

from sage.plot.plot3d.shapes2 import point3d 

return point3d(points, **kwds) 

 

@rename_keyword(color='rgbcolor', pointsize='size') 

@options(alpha=1, aspect_ratio='automatic', faceted=False, 

legend_color=None, legend_label=None, marker='o', 

markeredgecolor=None, rgbcolor=(0,0,1), size=10) 

def point2d(points, **options): 

r""" 

A point of size ``size`` defined by point = `(x,y)`. 

 

INPUT: 

 

- ``points`` - either a single point (as a tuple), a list of 

points, a single complex number, or a list of complex numbers. 

- ``alpha`` -- How transparent the point is. 

- ``faceted`` -- If True color the edge of the point. (only for 2D plots) 

- ``hue`` -- The color given as a hue. 

- ``legend_color`` -- The color of the legend text 

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

- ``marker`` -- the marker symbol for 2D plots only (see documentation of 

:func:`plot` for details) 

- ``markeredgecolor`` -- the color of the marker edge (only for 2D plots) 

- ``rgbcolor`` -- The color as an RGB tuple. 

- ``size`` -- How big the point is (i.e., area in points^2=(1/72 inch)^2). 

- ``zorder`` -- The layer level in which to draw 

 

EXAMPLES: 

 

A purple point from a single tuple or coordinates:: 

 

sage: point((0.5, 0.5), rgbcolor=hue(0.75)) 

Graphics object consisting of 1 graphics primitive 

 

Points with customized markers and edge colors:: 

 

sage: r = [(random(), random()) for _ in range(10)] 

sage: point(r, marker='d', markeredgecolor='red', size=20) 

Graphics object consisting of 1 graphics primitive 

 

Passing an empty list returns an empty plot:: 

 

sage: point([]) 

Graphics object consisting of 0 graphics primitives 

sage: import numpy; point(numpy.array([])) 

Graphics object consisting of 0 graphics primitives 

 

If you need a 2D point to live in 3-space later, this is possible:: 

 

sage: A=point((1,1)) 

sage: a=A[0];a 

Point set defined by 1 point(s) 

sage: b=a.plot3d(z=3) 

 

This is also true with multiple points:: 

 

sage: P=point([(0,0), (1,1)]) 

sage: p=P[0] 

sage: q=p.plot3d(z=[2,3]) 

 

Here are some random larger red points, given as a list of tuples:: 

 

sage: point(((0.5, 0.5), (1, 2), (0.5, 0.9), (-1, -1)), rgbcolor=hue(1), size=30) 

Graphics object consisting of 1 graphics primitive 

 

And an example with a legend:: 

 

sage: point((0,0), rgbcolor='black', pointsize=40, legend_label='origin') 

Graphics object consisting of 1 graphics primitive 

 

The legend can be colored:: 

 

sage: P = points([(0,0),(1,0)], pointsize=40, legend_label='origin', legend_color='red') 

sage: P + plot(x^2,(x,0,1), legend_label='plot', legend_color='green') 

Graphics object consisting of 2 graphics primitives 

 

Extra options will get passed on to show(), as long as they are valid:: 

 

sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True) 

Graphics object consisting of 1 graphics primitive 

sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent 

 

For plotting data, we can use a logarithmic scale, as long as we are sure 

not to include any nonpositive points in the logarithmic direction:: 

 

sage: point([(1,2),(2,4),(3,4),(4,8),(4.5,32)],scale='semilogy',base=2) 

Graphics object consisting of 1 graphics primitive 

 

Since Sage Version 4.4 (:trac:`8599`), the size of a 2d point can be 

given by the argument ``size`` instead of ``pointsize``. The argument 

``pointsize`` is still supported:: 

 

sage: point((3,4), size=100) 

Graphics object consisting of 1 graphics primitive 

 

:: 

 

sage: point((3,4), pointsize=100) 

Graphics object consisting of 1 graphics primitive 

 

We can plot a single complex number:: 

 

sage: point(CC(1+I), pointsize=100) 

Graphics object consisting of 1 graphics primitive 

 

We can also plot a list of complex numbers:: 

 

sage: point([CC(I), CC(I+1), CC(2+2*I)], pointsize=100) 

Graphics object consisting of 1 graphics primitive 

 

TESTS:: 

 

sage: point2d(iter([])) 

Graphics object consisting of 0 graphics primitives 

""" 

from sage.plot.plot import xydata_from_point_list 

from sage.plot.all import Graphics 

from sage.rings.all import CC, CDF 

if points in CC or points in CDF: 

pass 

else: 

try: 

l = len(points) 

except TypeError: 

# argument is an iterator 

points = list(points) 

l = len(points) 

 

if l == 0: 

return Graphics() 

 

xdata, ydata = xydata_from_point_list(points) 

g = Graphics() 

g._set_extra_kwds(Graphics._extract_kwds_for_show(options)) 

g.add_primitive(Point(xdata, ydata, options)) 

if options['legend_label']: 

g.legend(True) 

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

return g 

 

points = point