ColorFunction Example Mathematica
ColorFunction Example Mathematica
RGBColor
Here is a brief overview of the use of “colors” in plots and more generally the use of ColorData. At the
simplest level we can specify a color for a plot. At the simplest level is to use a RGBColor function
where the specified colors {red, green, blue) take on values between 0 and 1. How is an example using
Plot and specifying the plot will be displayed in Blue.
In[3]:= Plot[Sin[3 x], {x, 0, 2 π}, PlotStyle → RGBColor[0, 0, 1]]
1.0
0.5
Out[3]=
1 2 3 4 5 6
-−0.5
-−1.0
Instead of specifying the function RGBColor you could simply use the function Blue
2 ColorFunctionExample.nb
1.0
0.5
Out[5]=
1 2 3 4 5 6
-−0.5
-−1.0
CMYKColor
Instead of specifying RGBColor you can specify CMYKColor[cyan, magenta,yellow,black] or simply use
the shortcuts, Cyan, Magenta, Yellow, etc
In[6]:= Plot[Sin[3 x], {x, 0, 2 π}, PlotStyle → Magenta]
1.0
0.5
Out[6]=
1 2 3 4 5 6
-−0.5
-−1.0
1.0
0.5
Out[8]=
1 2 3 4 5 6
-−0.5
-−1.0
ColorFunctionExample.nb 3
ColorData
Here is a simple example of
In[1]:= ? ColorData
Here is a simple example that shows the use of ColorData using the scheme called “TemperatureMap”.
First let us see what ColorData produces when you specify a color scheme
In[9]:= ColorData["TemperatureMap"]
Name: TemperatureMap
Out[9]= ColorDataFunction
Gradient:
You get what is called a ColorDataFunction. If you specify a parameter value between 0 and 1 you get a
color from this ColorData function
In[11]:= ColorData["TemperatureMap"][0.8]
Out[11]=
Out[12]=
In this case there is no “ColorList” property for the color scheme “TemperatureMap”
Here is what the property “Image” yields for our color scheme
In[16]:= ColorData["TemperatureMap", "Image"]
Out[16]=
Here is what the property “Image” yields for the color scheme “VisibleSpectrum”:
4 ColorFunctionExample.nb
Out[17]=
The property Range gives an idea of what type of parameter you need to pass to the selected color
scheme. For example:
In[19]:= {ColorData["TemperatureMap", "Range"], ColorData["VisibleSpectrum", "Range"]}
Out[19]= {{0, 1}, {380, 750}}
Shown below are the various color schemes one can use with ColorData
In[47]:= ColorData[]
Out[47]= {Gradients, Indexed, Named, Physical}
In[48]:= ColorData["Named"]
Out[48]= {Atoms, Crayola, GeologicAges, HTML, Legacy, WebSafe}
Index: 46 Colors: 15
Out[59]= ColorDataFunction
Palette:
We see we have 15 colors in this indexed scheme, We can display the colors in this scheme using
ArrayPlot:
ColorFunctionExample.nb 5
We see we have 15 colors in this indexed scheme, We can display the colors in this scheme using
ArrayPlot:
In[60]:= ArrayPlot[{Range[40]}, ColorFunction → ColorData[46],
ColorFunctionScaling → False, AspectRatio → .3]
Out[60]=
Name: TemperatureMap
Out[58]= ColorDataFunction
Gradient:
By specifying a value as the second argument we get a color from the specified gradient scheme ( in
this case “TemperatureMap”
In[52]:= ColorData["TemperatureMap"][0.1]
Out[52]=
Here are the range of colors for values between 0 and 1 in steps of 0.1
Table[ColorData["TemperatureMap"][i], {i, 0, 1, 0.01}]
Out[45]= , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , ,
A nicer way to display the colors is using ArrayPlot ( here with intervals of 0.1 and not 0.01)
In[65]:= ArrayPlot[{Range[0, 1, 0.1]},
ColorFunction → ColorData["TemperatureMap"], AspectRatio → .3]
Out[65]=
If you specify a value outside the range you get the color associated with the lower or upper bound
values. For example
In[54]:= ColorData["TemperatureMap"][-− 4] == ColorData["TemperatureMap"][0]
Out[54]= True
6 ColorFunctionExample.nb
Out[20]=
and here is the “range” for the parameter that can be passed to this color scheme
In[21]:= ColorData["NeonColors", "Range"]
Out[21]= {0, 1}
Out[15]= , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , ,
If we inspect the Help on the use of ColorFunction, we see that for the function Plot we can either pass
values related to “x” or “y” values of the plot. The most general syntax for using color function is
1.0
0.8
0.6
Out[24]= 0.4
0.2
2 4 6 8 10
-−0.2
The first thing to note is that the range of y values for the plot are approximately -0.2<y<1, while the
range for the x values is 0≤x≤10. But the range for the parameter that can be passed to NeonColors
scheme is {0,1}. This is handled conveniently in Mathematica by using the function
In[25]:= ? ColorFunctionScaling
For many Plot functions the default value is “True”. Let us apply our color scheme “NeonColors” to the
sync plot using ColorFunction. Here is the code
In[26]:= Plot[Sinc[x], {x, 0, 10}, PlotStyle → Thick,
ColorFunction → Function[{x, y}, ColorData["NeonColors"][y]], PlotRange → All]
Out[26]=
Out[27]=
In[32]:= ? Function
Function[body] or body & is a pure function. The formal parameters are # (or #1), #2, etc.
Function[x, body] is a pure function with a single formal parameter x.
Function[{x1 , x2 , …}, body] is a pure function with a list of formal parameters. %
Out[28]=
Because we have set ColorFunctionScaling→False, all negative values of y are colored with
In[31]:= ? Function
Function[body] or body & is a pure function. The formal parameters are # (or #1), #2, etc.
Function[x, body] is a pure function with a single formal parameter x.
Function[{x1 , x2 , …}, body] is a pure function with a list of formal parameters. %
In[29]:= ColorData["NeonColors"][0]
Out[29]=
We can also use the short-hand notation for specifying a pure function, which is some times more
convenient. Using this format we have to specify which “slot value” must be passed to the pure function :
#1 denotes x values
#2 denotes y values
Here is the code using a pure function. Note the use of the parentheses and the “&”.
ColorFunctionExample.nb 9
Out[30]=
Out[33]=
Out[15]= , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , ,
Out[12]=
Here is the same plot but now setting ColorFunctionScaling→False, and using the long version for
specifying a pure function:
In[13]:= ContourPlot[2 Sin[2 x] Cos[3 y], {x, -− 2, 2}, {y, -− 2, 2},
ColorFunction → Function[{z}, ColorData["NeonColors"][z]],
ColorFunctionScaling → False]
Out[13]=
ColorFunctionExample.nb 11
Out[32]=
Name: TemperatureMap
Out[58]= ColorDataFunction
Gradient:
By specifying a value as the second argument we get a color from the specified gradient scheme ( in
this case “TemperatureMap”
In[52]:= ColorData["TemperatureMap"][0.1]
Out[52]=
Here are the range of colors for values between 0 and 1 in steps of 0.1
Table[ColorData["TemperatureMap"][i], {i, 0, 1, 0.01}]
Out[45]= , , , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , , ,
, , , , , , , , , , , , , , , , , , , , , , , ,
A nicer way to display the colors is using ArrayPlot ( here with intervals of 0.1 and not 0.01)
12 ColorFunctionExample.nb
Out[65]=
If you specify a value outside the range you get the color associated with the lower or upper bound
values. For example
In[54]:= ColorData["TemperatureMap"][-− 4] == ColorData["TemperatureMap"][0]
Out[54]= True
Here is the same plot but using the TemperatureMap color scheme instead of a Hue color scheme and
as before we pass the z value to the color scheme. In this case we use the short-hand version for
creating the Pure function ( see the previous section using “NeonColors” scheme:
In[36]:= Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction → (ColorData["TemperatureMap"][#3] &)]
Out[36]=
Here is the same plot but using the TemperatureMap color scheme but passing the x value to the color
scheme. Note ColorFunctionScaling is by default True.
ColorFunctionExample.nb 13
In[37]:= Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction → (ColorData["TemperatureMap"][#1] &)]
Out[37]=
When we set ColorFunctionScaling->False and pass the x-values to the color scheme. For all values of
x>1 we have a Red plot
In[41]:= Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3},
ColorFunction → (ColorData["TemperatureMap"][#1] &), ColorFunctionScaling → False]
Out[41]=
Name: Rainbow
Out[66]= ColorDataFunction
Gradient:
14 ColorFunctionExample.nb
Out[77]=
Note in this case we are passing the z value to the ColorFunction, which ranges from Exp[1]
In[72]:= {Exp[1.], Exp[1 -− 8.]}
Out[72]= {2.71828, 0.000911882}
By default, the option ColorFunctionScaling→True is used so that the z values passed to the ColorFunc-
tion are scaled from 0 to 1. Thus we get the full range of colors in the plot with no repeats.
Let us now pass the x value (slot #1) to the pure function. As the plot below shows now the x values are
colored with the specified ColorData. Again ColorFunctionScaling→True is implemented. That is to say
the x- values passed to ColorData are scaled from 0 to 1 so that there are no repeat values of the colors
in the plot.
Out[78]=
If we specify ColorFunctionScaling→False, then the ColorData is applied for x values between 0 and 1,
but for x values outside this range the upper ( 1<x<2) and lower (-2<x<0) bound colors from the Color-
Data Function are used.
ColorFunctionExample.nb 15
If we specify ColorFunctionScaling→False, then the ColorData is applied for x values between 0 and 1,
but for x values outside this range the upper ( 1<x<2) and lower (-2<x<0) bound colors from the Color-
Data Function are used.
Out[54]=
Note by increasing the number of PlotPoints, one gets smother degradations of the ColorData
Out[99]= , , , , , , , , , , , , , , , , , , , ,
In the example below we pass the x value to the Colordata[54]. For all x values between 0 and 2 we
scale the values so that we get 20 values. Here is the result. Note it is important to increase the number
of PlotPoints so that we get uniform strips for a given x value,
16 ColorFunctionExample.nb
Out[107]=
Blend[{col1 , col2 }, x] gives a color obtained by blending a fraction 1 -− x of color col1 and x of color col2 .
Blend[{col1 , col2 , col3 , …}, x] linearly interpolates between colors coli as x varies from 0 to 1.
Blend[{{x1 , col1 }, {x2 , col2 }, …}, x] interpolates to give coli when x = xi .
Blend[{col1 , col2 , …}, {u1 , u2 , …}] blends all the coli , using fraction ui of color coli .
Blend[{image1 , image2 , …}, …] blends pixel values of 2D or 3D images imagei . %
Here is an example that blends Blue and White for coloring a disk
In[114]:= Graphics[Table[{Blend[{White, Blue}, x], Disk[{10 x, 0}]}, {x, 0, 1, 1 /∕ 10}]]
Out[114]=
In this next plot we use ArrayPlot and use Blend for our ColorFunction. In this case the color function is
applied to each cell of the array. The value of each cell is sent to the ColorFunction. In the example
below. The cell values may be greater than 1, so the default ColorFunctionScaling->True is used.
ColorFunctionExample.nb 17
Out[115]=
The ColorData function in this case can take 6 arguments, {x,y,z,r,θ𝜃,ϕ𝜑}. We will also use the Rainbow
color scheme
In[3]:= ArrayPlot[{Range[0, 1, 0.1]}, ColorFunction → ColorData["Rainbow"], AspectRatio → .3]
Out[3]=
Out[2]=
Here is a overview that shows the application of the ColorFunction to all 6 variables
In[10]:= Row[{SphericalPlot3D[1 + Sin[8 ϕ] Cos[8 θ] /∕ 5, {θ, 0, π}, {ϕ, 0, 2 π},
ColorFunction → (ColorData["Rainbow"][#6] &), Mesh → None, PlotPoints → 25,
Boxed → False, Axes → False], SphericalPlot3D[1 + Sin[8 ϕ] Cos[8 θ] /∕ 5,
{θ, 0, π}, {ϕ, 0, 2 π}, ColorFunction → (ColorData["Rainbow"][#5] &),
Mesh → None, PlotPoints → 25, Boxed → False, Axes → False],
SphericalPlot3D[1 + Sin[5 ϕ] Sin[10 θ] /∕ 10, {θ, 0, π}, {ϕ, 0, 2 π},
ColorFunction → (ColorData["Rainbow"][#4] &), Mesh → None,
PlotPoints → 25, Boxed → False, Axes → False],
SphericalPlot3D[1 + Sin[5 ϕ] Sin[10 θ] /∕ 10, {θ, 0, π}, {ϕ, 0, 2 π},
ColorFunction → (ColorData["Rainbow"][#3] &), Mesh → None,
PlotPoints → 25, Boxed → False, Axes → False],
SphericalPlot3D[1 + Sin[5 ϕ] Sin[10 θ] /∕ 10, {θ, 0, π}, {ϕ, 0, 2 π},
ColorFunction → (ColorData["Rainbow"][#2] &), Mesh → None,
PlotPoints → 25, Boxed → False, Axes → False],
SphericalPlot3D[1 + Sin[5 ϕ] Sin[10 θ] /∕ 10, {θ, 0, π}, {ϕ, 0, 2 π},
ColorFunction → (ColorData["Rainbow"][#1] &), Mesh → None,
PlotPoints → 25, Boxed → False, Axes → False]}]
Out[10]=
ColorFunctionExample.nb 19
Other Examples
In this example we use the color scheme Hue to color the surface of a sphere based on the value of the
variable z
In[1]:= SphericalPlot3D[1, {θ, 0, Pi}, {ϕ, 0, 2 Pi},
ColorFunction → Function[{x, y, z, θ, ϕ, r}, Hue[z]], Axes → None, Boxed → False]
Out[1]=
Out[2]=
We then use the TextureCoordinateFunction to apply the image to the surface of the sphere
In[3]:= ? TextureCoordinateFunction
If you apply it to SphericalPlot3D it can take up to 6 coordinates. In the example below, we apply it to ϕ𝜑
(#5) and θ𝜃(#4) coordinates
ColorFunctionExample.nb 21
Out[4]=