Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/point.py
4034 views
1
"""
2
Points
3
4
TESTS::
5
6
sage: E = EllipticCurve('37a')
7
sage: P = E(0,0)
8
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])
9
sage: sum([get_points(15*n).plot3d(z=n) for n in range(1,10)])
10
"""
11
12
#*****************************************************************************
13
# Copyright (C) 2006 Alex Clemesha <[email protected]>,
14
# William Stein <[email protected]>,
15
# 2008 Mike Hansen <[email protected]>,
16
#
17
# Distributed under the terms of the GNU General Public License (GPL)
18
#
19
# This code is distributed in the hope that it will be useful,
20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22
# General Public License for more details.
23
#
24
# The full text of the GPL is available at:
25
#
26
# http://www.gnu.org/licenses/
27
#*****************************************************************************
28
from sage.misc.decorators import options, rename_keyword
29
from sage.plot.colors import to_mpl_color
30
from sage.plot.primitive import GraphicPrimitive_xydata
31
32
# TODO: create _allowed_options for 3D point classes to
33
# improve bad option handling in plot3d?
34
class Point(GraphicPrimitive_xydata):
35
"""
36
Primitive class for the point graphics type. See point?, point2d?
37
or point3d? for information about actually plotting points.
38
39
INPUT:
40
41
- xdata - list of x values for points in Point object
42
43
- ydata - list of y values for points in Point object
44
45
- options - dict of valid plot options to pass to constructor
46
47
EXAMPLES:
48
49
Note this should normally be used indirectly via ``point`` and friends::
50
51
sage: from sage.plot.point import Point
52
sage: P = Point([1,2],[2,3],{'alpha':.5})
53
sage: P
54
Point set defined by 2 point(s)
55
sage: P.options()['alpha']
56
0.500000000000000
57
sage: P.xdata
58
[1, 2]
59
60
TESTS:
61
62
We test creating a point::
63
64
sage: point((3,3))
65
"""
66
def __init__(self, xdata, ydata, options):
67
"""
68
Initializes base class Point.
69
70
EXAMPLES::
71
72
sage: P = point((3,4))
73
sage: P[0].xdata
74
[3.0]
75
sage: P[0].options()['alpha']
76
1
77
"""
78
self.xdata = xdata
79
self.ydata = ydata
80
GraphicPrimitive_xydata.__init__(self, options)
81
82
def _allowed_options(self):
83
"""
84
Return the allowed options for the Point class.
85
86
EXAMPLES::
87
88
sage: P = point((3,4))
89
sage: P[0]._allowed_options()['size']
90
'How big the point is (i.e., area in points^2=(1/72 inch)^2).'
91
"""
92
return {'alpha':'How transparent the point is.',
93
'legend_label':'The label for this item in the legend.',
94
'size': 'How big the point is (i.e., area in points^2=(1/72 inch)^2).',
95
'faceted': 'If True color the edge of the point.',
96
'rgbcolor':'The color as an RGB tuple.',
97
'hue':'The color given as a hue.',
98
'zorder':'The layer level in which to draw'}
99
100
def _plot3d_options(self, options=None):
101
"""
102
Translate 2D plot options into 3D plot options.
103
104
EXAMPLES::
105
106
sage: A=point((1,1),size=22)
107
sage: a=A[0];a
108
Point set defined by 1 point(s)
109
sage: b=a.plot3d()
110
sage: b.size
111
22
112
sage: b=a.plot3d(size=3)
113
sage: b.size
114
3
115
"""
116
if options == None:
117
options = dict(self.options())
118
options_3d = {}
119
if 'size' in options:
120
options_3d['size'] = options['size']
121
del options['size']
122
if 'faceted' in options:
123
if options['faceted']:
124
raise NotImplementedError, "No 3d faceted points."
125
del options['faceted']
126
options_3d.update(GraphicPrimitive_xydata._plot3d_options(self, options))
127
return options_3d
128
129
def plot3d(self, z=0, **kwds):
130
"""
131
Plots a two-dimensional point in 3-D, with default height zero.
132
133
INPUT:
134
135
136
- ``z`` - optional 3D height above `xy`-plane. May be a list
137
if self is a list of points.
138
139
EXAMPLES:
140
141
One point::
142
143
sage: A=point((1,1))
144
sage: a=A[0];a
145
Point set defined by 1 point(s)
146
sage: b=a.plot3d()
147
148
One point with a height::
149
150
sage: A=point((1,1))
151
sage: a=A[0];a
152
Point set defined by 1 point(s)
153
sage: b=a.plot3d(z=3)
154
sage: b.loc[2]
155
3.0
156
157
Multiple points::
158
159
sage: P=point([(0,0), (1,1)])
160
sage: p=P[0]; p
161
Point set defined by 2 point(s)
162
sage: q=p.plot3d(size=22)
163
164
Multiple points with different heights::
165
166
sage: P=point([(0,0), (1,1)])
167
sage: p=P[0]
168
sage: q=p.plot3d(z=[2,3])
169
sage: q.all[0].loc[2]
170
2.0
171
sage: q.all[1].loc[2]
172
3.0
173
174
Note that keywords passed must be valid point3d options::
175
176
sage: A=point((1,1),size=22)
177
sage: a=A[0];a
178
Point set defined by 1 point(s)
179
sage: b=a.plot3d()
180
sage: b.size
181
22
182
sage: b=a.plot3d(pointsize=23) # only 2D valid option
183
sage: b.size
184
22
185
sage: b=a.plot3d(size=23) # correct keyword
186
sage: b.size
187
23
188
189
TESTS:
190
191
Heights passed as a list should have same length as
192
number of points::
193
194
sage: P=point([(0,0), (1,1), (2,3)])
195
sage: p=P[0]
196
sage: q=p.plot3d(z=2)
197
sage: q.all[1].loc[2]
198
2.0
199
sage: q=p.plot3d(z=[2,-2])
200
Traceback (most recent call last):
201
...
202
ValueError: Incorrect number of heights given
203
"""
204
from sage.plot.plot3d.base import Graphics3dGroup
205
from sage.plot.plot3d.shapes2 import point3d
206
options = self._plot3d_options()
207
options.update(kwds)
208
zdata=[]
209
if type(z) is list:
210
zdata=z
211
else:
212
zdata=[z]*len(self.xdata)
213
if len(zdata)==len(self.xdata):
214
all = [point3d([(x, y, z) for x, y, z in zip(self.xdata, self.ydata, zdata)], **options)]
215
if len(all) == 1:
216
return all[0]
217
else:
218
return Graphics3dGroup(all)
219
else:
220
raise ValueError, 'Incorrect number of heights given'
221
222
def _repr_(self):
223
"""
224
String representation of Point primitive.
225
226
EXAMPLES::
227
228
sage: P=point([(0,0), (1,1)])
229
sage: p=P[0]; p
230
Point set defined by 2 point(s)
231
"""
232
return "Point set defined by %s point(s)"%len(self.xdata)
233
234
def __getitem__(self, i):
235
"""
236
Returns tuple of coordinates of point.
237
238
EXAMPLES::
239
240
sage: P=point([(0,0), (1,1), (2,3)])
241
sage: p=P[0]; p
242
Point set defined by 3 point(s)
243
sage: p[1]
244
(1.0, 1.0)
245
"""
246
return self.xdata[i], self.ydata[i]
247
248
def _render_on_subplot(self,subplot):
249
r"""
250
TESTS:
251
252
We check to make sure that \#2076 is fixed by verifying all
253
the points are red::
254
255
sage: point(((1,1), (2,2), (3,3)), rgbcolor=hue(1), size=30)
256
"""
257
options = self.options()
258
259
#Convert the color to a hex string so that the scatter
260
#method does not interpret it as a list of 3 floating
261
#point color specifications when there are
262
#three points. This is mentioned in the matplotlib 0.98
263
#documentation and fixes \#2076
264
from matplotlib.colors import rgb2hex
265
c = rgb2hex(to_mpl_color(options['rgbcolor']))
266
267
a = float(options['alpha'])
268
z = int(options.pop('zorder', 0))
269
s = int(options['size'])
270
faceted = options['faceted'] #faceted=True colors the edge of point
271
scatteroptions={}
272
if not faceted: scatteroptions['edgecolors'] = 'none'
273
subplot.scatter(self.xdata, self.ydata, s=s, c=c, alpha=a, zorder=z, label=options['legend_label'], **scatteroptions)
274
275
276
def point(points, **kwds):
277
"""
278
Returns either a 2-dimensional or 3-dimensional point or sum of points.
279
280
INPUT:
281
282
- ``points`` - either a single point (as a tuple) or a list of points.
283
284
For information regarding additional arguments, see either point2d?
285
or point3d?.
286
287
EXAMPLES::
288
289
sage: point((1,2))
290
291
::
292
293
sage: point((1,2,3))
294
295
::
296
297
sage: point([(0,0), (1,1)])
298
299
::
300
301
sage: point([(0,0,1), (1,1,1)])
302
303
Extra options will get passed on to show(), as long as they are valid::
304
305
sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True)
306
sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent
307
"""
308
try:
309
return point2d(points, **kwds)
310
except (ValueError, TypeError):
311
from sage.plot.plot3d.shapes2 import point3d
312
return point3d(points, **kwds)
313
314
@rename_keyword(color='rgbcolor', pointsize='size')
315
@options(alpha=1, size=10, faceted=False, rgbcolor=(0,0,1), legend_label=None, aspect_ratio='automatic')
316
def point2d(points, **options):
317
r"""
318
A point of size ``size`` defined by point = `(x,y)`.
319
Point takes either a single tuple of coordinates or a list of tuples.
320
321
Type ``point2d.options`` to see all options.
322
323
EXAMPLES:
324
325
A purple point from a single tuple or coordinates::
326
327
sage: point((0.5, 0.5), rgbcolor=hue(0.75))
328
329
Passing an empty list returns an empty plot::
330
331
sage: point([])
332
333
If you need a 2D point to live in 3-space later,
334
this is possible::
335
336
sage: A=point((1,1))
337
sage: a=A[0];a
338
Point set defined by 1 point(s)
339
sage: b=a.plot3d(z=3)
340
341
This is also true with multiple points::
342
343
sage: P=point([(0,0), (1,1)])
344
sage: p=P[0]
345
sage: q=p.plot3d(z=[2,3])
346
347
Here are some random larger red points, given as a list of tuples::
348
349
sage: point(((0.5, 0.5), (1, 2), (0.5, 0.9), (-1, -1)), rgbcolor=hue(1), size=30)
350
351
And an example with a legend::
352
353
sage: point((0,0), rgbcolor='black', pointsize=40, legend_label='origin')
354
355
Extra options will get passed on to show(), as long as they are valid::
356
357
sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)], frame=True)
358
sage: point([(cos(theta), sin(theta)) for theta in srange(0, 2*pi, pi/8)]).show(frame=True) # These are equivalent
359
360
Since Sage Version 4.4 (ticket #8599), the size of a 2d point can be
361
given by the argument ``size`` instead of ``pointsize``. The argument
362
``pointsize`` is still supported::
363
364
sage: point((3,4), size=100)
365
366
::
367
368
sage: point((3,4), pointsize=100)
369
"""
370
from sage.plot.plot import xydata_from_point_list
371
from sage.plot.all import Graphics
372
if points == []:
373
return Graphics()
374
xdata, ydata = xydata_from_point_list(points)
375
g = Graphics()
376
g._set_extra_kwds(Graphics._extract_kwds_for_show(options))
377
g.add_primitive(Point(xdata, ydata, options))
378
if options['legend_label']:
379
g.legend(True)
380
return g
381
382
points = point
383
384