Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/plot/plot3d/plot_field3d.py
4036 views
1
"""
2
Plotting 3D fields
3
"""
4
#*****************************************************************************
5
# Copyright (C) 2009 Jason Grout <[email protected]>
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
#
9
# This code is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
# General Public License for more details.
13
#
14
# The full text of the GPL is available at:
15
#
16
# http://www.gnu.org/licenses/
17
#*****************************************************************************
18
19
from sage.misc.misc import srange
20
from sage.plot.misc import setup_for_eval_on_grid
21
from sage.modules.free_module_element import vector
22
from sage.plot.plot import plot
23
24
def plot_vector_field3d(functions, xrange, yrange, zrange,
25
plot_points=5, colors='jet', center_arrows=False,**kwds):
26
r"""
27
Plot a 3d vector field
28
29
INPUT:
30
31
- ``functions`` - a list of three functions, representing the x-,
32
y-, and z-coordinates of a vector
33
34
- ``xrange``, ``yrange``, and ``zrange`` - three tuples of the
35
form (var, start, stop), giving the variables and ranges for each axis
36
37
- ``plot_points`` (default 5) - either a number or list of three
38
numbers, specifying how many points to plot for each axis
39
40
- ``colors`` (default 'jet') - a color, list of colors (which are
41
interpolated between), or matplotlib colormap name, giving the coloring
42
of the arrows. If a list of colors or a colormap is given,
43
coloring is done as a function of length of the vector
44
45
- ``center_arrows`` (default False) - If True, draw the arrows
46
centered on the points; otherwise, draw the arrows with the tail
47
at the point
48
49
- any other keywords are passed on to the plot command for each arrow
50
51
EXAMPLES::
52
53
sage: x,y,z=var('x y z')
54
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi))
55
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),colors=['red','green','blue'])
56
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),colors='red')
57
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),plot_points=4)
58
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),plot_points=[3,5,7])
59
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),center_arrows=True)
60
61
TESTS:
62
63
This tests that Trac # 2100 is fixed in a way compatible with this command::
64
65
sage: plot_vector_field3d((x*cos(z),-y*cos(z),sin(z)), (x,0,pi), (y,0,pi), (z,0,pi),center_arrows=True,aspect_ratio=(1,2,1))
66
"""
67
(ff,gg,hh), ranges = setup_for_eval_on_grid(functions, [xrange, yrange, zrange], plot_points)
68
xpoints, ypoints, zpoints = [srange(*r, include_endpoint=True) for r in ranges]
69
points = [vector((i,j,k)) for i in xpoints for j in ypoints for k in zpoints]
70
vectors = [vector((ff(*point), gg(*point), hh(*point))) for point in points]
71
72
try:
73
from matplotlib.cm import get_cmap
74
cm = get_cmap(colors)
75
assert(cm is not None)
76
except (TypeError, AssertionError):
77
if isinstance(colors, (list, tuple)):
78
from matplotlib.colors import LinearSegmentedColormap
79
cm = LinearSegmentedColormap.from_list('mymap',colors)
80
else:
81
cm = lambda x: colors
82
83
max_len = max(v.norm() for v in vectors)
84
scaled_vectors = [v/max_len for v in vectors]
85
86
if center_arrows:
87
return sum([plot(v,color=cm(v.norm()),**kwds).translate(p-v/2) for v,p in zip(scaled_vectors, points)])
88
else:
89
return sum([plot(v,color=cm(v.norm()),**kwds).translate(p) for v,p in zip(scaled_vectors, points)])
90
91
92