Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/step.py
4034 views
1
"""
2
Step function plots
3
"""
4
5
#*****************************************************************************
6
# Copyright (C) 2009 William Stein <[email protected]>,
7
#
8
# Distributed under the terms of the GNU General Public License (GPL)
9
#
10
# This code is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
# General Public License for more details.
14
#
15
# The full text of the GPL is available at:
16
#
17
# http://www.gnu.org/licenses/
18
#*****************************************************************************
19
20
def plot_step_function(v, vertical_lines=True, **kwds):
21
r"""
22
Return the line graphics object that gives the plot of the step
23
function `f` defined by the list `v` of pairs `(a,b)`. Here if
24
`(a,b)` is in `v`, then `f(a) = b`. The user does not have to
25
worry about sorting the input list `v`.
26
27
INPUT:
28
29
- ``v`` -- list of pairs (a,b)
30
31
- ``vertical_lines`` -- bool (default: True) if True, draw
32
vertical risers at each step of this step function.
33
Technically these vertical lines are not part of the graph
34
of this function, but they look very nice in the plot so we
35
include them by default
36
37
EXAMPLES:
38
39
We plot the prime counting function::
40
41
sage: plot_step_function([(i,prime_pi(i)) for i in range(20)])
42
43
sage: plot_step_function([(i,sin(i)) for i in range(5,20)])
44
45
We pass in many options and get something that looks like "Space Invaders"::
46
47
sage: v = [(i,sin(i)) for i in range(5,20)]
48
sage: plot_step_function(v, vertical_lines=False, thickness=30, rgbcolor='purple', axes=False)
49
"""
50
from plot import line
51
# make sorted copy of v (don't change in place, since that would be rude).
52
v = list(sorted(v))
53
if len(v) <= 1:
54
return line([]) # empty line
55
if vertical_lines:
56
w = []
57
for i in range(len(v)):
58
w.append(v[i])
59
if i+1 < len(v):
60
w.append((v[i+1][0],v[i][1]))
61
return line(w, **kwds)
62
else:
63
return sum(line([v[i],(v[i+1][0],v[i][1])], **kwds) for i in range(len(v)-1))
64
65