List plots - 3D Graphics

Skip to content
Back to top
View this page
Edit this page

# List plots

sage.plot.plot3d.list_plot3d.list_plot3d(v, interpolation_type='default', point_list=None, **kwds)

  A 3-dimensional plot of a surface defined by the list \(v\) of points in 3-dimensional space.

  INPUT:

  - `v` – something that defines a set of points in 3 space:

    - a matrix

    - a list of 3-tuples

    - a list of lists (all of the same length) – this is treated the same as a matrix

  OPTIONAL KEYWORDS:

  - `interpolation_type` – ‘linear’, ‘clough’ (CloughTocher2D), ‘spline’

    ‘linear’ will perform linear interpolation

    The option ‘clough’ will interpolate by using a piecewise cubic interpolating Bezier polynomial on each triangle, using a Clough-Tocher scheme. The interpolant is guaranteed to be continuously differentiable. The gradients of the interpolant are chosen so that the curvature of the interpolating surface is approximately minimized.

    The option ‘spline’ interpolates using a bivariate B-spline.

    When v is a matrix the default is to use linear interpolation, when v is a list of points the default is ‘clough’.

  - `degree` – integer between 1 and 5, controls the degree of spline used for spline interpolation. For data that is highly oscillatory use higher values

  - `point_list` – if `point_list=True` is passed, then if the array is a list of lists of length three, it will be treated as an array of points rather than a 3xn array.

  - `num_points` – number of points to sample interpolating function in each direction, when `interpolation_type` is not `default`. By default for an \(n\times n\) array this is \(n\).

  - `**kwds` – all other arguments are passed to the surface function

  OUTPUT: a 3d plot

  EXAMPLES:

  We plot a matrix that illustrates summation modulo \(n\):

  Sage

      sage: n = 5
      sage: list_plot3d(matrix(RDF, n, [(i+j)%n for i in [1..n] for j in [1..n]]))
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> n = Integer(5)
      >>> list_plot3d(matrix(RDF, n, [(i+j)%n for i in (ellipsis_range(Integer(1),Ellipsis,n)) for j in (ellipsis_range(Integer(1),Ellipsis,n))]))
      Graphics3d Object

  We plot a matrix of values of `sin`:

  Sage

      sage: from math import pi
      sage: m = matrix(RDF, 6, [sin(i^2 + j^2)
      ....:                     for i in [0,pi/5,..,pi] for j in [0,pi/5,..,pi]])
      sage: list_plot3d(m, color='yellow', frame_aspect_ratio=[1, 1, 1/3])
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> from math import pi
      >>> m = matrix(RDF, Integer(6), [sin(i**Integer(2) + j**Integer(2))
      ...                     for i in (ellipsis_range(Integer(0),pi/Integer(5),Ellipsis,pi)) for j in (ellipsis_range(Integer(0),pi/Integer(5),Ellipsis,pi))])
      >>> list_plot3d(m, color='yellow', frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])
      Graphics3d Object

  Though it does not change the shape of the graph, increasing `num_points` can increase the clarity of the graph:

  Sage

      sage: list_plot3d(m, color='yellow', num_points=40,
      ....:             frame_aspect_ratio=[1, 1, 1/3])
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> list_plot3d(m, color='yellow', num_points=Integer(40),
      ...             frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])
      Graphics3d Object

  We can change the interpolation type:

  Sage

      sage: import warnings
      sage: warnings.simplefilter('ignore', UserWarning)
      sage: list_plot3d(m, color='yellow', interpolation_type='clough',
      ....:             frame_aspect_ratio=[1, 1, 1/3])
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> import warnings
      >>> warnings.simplefilter('ignore', UserWarning)
      >>> list_plot3d(m, color='yellow', interpolation_type='clough',
      ...             frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])
      Graphics3d Object

  We can make this look better by increasing the number of samples:

  Sage

      sage: list_plot3d(m, color='yellow', interpolation_type='clough',
      ....:             frame_aspect_ratio=[1, 1, 1/3], num_points=40)
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> list_plot3d(m, color='yellow', interpolation_type='clough',
      ...             frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)], num_points=Integer(40))
      Graphics3d Object

  Let us try a spline:

  Sage

      sage: list_plot3d(m, color='yellow', interpolation_type='spline',
      ....:             frame_aspect_ratio=[1, 1, 1/3])
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> list_plot3d(m, color='yellow', interpolation_type='spline',
      ...             frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])
      Graphics3d Object

  That spline does not capture the oscillation very well; let’s try a higher degree spline:

  Sage

      sage: list_plot3d(m, color='yellow', interpolation_type='spline', degree=5,
      ....:             frame_aspect_ratio=[1, 1, 1/3])
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> list_plot3d(m, color='yellow', interpolation_type='spline', degree=Integer(5),
      ...             frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])
      Graphics3d Object

  We plot a list of lists:

  Sage

      sage: show(list_plot3d([[1, 1, 1, 1], [1, 2, 1, 2], [1, 1, 3, 1], [1, 2, 1, 4]]))

  [Python]

      >>> from sage.all import *
      >>> show(list_plot3d([[Integer(1), Integer(1), Integer(1), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(2)], [Integer(1), Integer(1), Integer(3), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(4)]]))

  We plot a list of points. As a first example we can extract the (x,y,z) coordinates from the above example and make a list plot out of it. By default we do linear interpolation:

  Sage

      sage: l = []
      sage: for i in range(6):
      ....:     for j in range(6):
      ....:         l.append((float(i*pi/5), float(j*pi/5), m[i, j]))
      sage: list_plot3d(l, color='red')
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> l = []
      >>> for i in range(Integer(6)):
      ...     for j in range(Integer(6)):
      ...         l.append((float(i*pi/Integer(5)), float(j*pi/Integer(5)), m[i, j]))
      >>> list_plot3d(l, color='red')
      Graphics3d Object

  Note that the points do not have to be regularly sampled. For example:

  Sage

      sage: l = []
      sage: for i in range(-5, 5):
      ....:     for j in range(-5, 5):
      ....:         l.append((normalvariate(0, 1),
      ....:                   normalvariate(0, 1),
      ....:                   normalvariate(0, 1)))
      sage: L = list_plot3d(l, interpolation_type='clough',
      ....:                 color='orange', num_points=100)
      sage: L
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> l = []
      >>> for i in range(-Integer(5), Integer(5)):
      ...     for j in range(-Integer(5), Integer(5)):
      ...         l.append((normalvariate(Integer(0), Integer(1)),
      ...                   normalvariate(Integer(0), Integer(1)),
      ...                   normalvariate(Integer(0), Integer(1))))
      >>> L = list_plot3d(l, interpolation_type='clough',
      ...                 color='orange', num_points=Integer(100))
      >>> L
      Graphics3d Object

  Check that no NaNs are produced (see Issue #13135):

  Sage

      sage: any(math.isnan(c) for v in L.vertices() for c in v)
      False

  Python

      >>> from sage.all import *
      >>> any(math.isnan(c) for v in L.vertices() for c in v)
      False

sage.plot.plot3d.list_plot3d.list_plot3d_array_of_arrays(v, interpolation_type, **kwds)

  A 3-dimensional plot of a surface defined by a list of lists `v` defining points in 3-dimensional space.

  This is done by making the list of lists into a matrix and passing back to `list_plot3d()`. See `list_plot3d()` for full details.

  INPUT:

  - `v` – list of lists, all the same length

  - `interpolation_type` – (default: `'linear'`)

  OPTIONAL KEYWORDS:

  - `**kwds` – all other arguments are passed to the surface function

  OUTPUT: a 3d plot

  EXAMPLES:

  The resulting matrix does not have to be square:

  Sage

      sage: show(list_plot3d([[1, 1, 1, 1], [1, 2, 1, 2], [1, 1, 3, 1]])) # indirect doctest

  Python

      >>> from sage.all import *
      >>> show(list_plot3d([[Integer(1), Integer(1), Integer(1), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(2)], [Integer(1), Integer(1), Integer(3), Integer(1)]])) # indirect doctest

  The normal route is for the list of lists to be turned into a matrix and use `list_plot3d_matrix()`:

  Sage

      sage: show(list_plot3d([[1, 1, 1, 1], [1, 2, 1, 2], [1, 1, 3, 1], [1, 2, 1, 4]]))

  [Python]

      >>> from sage.all import *
      >>> show(list_plot3d([[Integer(1), Integer(1), Integer(1), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(2)], [Integer(1), Integer(1), Integer(3), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(4)]]))

  With certain extra keywords (see `list_plot3d_matrix()`), this function will end up using `list_plot3d_tuples()`:

  Sage

      sage: show(list_plot3d([[1, 1, 1, 1], [1, 2, 1, 2], [1, 1, 3, 1], [1, 2, 1, 4]],
      ....:                  interpolation_type='spline'))

  Python

      >>> from sage.all import *
      >>> show(list_plot3d([[Integer(1), Integer(1), Integer(1), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(2)], [Integer(1), Integer(1), Integer(3), Integer(1)], [Integer(1), Integer(2), Integer(1), Integer(4)]],
      ...                  interpolation_type='spline'))

sage.plot.plot3d.list_plot3d.list_plot3d_matrix(m, **kwds)

  A 3-dimensional plot of a surface defined by a matrix `M` defining points in 3-dimensional space.

  See `list_plot3d()` for full details.

  INPUT:

  - `M` – a matrix

  OPTIONAL KEYWORDS:

  - `**kwds` – all other arguments are passed to the surface function

  OUTPUT: a 3d plot

  EXAMPLES:

  We plot a matrix that illustrates summation modulo \(n\):

  Sage

      sage: n = 5
      sage: list_plot3d(matrix(RDF, n, [(i+j) % n         # indirect doctest
      ....:                             for i in [1..n] for j in [1..n]]))
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> n = Integer(5)
      >>> list_plot3d(matrix(RDF, n, [(i+j) % n         # indirect doctest
      ...                             for i in (ellipsis_range(Integer(1),Ellipsis,n)) for j in (ellipsis_range(Integer(1),Ellipsis,n))]))
      Graphics3d Object

  The interpolation type for matrices is ‘linear’; for other types use other `list_plot3d()` input types.

  We plot a matrix of values of \(sin\):

  Sage

      sage: from math import pi
      sage: m = matrix(RDF, 6, [sin(i^2 + j^2)
      ....:                     for i in [0,pi/5,..,pi] for j in [0,pi/5,..,pi]])
      sage: list_plot3d(m, color='yellow', frame_aspect_ratio=[1, 1, 1/3])  # indirect doctest
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> from math import pi
      >>> m = matrix(RDF, Integer(6), [sin(i**Integer(2) + j**Integer(2))
      ...                     for i in (ellipsis_range(Integer(0),pi/Integer(5),Ellipsis,pi)) for j in (ellipsis_range(Integer(0),pi/Integer(5),Ellipsis,pi))])
      >>> list_plot3d(m, color='yellow', frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])  # indirect doctest
      Graphics3d Object

  ::

    sage: list_plot3d(m, color=’yellow’, interpolation_type=’linear’) # indirect doctest Graphics3d Object

  Here is a colored example, using a colormap and a coloring function which must take values in (0, 1):

  Sage

      sage: cm = colormaps.rainbow
      sage: n = 20
      sage: cf = lambda x, y: ((2*(x-y)/n)**2) % 1
      sage: list_plot3d(matrix(RDF, n, [cos(pi*(i+j)/n) for i in [1..n]
      ....:                             for j in [1..n]]), color=(cf,cm))
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> cm = colormaps.rainbow
      >>> n = Integer(20)
      >>> cf = lambda x, y: ((Integer(2)*(x-y)/n)**Integer(2)) % Integer(1)
      >>> list_plot3d(matrix(RDF, n, [cos(pi*(i+j)/n) for i in (ellipsis_range(Integer(1),Ellipsis,n))
      ...                             for j in (ellipsis_range(Integer(1),Ellipsis,n))]), color=(cf,cm))
      Graphics3d Object

sage.plot.plot3d.list_plot3d.list_plot3d_tuples(v, interpolation_type, **kwds)

  A 3-dimensional plot of a surface defined by the list \(v\) of points in 3-dimensional space.

  INPUT:

  - `v` – something that defines a set of points in 3 space, for example:

    - a matrix

      This will be if using an `interpolation_type` other than `'linear'`, or if using `num_points` with `'linear'`; otherwise see `list_plot3d_matrix()`.

    - a list of 3-tuples

    - a list of lists (all of the same length, under same conditions as a matrix)

  OPTIONAL KEYWORDS:

  - `interpolation_type` – one of `'linear'`, `'clough'` (CloughTocher2D), `'spline'`

    `'linear'` will perform linear interpolation

    The option ‘clough’ will interpolate by using a piecewise cubic interpolating Bezier polynomial on each triangle, using a Clough-Tocher scheme. The interpolant is guaranteed to be continuously differentiable.

    The option `'spline'` interpolates using a bivariate B-spline.

    When `v` is a matrix the default is to use linear interpolation, when `v` is a list of points the default is `'clough'`.

  - `degree` – integer between 1 and 5, controls the degree of spline used for spline interpolation. For data that is highly oscillatory use higher values

  - `point_list` – if `point_list=True` is passed, then if the array is a list of lists of length three, it will be treated as an array of points rather than a \(3\times n\) array.

  - `num_points` – number of points to sample interpolating function in each direction. By default for an \(n\times n\) array this is \(n\).

  - `**kwds` – all other arguments are passed to the surface function

  OUTPUT: a 3d plot

  EXAMPLES:

  All of these use this function; see `list_plot3d()` for other list plots:

  Sage

      sage: from math import pi
      sage: m = matrix(RDF, 6, [sin(i^2 + j^2)
      ....:                     for i in [0,pi/5,..,pi] for j in [0,pi/5,..,pi]])
      sage: list_plot3d(m, color='yellow', interpolation_type='linear',  # indirect doctest
      ....:             num_points=5)
      Graphics3d Object

  Python

      >>> from sage.all import *
      >>> from math import pi
      >>> m = matrix(RDF, Integer(6), [sin(i**Integer(2) + j**Integer(2))
      ...                     for i in (ellipsis_range(Integer(0),pi/Integer(5),Ellipsis,pi)) for j in (ellipsis_range(Integer(0),pi/Integer(5),Ellipsis,pi))])
      >>> list_plot3d(m, color='yellow', interpolation_type='linear',  # indirect doctest
      ...             num_points=Integer(5))
      Graphics3d Object

  Sage

      sage: list_plot3d(m, color='yellow', interpolation_type='spline',
      ....:             frame_aspect_ratio=[1, 1, 1/3])
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> list_plot3d(m, color='yellow', interpolation_type='spline',
      ...             frame_aspect_ratio=[Integer(1), Integer(1), Integer(1)/Integer(3)])
      Graphics3d Object

  Sage

      sage: show(list_plot3d([[1, 1, 1], [1, 2, 1], [0, 1, 3], [1, 0, 4]],
      ....:                  point_list=True))

  Python

      >>> from sage.all import *
      >>> show(list_plot3d([[Integer(1), Integer(1), Integer(1)], [Integer(1), Integer(2), Integer(1)], [Integer(0), Integer(1), Integer(3)], [Integer(1), Integer(0), Integer(4)]],
      ...                  point_list=True))

  Sage

      sage: list_plot3d([(1, 2, 3), (0, 1, 3), (2, 1, 4), (1, 0, -2)],  # long time
      ....:             color='yellow', num_points=50)
      Graphics3d Object

  [Python]

      >>> from sage.all import *
      >>> list_plot3d([(Integer(1), Integer(2), Integer(3)), (Integer(0), Integer(1), Integer(3)), (Integer(2), Integer(1), Integer(4)), (Integer(1), Integer(0), -Integer(2))],  # long time
      ...             color='yellow', num_points=Integer(50))
      Graphics3d Object