This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

LineColorFunction, FillColorFunction

Functional line coloring

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Value Summary

FillColorFunction, LineColorFunctionOptionalColor function (see below)


These options accept functions that define the color of a plot at arbitrary points.

Using FillColorType and LineColorType, the user can control the color of many graphical objects. The setting providing the most detailed (and most complicated) control is Functional. In this case, a color function must be provided using one of LineColorFunction, FillColorFunction.

A color function can be a list of three or four expressions.

If three expressions are given, they specify RGB colors. If four expressions are given, they specify RGBA colors. See the introduction for more details on color specifications.

The expressions may contain the identifiers bound in the corresponding object. For example, in a plot::Function2d(sin(x), x=0..PI), the color function may refer to x. More formally, the expressions may contain the identifiers found in the attributes XName, YName, ZName, UName, VName, and ParameterName of the plot object they are found in.

All of these expressions must, for values in the given ranges, evaluate to real numbers in the range . Real values outside this range do not yield errors, they are simply clipped.

See also Example 1.

Alternatively, a color function can be a procedure or function environment.

A procedure (or a function environment) used as a color function must return lists of three or four real numbers in the range . Real values outside this range are clipped. (If this function ever returns a list of four numbers, it must always do so.) A list of three numbers is interpreted as an RGB color, while a list of four values is interpreted as an RGBA color. See the introduction for more details on color specifications.

The number and meaning of arguments a color function is called with depends on the object type. Informally, we have:

Type (abbreviated)Parameters
Conformal(f(z)) z, Re(f(z)), Im(f(z)), flag (with flag = 1 or flag = 2)
Curve2d(x(u),y(u)) u, x(u), y(u)
Curve3d(x(u),y(u),z(u)) u, x(u), y(u), z(u)
Cylindrical(r(u,v),phi(u,v),z(u,v))u, v, r(u,v), phi(u,v), z(u,v), x(u), y(u), z(u)
Density(f(x,y))x, y, f(x,y)
DodecahedronSee below.
Function2d(f(x))x, f(x)
Function3d(f(x,y))x, y, f(x,y)
HexahedronSee below.
IcosahedronSee below.
Implicit2d(f(x,y), Contours=[c])x, y, D([1],f)(x,y), D([2],f)(x,y), c
Implicit3d(f(x,y,z), Contours=[c])x, y, z, D([1],f)(x,y,z), D([2],f)(x,y,z), D([3],f)(x,y,z), c
Matrixplotx, y, z
OctahedronSee below.
Polar([r(t),phi(t)])t, r(t), phi(t), x(t), y(t)
Polygon2d([..,[xi,yi],..])xi, yi, i


xi, yi, zi, i

Rootlocus(p(z, u))

u, Re(z), Im(z)
Spherical(r(u,v),phi(u,v),thet(u,v))u, v, r(u,v), phi(u,v), thet(u,v), x, y, z
Streamlines2d(v(x,y), w(x, y))x, y, v(x,y), w(x,y), t, l, n
Surface(x(u,v),y(u,v),z(u,v))u, v, x(u,v), y(u,v), z(u,v)
SurfaceSTLSee below.
SurfaceSetSee below.
TetrahedronSee below.
TubeSee below.
VectorField2d(v(x,y),w(x,y))x, y, v(x,y), w(x,y)
XRotate(f(x))x, phi, x, y(x,phi), z(x,phi)
ZRotate(f(t))t, phi, x(t,phi), y(t,phi), f(t)(=z(t,phi))

Additionally, for animated objects, the current value of the animation parameter is provided.

Dodecahedron, Hexahedron, Icosahedron, SurfaceSTL, SurfaceSet, and Tetrahedron are built from triangles; the color functions are called once for each vertex of these triangles and are passed the number of the triangle (an integer count starting at 1), the coordinates of the vertex and the animation parameter, if that is used.

For plot::Tube, the color functions are given the coordinates of the currently visited point on the central curve, followed by the coordinates of the point on the surface, followed by the animation parameter, if any. (That makes seven arguments altogether.)

The examples below show different usage environments of color functions for some of the object types listed above.


Example 1

By default, most 3D-objects in MuPAD® get “height coloring”:


To change the direction of this color, you can use FillColorFunction:

xmin := -5:
xmax := 5:
color := zip(RGB::Red, RGB::CornflowerBlue,
             (a, b) -> (x-xmin)/(xmax-xmin)*a

         FillColorFunction = color))

Example 2

Animated color functions can be combined with static objects:

  plot::Curve2d([sin(3*x), sin(4*x + 1)], x = 0..2*PI,
                LineColorFunction = ((u, x, y, a) ->
                             [(u-a)/5, (u-a)/5, 1]),
                a = -5..6)

cf := (i, x, y, z, a) -> [RGB::Red,
                                       mod 3) + 1]:
plot(plot::Icosahedron(FillColorFunction = cf,
                       a = 0..9))

Example 3

A color function should generate values in RGB color space. Since a plot::Box does not allow a FillColorFunction, we use six (trivial) Surface objects to show the outside of this color space:

rgb := (u, v, x, y, z) -> [x, y, z]:
plot(plot::Surface(formula, u = 0..1, v = 0..1,
                   FillColorFunction = rgb)
     $ formula in [[0, u, v], [1, u, v],
                   [u, 0, v], [u, 1, v],
                   [u, v, 0], [u, v, 1]],
     plot::Box(0..1, 0..1, 0..1, Filled = FALSE,
               LineColor = RGB::Black.[0.25]),
     Scaling = Constrained, Axes = None,
     ULinesVisible = FALSE, VLinesVisible = FALSE,
     Lighting = None, CameraDirection = [4, 7, 3])

RGB colors are a very technical way of defining a color. The HSV color space is more popular with designers, since there the “hue” (i.e., the perceived color type) is not a combination of three numbers but rather one of the numbers making up a color:

hsv := (u, v, r, phi, z) -> RGB::fromHSV([180/PI*phi, r, z]):
plot(plot::Cylindrical([z, phi, z], z = 0..1, phi = 0..2*PI,
                       FillColorFunction = hsv),
     plot::Cylindrical([r, phi, 1], r = 0..1, phi = 0..2*PI,
                       FillColorFunction = hsv),
     plot::Circle3d(1, [0, 0, 1], [0, 0, 1],
                    Color = RGB::Black.[0.25]),
     ZXRatio = 1.5, Scaling = Unconstrained,
     Axes = None, Lighting = None,
     ULinesVisible = FALSE, VLinesVisible = FALSE,
     CameraDirection = [-17, -12, 3])

Example 4

HSV color space is especially suitable for quick coloring of cylindrical,polar, or spherical plots, due to its circular nature:

hsv := (u, v, r, phi,thet) ->
                  3/4+sin(u)/4, 1]):
plot(plot::Spherical([1, u, v], u = 0..2*PI, v = 0..PI,
                     FillColorFunction = hsv))

There are other examples, where the cyclic nature comes in handy, too:

hsv := (x, y, z) -> RGB::fromHSV([150*z, 1, 1]):
plot(plot::Function3d(sin(x*y)*(x-y), x = -3..3, y = -3..3,
                      Submesh = [2, 2], FillColorFunction = hsv))

The following example takes a long time to compute. Reducing the values set for Mesh results in a shorter computation, while higher values lead to an image with finer details:

c := 0.377+0.2*I:
julia := proc(x, y)
           local i, z;
           i := 0;
           z := float(x + I*y);
           while i < 1000 and abs(z) < 4 do
             z := z^2 + c;
             i := i + 1;
Jcol := (x, y, i) -> if i >= 1000 then
                       RGB::fromHSV([i, 1, 3/4+i/2000])
plot(plot::Density(julia, x = 0..0.5, y=0.25..0.75,
                   FillColorFunction = Jcol,
                   Mesh = [100,75]))

Example 5

Another way of getting a smooth color transition is to use a periodic function in between, for example trigonometric ones (note the (1+sin(a))/2: we need values between 0 and 1):

  plot::Polar([r*surd(r, 3), r], r = -4*PI..4*PI,
               AdaptiveMesh = 2,
               LineColorFunction = [(sin(r) + 1)/2,
                                    (cos(r/2) + 1)/2,
               LineWidth = 1*unit::mm)

This also applies for cyclic colors in terms of time:

plot(plot::Function3d(sin(x)+sin(y), x = -5..5, y = -5..5,
          FillColorFunction = [(x+5)/10, (y+5)/10,
          a = 0..2*PI),
     CameraDirection = [-1, -3, 3],
     Scaling = Constrained)


Animation is handled by the general framework, not the individual objects. Therefore, the framework also supplies the animation parameter to the color functions.

See Also

MuPAD Functions

Was this topic helpful?