Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/primitive.py
4034 views
1
"""
2
Plotting primitives
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2006 Alex Clemesha <[email protected]>,
6
# William Stein <[email protected]>,
7
# 2008 Mike Hansen <[email protected]>,
8
#
9
# Distributed under the terms of the GNU General Public License (GPL)
10
#
11
# This code is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
# General Public License for more details.
15
#
16
# The full text of the GPL is available at:
17
#
18
# http://www.gnu.org/licenses/
19
#*****************************************************************************
20
from sage.structure.sage_object import SageObject
21
from sage.misc.misc import verbose
22
23
class GraphicPrimitive(SageObject):
24
"""
25
Base class for graphics primitives, e.g., things that knows how to draw
26
themselves in 2D.
27
28
EXAMPLES:
29
30
We create an object that derives from GraphicPrimitive::
31
32
sage: P = line([(-1,-2), (3,5)])
33
sage: P[0]
34
Line defined by 2 points
35
sage: type(P[0])
36
<class 'sage.plot.line.Line'>
37
"""
38
def __init__(self, options):
39
"""
40
Create a base class GraphicsPrimitive. All this does is
41
set the options.
42
43
EXAMPLES:
44
45
We indirectly test this function::
46
47
sage: from sage.plot.primitive import GraphicPrimitive
48
sage: GraphicPrimitive({})
49
Graphics primitive
50
"""
51
self.__options = options
52
53
def _allowed_options(self):
54
"""
55
Return the allowed options for a graphics primitive.
56
57
OUTPUT:
58
59
- a reference to a dictionary.
60
61
EXAMPLES::
62
63
sage: from sage.plot.primitive import GraphicPrimitive
64
sage: GraphicPrimitive({})._allowed_options()
65
{}
66
"""
67
return {}
68
69
def plot3d(self, **kwds):
70
"""
71
Plots 3D version of 2D graphics object. Not implemented
72
for base class.
73
74
EXAMPLES::
75
76
sage: from sage.plot.primitive import GraphicPrimitive
77
sage: G=GraphicPrimitive({})
78
sage: G.plot3d()
79
Traceback (most recent call last):
80
...
81
NotImplementedError: 3D plotting not implemented for Graphics primitive
82
"""
83
raise NotImplementedError, "3D plotting not implemented for %s" % self._repr_()
84
85
def _plot3d_options(self, options=None):
86
"""
87
Translate 2D plot options into 3D plot options.
88
89
EXAMPLES::
90
91
sage: P = line([(-1,-2), (3,5)], alpha=.5, thickness=4)
92
sage: p = P[0]; p
93
Line defined by 2 points
94
sage: q=p.plot3d()
95
sage: q.thickness
96
4
97
sage: q.texture.opacity
98
0.500000000000000
99
"""
100
if options == None:
101
options = self.options()
102
options_3d = {}
103
if 'rgbcolor' in options:
104
options_3d['rgbcolor'] = options['rgbcolor']
105
del options['rgbcolor']
106
if 'alpha' in options:
107
options_3d['opacity'] = options['alpha']
108
del options['alpha']
109
if 'legend_label' in options:
110
del options['legend_label'] # no equivalent in 3d for now
111
if 'zorder' in options:
112
del options['zorder']
113
if len(options) != 0:
114
raise NotImplementedError, "Unknown plot3d equivalent for %s" % ", ".join(options.keys())
115
return options_3d
116
117
def set_zorder(self, zorder):
118
"""
119
Set the layer in which to draw the object.
120
121
EXAMPLES::
122
123
sage: P = line([(-2,-3), (3,4)], thickness=4)
124
sage: p=P[0]
125
sage: p.set_zorder(2)
126
sage: p.options()['zorder']
127
2
128
sage: Q = line([(-2,-4), (3,5)], thickness=4,zorder=1,hue=.5)
129
sage: P+Q # blue line on top
130
sage: q=Q[0]
131
sage: q.set_zorder(3)
132
sage: P+Q # teal line on top
133
sage: q.options()['zorder']
134
3
135
"""
136
self.__options['zorder'] = zorder
137
138
def options(self):
139
"""
140
Return the dictionary of options for this graphics primitive.
141
142
By default this function verifies that the options are all
143
valid; if any aren't, then a verbose message is printed with level 0.
144
145
EXAMPLES::
146
147
sage: from sage.plot.primitive import GraphicPrimitive
148
sage: GraphicPrimitive({}).options()
149
{}
150
"""
151
from sage.plot.graphics import do_verify
152
from sage.plot.colors import hue
153
O = dict(self.__options)
154
if do_verify:
155
A = self._allowed_options()
156
t = False
157
K = A.keys() + ['xmin', 'xmax', 'ymin', 'ymax', 'axes']
158
for k in O.keys():
159
if not k in K:
160
do_verify = False
161
verbose("WARNING: Ignoring option '%s'=%s"%(k,O[k]), level=0)
162
t = True
163
if t:
164
s = "\nThe allowed options for %s are:\n"%self
165
K.sort()
166
for k in K:
167
if A.has_key(k):
168
s += " %-15s%-60s\n"%(k,A[k])
169
verbose(s, level=0)
170
171
172
if 'hue' in O:
173
t = O['hue']
174
if not isinstance(t, (tuple,list)):
175
t = [t,1,1]
176
O['rgbcolor'] = hue(*t)
177
del O['hue']
178
return O
179
180
def _repr_(self):
181
"""
182
String representation of this graphics primitive.
183
184
EXAMPLES::
185
186
sage: from sage.plot.primitive import GraphicPrimitive
187
sage: GraphicPrimitive({})._repr_()
188
'Graphics primitive'
189
"""
190
return "Graphics primitive"
191
192
193
194
class GraphicPrimitive_xydata(GraphicPrimitive):
195
def get_minmax_data(self):
196
"""
197
Returns a dictionary with the bounding box data.
198
199
EXAMPLES::
200
201
sage: d = polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))[0].get_minmax_data()
202
sage: d['ymin']
203
0.0
204
sage: d['xmin']
205
1.0
206
207
::
208
209
sage: d = point((3, 3), rgbcolor=hue(0.75))[0].get_minmax_data()
210
sage: d['xmin']
211
3.0
212
sage: d['ymin']
213
3.0
214
215
::
216
217
sage: l = line([(100, 100), (120, 120)])[0]
218
sage: d = l.get_minmax_data()
219
sage: d['xmin']
220
100.0
221
sage: d['xmax']
222
120.0
223
224
"""
225
from sage.plot.plot import minmax_data
226
return minmax_data(self.xdata, self.ydata, dict=True)
227
228
229