Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/plot/primitive.py
8815 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
110
for o in ('legend_color', 'legend_label', 'zorder'):
111
if o in options:
112
del options[o]
113
114
if len(options) != 0:
115
raise NotImplementedError("Unknown plot3d equivalent for {}".format(
116
", ".join(options.keys())))
117
return options_3d
118
119
def set_zorder(self, zorder):
120
"""
121
Set the layer in which to draw the object.
122
123
EXAMPLES::
124
125
sage: P = line([(-2,-3), (3,4)], thickness=4)
126
sage: p=P[0]
127
sage: p.set_zorder(2)
128
sage: p.options()['zorder']
129
2
130
sage: Q = line([(-2,-4), (3,5)], thickness=4,zorder=1,hue=.5)
131
sage: P+Q # blue line on top
132
sage: q=Q[0]
133
sage: q.set_zorder(3)
134
sage: P+Q # teal line on top
135
sage: q.options()['zorder']
136
3
137
"""
138
self._options['zorder'] = zorder
139
140
def set_options(self, new_options):
141
"""
142
Change the options to `new_options`.
143
144
EXAMPLES::
145
146
sage: from sage.plot.circle import Circle
147
sage: c = Circle(0,0,1,{})
148
sage: c.set_options({'thickness': 0.6})
149
sage: c.options()
150
{'thickness': 0.6...}
151
"""
152
if new_options is not None: self._options = new_options
153
154
def options(self):
155
"""
156
Return the dictionary of options for this graphics primitive.
157
158
By default this function verifies that the options are all
159
valid; if any aren't, then a verbose message is printed with level 0.
160
161
EXAMPLES::
162
163
sage: from sage.plot.primitive import GraphicPrimitive
164
sage: GraphicPrimitive({}).options()
165
{}
166
"""
167
from sage.plot.graphics import do_verify
168
from sage.plot.colors import hue
169
O = dict(self._options)
170
if do_verify:
171
A = self._allowed_options()
172
t = False
173
K = A.keys() + ['xmin', 'xmax', 'ymin', 'ymax', 'axes']
174
for k in O.keys():
175
if not k in K:
176
do_verify = False
177
verbose("WARNING: Ignoring option '%s'=%s"%(k,O[k]), level=0)
178
t = True
179
if t:
180
s = "\nThe allowed options for %s are:\n"%self
181
K.sort()
182
for k in K:
183
if A.has_key(k):
184
s += " %-15s%-60s\n"%(k,A[k])
185
verbose(s, level=0)
186
187
188
if 'hue' in O:
189
t = O['hue']
190
if not isinstance(t, (tuple,list)):
191
t = [t,1,1]
192
O['rgbcolor'] = hue(*t)
193
del O['hue']
194
return O
195
196
def _repr_(self):
197
"""
198
String representation of this graphics primitive.
199
200
EXAMPLES::
201
202
sage: from sage.plot.primitive import GraphicPrimitive
203
sage: GraphicPrimitive({})._repr_()
204
'Graphics primitive'
205
"""
206
return "Graphics primitive"
207
208
209
210
class GraphicPrimitive_xydata(GraphicPrimitive):
211
def get_minmax_data(self):
212
"""
213
Returns a dictionary with the bounding box data.
214
215
EXAMPLES::
216
217
sage: d = polygon([[1,2], [5,6], [5,0]], rgbcolor=(1,0,1))[0].get_minmax_data()
218
sage: d['ymin']
219
0.0
220
sage: d['xmin']
221
1.0
222
223
::
224
225
sage: d = point((3, 3), rgbcolor=hue(0.75))[0].get_minmax_data()
226
sage: d['xmin']
227
3.0
228
sage: d['ymin']
229
3.0
230
231
::
232
233
sage: l = line([(100, 100), (120, 120)])[0]
234
sage: d = l.get_minmax_data()
235
sage: d['xmin']
236
100.0
237
sage: d['xmax']
238
120.0
239
240
"""
241
from sage.plot.plot import minmax_data
242
return minmax_data(self.xdata, self.ydata, dict=True)
243
244
245