示例#1
0
def scatter(points,
            color=None,
            opacity=None,
            radius=1.,
            use_cursors=False,
            fig="gcf"):
    """Scatter plot using little spheres or cursor objects.

    :param points: The point(s) to place the marker(s) at.
    :type points: np.array with ``points.shape[-1] == 3``

    :param color: The color of the markers, can be singular or per marker, defaults to white.
    :type color: str, 3-tuple, 4-tuple, np.array with same shape as `points`, optional

    :param opacity: The translucencies of the plots, 0 is invisible, 1 is solid, defaults to solid.
    :type opacity: float, np.array, optional

    :param radius: The radius of each marker, defaults to 1.0.
    :type radius: float, np.array, optional

    :param use_cursors: If false use spheres, if true use cursors, defaults to False.
    :type use_cursors: bool, optional

    :param fig: The figure to plot into, can be None, defaults to vpl.gcf().
    :type fig: vpl.figure, vpl.QtFigure, optional


    :return: The marker or an array of markers.
    :rtype: vtkplotlib.plots.Scatter.Sphere or vtkplotlib.plots.Scatter.Cursor or np.array


    """

    points = np.asarray(points)
    out = np.empty(points.shape[:-1], object)
    out_flat = out.ravel()
    for (i, (xyz, c, r)) in enumerate(
            zip(_iter_points(points), _iter_colors(color, points.shape[:-1]),
                _iter_scalar(radius, points.shape[:-1]))):

        if use_cursors:
            cls = Cursor
        else:
            cls = Sphere

        out_flat[i] = cls(xyz, c, opacity, r, fig)

    if out.ndim:
        return out
    else:
        return out_flat[0]
示例#2
0
def cmap_from_list(colors, opacities=None, scalars=None, resolution=None):
    """Create a colormap from a list of colors. Unlike matplotlib's
    ``ListedColormap``, this method will interpolate between the input
    **colors** to give a smooth map.

    :param colors: A list colors.
    :type colors: list of valid colors as defined by :meth:`as_rgb_a`

    :param opacities: Translucency or translucencies, defaults to ``None``.
    :type opacities: Scalar from 0 to 1 or array-like of scalars, optional

    :param scalars: Control scalars to correspond exact colors from **color**, defaults to ``np.arange(len(colors))``.
    :type scalars: array-like with same length as **colors**, optional

    :param resolution: Number of colors in output, defaults to ``(len(colors) - 1) * 255 + 1``.
    :type resolution: int, optional

    :return: An array of RGBA values.
    :rtype: ``np.ndarray`` with shape ``(n, 4)`` and dtype ``np.uint8``

    The output can be fed either to :meth:`as_vtk_cmap` or passed directly as a
    **cmap** argument to any vtkplotlib method that takes one.

    """
    from vtkplotlib.plots.BasePlot import _iter_scalar
    from vtkplotlib.nuts_and_bolts import zip_axes, unzip_axes
    n = len(colors)

    rgbas = np.empty((len(colors), 4))

    for (i, color, opacity) in zip(range(n), colors,
                                   _iter_scalar(opacities, n)):
        color, opacity = as_rgb_a(color, opacity)
        rgbas[i, :3], rgbas[i:,
                            3] = color, (1. if opacity is None else opacity)

    if scalars is None:
        scalars = np.arange(n)

    if resolution is None:
        resolution = (n - 1) * 255 + 1

    ts = np.linspace(scalars[0], scalars[-1], resolution)

    arr = zip_axes(*(np.interp(ts, scalars, i) for i in unzip_axes(rgbas)))

    return arr
示例#3
0
def arrow(start,
          end,
          length=None,
          width_scale=1.,
          color=None,
          opacity=None,
          fig="gcf"):
    """Draw (an) arrow(s) from `start` to `end`.

    :param start: The starting point(s) of the arrow(s).
    :type start: np.ndarray

    :param end: The end point(s) of the arrow(s).
    :type end:  np.ndarray

    :param length: The length of the arrow(s), defaults to None.
    :type length: number, np.ndarray, optional

    :param width_scale: How fat to make each arrow, is relative to its length, defaults to 1.0.
    :type width_scale: number, np.ndarray, optional

    :param color: The color of each arrow, defaults to white.
    :type color: str, 3-tuple, 4-tuple, np.ndarray of shape(n, 3)

    :param opacity: The translucency of each arrow, 0 is invisible, 1 is solid, defaults to solid.
    :type opacity: float

    :param fig: The figure to plot into, can be None, defaults to vpl.gcf().
    :type fig: vpl.figure, vpl.QtFigure


    :return: arrow or array of arrows
    :rtype: vtkplotlib.plots.Arrow.Arrow, np.array of Arrows


    The shapes of `start` and `end` should match. Arrow lengths are
    automatically calculated via pythagoras if not provided but can be
    overwritten by setting `length`. In this case the arrow(s) will always
    start at `start` but may not end at `end`. `length` can either be a single
    value for all arrows or an array of lengths to match the number of arrows.

    .. note::

        arrays are supported only for convenience and just use a python for
        loop. There is no speed bonus to using numpy or trying to plot in bulk
        here.

    .. seealso:: ``vpl.quiver`` for field plots.

    """

    start = np.asarray(start)
    end = np.asarray(end)

    assert start.shape == end.shape

    out = np.empty(start.shape[:-1], object)
    out_flat = out.ravel()

    for (i, s, e, l, c) in zip(range(out.size), _iter_points(start),
                               _iter_points(end),
                               _iter_scalar(length, start.shape[:-1]),
                               _iter_colors(color, start.shape[:-1])):

        out_flat[i] = Arrow(s, e, l, width_scale, c, opacity, fig)

    return out_flat
示例#4
0
def scatter(points,
            color=None,
            opacity=None,
            radius=1.,
            use_cursors=False,
            fig="gcf",
            label=None):
    """Scatter plot using little spheres or cursor objects.

    :param points: The point(s) to place the marker(s) at.
    :type points: np.array with ``points.shape[-1] == 3``

    :param color: The color of the markers, can be singular or per marker, defaults to white.
    :type color: str, 3-tuple, 4-tuple, np.array with same shape as **points**, optional

    :param opacity: The translucency of the plot, from `0` invisible to `1` solid, defaults to `1`.
    :type opacity: float, np.array, optional

    :param radius: The radius of each marker, defaults to 1.0.
    :type radius: float, np.array, optional

    :param use_cursors: If false use spheres, if true use cursors, defaults to False.
    :type use_cursors: bool, optional

    :param fig: The figure to plot into, can be None, defaults to :meth:`vtkplotlib.gcf`.
    :type fig: :class:`vtkplotlib.figure`, :class:`vtkplotlib.QtFigure`, optional

    :param label: Give the plot a label to use in legends, defaults to None.
    :type label: str, optional

    :return: The marker or an array of markers.
    :rtype: :class:`vtkplotlib.plots.Scatter.Sphere` or :class:`vtkplotlib.plots.Scatter.Cursor` or ``np.ndarray`` or spheres or cursors.


    Coloring by directly with scalars is not supported for scatter but you can
    do it using:

    .. code-block:: python

        from matplotlib.cm import get_cmap
        vpl.scatter(points, color=get_cmap("rainbow")(scalars))

    """

    points = np.asarray(points)
    out = np.empty(points.shape[:-1], object)
    out_flat = out.ravel()
    for (i, (xyz, c, r, l)) in enumerate(
            zip(_iter_points(points), _iter_colors(color, points.shape[:-1]),
                _iter_scalar(radius, points.shape[:-1]),
                _iter_scalar(label, points.shape[:-1]))):

        if use_cursors:
            cls = Cursor
        else:
            cls = Sphere

        out_flat[i] = cls(xyz, c, opacity, r, fig, l)

    if out.ndim:
        return out
    else:
        return out_flat[0]
示例#5
0
def arrow(start,
          end,
          length=None,
          width_scale=1.,
          color=None,
          opacity=None,
          fig="gcf",
          label=None):
    """Draw (an) arrow(s) from **start** to **end**.

    :param start: The starting point(s) of the arrow(s).
    :type start: np.ndarray

    :param end: The end point(s) of the arrow(s).
    :type end:  np.ndarray

    :param length: The length of the arrow(s), defaults to None.
    :type length: number, np.ndarray, optional

    :param width_scale: How fat to make each arrow, is relative to its length, defaults to 1.0.
    :type width_scale: number, np.ndarray, optional

    :param color: The color of each arrow, defaults to white.
    :type color: str, 3-tuple, 4-tuple, np.ndarray of shape (n, 3)

    :param opacity: The translucency of the plot, from `0` invisible to `1` solid, defaults to `1`.
    :type opacity: float

    :param fig: The figure to plot into, can be None, defaults to :meth:`vtkplotlib.gcf`.
    :type fig: :class:`vtkplotlib.figure`, :class:`vtkplotlib.QtFigure`, optional

    :param label: Give the plot a label to use in legends, defaults to None.
    :type label: str, optional

    :return: arrow or array of arrows
    :rtype: vtkplotlib.plots.Arrow.Arrow, np.array of Arrows


    The shapes of **start** and **end** should match. Arrow lengths are
    automatically calculated via pythagoras if not provided but can be
    overwritten by setting **length**. In this case the arrow(s) will always
    start at **start** but may not end at **end**. **length** can either be a single
    value for all arrows or an array of lengths to match the number of arrows.

    .. note::

        Arrays are supported only for convenience and just use a python for
        loop. There is no speed bonus to using numpy or trying to plot in bulk
        here.

    .. seealso:: :meth:`vtkplotlib.quiver` for field plots.

    """

    start = np.asarray(start)
    end = np.asarray(end)

    assert start.shape == end.shape

    shape = start.shape[:-1]
    out = np.empty(shape, object)
    out_flat = out.ravel()

    for (i, s, e, l, c, lab) in zip(range(out.size), _iter_points(start),
                                    _iter_points(end),
                                    _iter_scalar(length, shape),
                                    _iter_colors(color, shape),
                                    _iter_scalar(label, shape)):

        out_flat[i] = Arrow(s, e, l, width_scale, c, opacity, fig, lab)

    if out.ndim == 0:
        return out.item()
    return out