The Mathematica® Journal

Visualization of Complex Functions

Bernd Thaller

Institute of Mathematics
University of Graz
Heinrichstr. 36
A-8010 Graz

1. Introduction

Functions [Graphics:Images/thaller_gr_1.gif] of two variables with values in the field of complex numbers (e.g., analytic functions) are often considered abstract mathematical objects which are difficult to visualize. Indeed, the graph of such a function would have to be drawn in a four-dimensional space with coordinates[Graphics:Images/thaller_gr_2.gif], which cannot be easily done on a sheet of paper. Therefore, several aids for visualizing complex functions have been developed. For example, the standard package ComplexMap.m by Roman Maeder illustrates how the function transforms and distorts the complex plane. Another method uses colors for the visualization of complex values. The standard package ArgColors.m specifies colors to describe the argument of complex numbers. This color map is also used in Kevin McIsaac's package ComplexPlot3D.m [McIsaac 1994].

Here we propose a refined color map of the complex plane. The color map is continuous and one-to-one from the complex plane onto the surface of the color manifold. In addition to describing the argument of a complex number by the hue of the color, the color map uses the lightness of the color to represent the absolute value. Thus the color map is suitable for colored density plots of complex functions. The package ComplexPlot.m provides commands for producing intuitive pictures of complex-valued functions, including density plots and surface plots. Since the human eye usually cannot recognize the precise color-values, our method is not suited for a quantitative representation. However, our method is useful for giving a quick impression of the most important qualitative properties of the complex function, such as the position and order of poles and zeros. The method is particularly useful for the visualization of quantum mechanical wave functions [Pauschenwein and Thaller 1996].

2. The Color Map

The complex numbers form a two-dimensional manifold which is usually described as a plane with coordinates [Graphics:Images/thaller_gr_3.gif] and [Graphics:Images/thaller_gr_4.gif]. Sometimes cylindrical coordinates[Graphics:Images/thaller_gr_5.gif] and [Graphics:Images/thaller_gr_6.gif] are also used to represent points in the complex plane. For our purpose, the representation of complex numbers as points [Graphics:Images/thaller_gr_7.gif] on a sphere is more suitable. This representation is given by the method of stereographic projection. Consider a sphere of radius [Graphics:Images/thaller_gr_8.gif] centered at the origin of the three-dimensional space. Let us interpret the [Graphics:Images/thaller_gr_9.gif] plane as the complex plane. From a point [Graphics:Images/thaller_gr_10.gif] corresponding to [Graphics:Images/thaller_gr_11.gif], we draw a straight line to the north pole [Graphics:Images/thaller_gr_12.gif] of the sphere. The intersection of the line with the surface of the sphere maps each point of the complex plane to a point on the surface in a one-to-one manner. Using spherical coordinates [Graphics:Images/thaller_gr_13.gif], we find that the azimuthal angle is just the phase [Graphics:Images/thaller_gr_14.gif], while the polar angle [Graphics:Images/thaller_gr_15.gif] depends only on [Graphics:Images/thaller_gr_16.gif] and on [Graphics:Images/thaller_gr_17.gif] as follows:


The origin [Graphics:Images/thaller_gr_19.gif] of the complex plane is mapped to the south pole [Graphics:Images/thaller_gr_20.gif]. The north pole[Graphics:Images/thaller_gr_21.gif] is the image of a complex "number" which has an undefined phase (like the origin) and has an infinite absolute value. Mathematica uses the symbol ComplexInfinity (which is the same as DirectedInfinity[]) to represent this object. The complex numbers with[Graphics:Images/thaller_gr_22.gif] are represented by the equator of the sphere. All complex numbers with absolute values bigger than [Graphics:Images/thaller_gr_23.gif] are mapped to the northern hemisphere, the points with [Graphics:Images/thaller_gr_24.gif] are mapped onto the southern hemisphere.

The manifold of colors that can be represented in a computer is three-dimensional. In the most frequently used RGB system, the colors are described by coordinates [Graphics:Images/thaller_gr_25.gif] which give the intensities of the "primary" colors: red, green, and blue. The graphics directive RGBColor[R,G,B] accepts arguments in the range [0,1]. Thus, in Mathematica the color manifold is represented by the three-dimensional unit cube. The corners of the cube represent the additive primary colors red [Graphics:Images/thaller_gr_26.gif], green [Graphics:Images/thaller_gr_27.gif], blue [Graphics:Images/thaller_gr_28.gif], and the subtractive primary colors yellow [Graphics:Images/thaller_gr_29.gif], cyan [Graphics:Images/thaller_gr_30.gif], magenta [Graphics:Images/thaller_gr_31.gif]. The remaining corners are black [Graphics:Images/thaller_gr_32.gif] and white [Graphics:Images/thaller_gr_33.gif]. All shades of gray are on the main diagonal from black to white.

A color code for complex numbers can be obtained by defining an arbitrary one-to-one mapping from the surface of the sphere into the manifold of colors. To do this in a natural way, it is useful to have a short look at some less familiar coordinate systems for colors. (For more information on color systems, see [Smith and Blachman 1995].)

The distance of a color [Graphics:Images/thaller_gr_34.gif] from the origin measured in the maximum metric is called the brightness, [Graphics:Images/thaller_gr_35.gif]. The distance from a gray point with the same brightness is called saturation, [Graphics:Images/thaller_gr_36.gif]. Note that the possible values of the brightness [Graphics:Images/thaller_gr_37.gif] are between 0 and 1, and for each value of [Graphics:Images/thaller_gr_38.gif] the saturation varies between 0 (gray) and [Graphics:Images/thaller_gr_39.gif]. The set of all colors with constant saturation [Graphics:Images/thaller_gr_40.gif] and brightness [Graphics:Images/thaller_gr_41.gif] is hence a closed polygonal curve [Graphics:Images/thaller_gr_42.gif] with length [Graphics:Images/thaller_gr_43.gif], which is formed by edges of a cube, each of length [Graphics:Images/thaller_gr_44.gif].


Figure 1.  The RGB color cube.

The hue [Graphics:Images/thaller_gr_46.gif] of a color [Graphics:Images/thaller_gr_47.gif] is [Graphics:Images/thaller_gr_48.gif], where [Graphics:Images/thaller_gr_49.gif] is the length of the part of [Graphics:Images/thaller_gr_50.gif] between [Graphics:Images/thaller_gr_51.gif] and the red corner of [Graphics:Images/thaller_gr_52.gif](the corner with maximal red component) in the positive direction (counter-clockwise, if viewed from the white corner). That means [Graphics:Images/thaller_gr_53.gif] and [Graphics:Images/thaller_gr_54.gif] both correspond to the red corner, and it is most natural to define the hue as a cyclic variable modulo 1. Hence the colors at the corners of [Graphics:Images/thaller_gr_55.gif] (which are red, yellow, green, cyan, blue, and magenta with saturation [Graphics:Images/thaller_gr_56.gif] and brightness [Graphics:Images/thaller_gr_57.gif]) have the hue values [Graphics:Images/thaller_gr_58.gif]. The built-in color directive Hue[h,s[Graphics:Images/thaller_gr_59.gif],b] defines a color in terms of its hue [Graphics:Images/thaller_gr_60.gif], the (scaled) saturation [Graphics:Images/thaller_gr_61.gif], and brightness [Graphics:Images/thaller_gr_62.gif] (the HSB color system). The saturation [Graphics:Images/thaller_gr_63.gif] is scaled so that its values at a given brightness [Graphics:Images/thaller_gr_64.gif] range between 0 and 1. In terms of the unscaled coordinates [Graphics:Images/thaller_gr_65.gif], the color manifold is most naturally represented as a cone.


Figure 2.  The HSB color cone.

It is sometimes considered a drawback of the HSB system that the "pure" red, yellow, green, and so on (the corners of the RGB cube) by definition have the same brightness as white ([Graphics:Images/thaller_gr_67.gif]). The definition of the lightness is designed to give a quantity that describes the human perception more accurately.

For any color [Graphics:Images/thaller_gr_68.gif] the lightness [Graphics:Images/thaller_gr_69.gif] is defined as the average of the maximal and the minimal component, [Graphics:Images/thaller_gr_70.gif] = [Graphics:Images/thaller_gr_71.gif] = [Graphics:Images/thaller_gr_72.gif]. We have [Graphics:Images/thaller_gr_73.gif]. At a given lightness [Graphics:Images/thaller_gr_74.gif], the brightness [Graphics:Images/thaller_gr_75.gif] ranges in [Graphics:Images/thaller_gr_76.gif]. Hence lightness [Graphics:Images/thaller_gr_77.gif] denotes black, and [Graphics:Images/thaller_gr_78.gif] (that is, [Graphics:Images/thaller_gr_79.gif], [Graphics:Images/thaller_gr_80.gif]) is white. The maximal saturation [Graphics:Images/thaller_gr_81.gif] at a given lightness [Graphics:Images/thaller_gr_82.gif] is [Graphics:Images/thaller_gr_83.gif] for [Graphics:Images/thaller_gr_84.gif] and [Graphics:Images/thaller_gr_85.gif] for [Graphics:Images/thaller_gr_86.gif].

The set of color points which have the maximal saturation with respect to their lightness is just the surface of the RGB color cube. On the surface, the brightness is also maximal, so that[Graphics:Images/thaller_gr_87.gif] for [Graphics:Images/thaller_gr_88.gif] and [Graphics:Images/thaller_gr_89.gif]for [Graphics:Images/thaller_gr_90.gif]. Thus the pure colors have lightness 1/2 and maximal saturation. The standard package Color.m defines the color directive HLSColor[h,l,s[Graphics:Images/thaller_gr_91.gif]] which describes a color in terms of hue [Graphics:Images/thaller_gr_92.gif], lightness [Graphics:Images/thaller_gr_93.gif] and saturation [Graphics:Images/thaller_gr_94.gif], where this time the saturation has been scaled so that its values at a given lightness range between 0 and 1. Hence the colors at the surface of the color manifold are described by HLSColor[h,l,1] or equivalently by Hue[h,1,2l] for [Graphics:Images/thaller_gr_95.gif] and Hue[h,2-2l,1] for [Graphics:Images/thaller_gr_96.gif].


Figure 3.  The HLS double cone.

In the HLS system, the colors on the surface of the color manifold have maximal saturation and can therefore be distinguished most easily. It is therefore most natural to color the sphere  by defining a continuous mapping from the points [Graphics:Images/thaller_gr_98.gif] of the sphere onto the surface of the color manifold. This can be done most easily in the HLS color system.

Our color map associates the point [Graphics:Images/thaller_gr_99.gif] on the sphere to a color given by [Graphics:Images/thaller_gr_100.gif]. We have chosen a linear dependence of the lightness on the polar angle [Graphics:Images/thaller_gr_101.gif] such that the south pole [Graphics:Images/thaller_gr_102.gif] has lightness 0 (black), and the north pole [Graphics:Images/thaller_gr_103.gif] appears with lightness 1 (white). The colors on the sphere have maximal saturation 1 in the HLS system and thus have maximal contrast. The cyclic variable hue is most naturally associated to the azimuthal angle [Graphics:Images/thaller_gr_104.gif], which corresponds to the argument of the complex number [Graphics:Images/thaller_gr_105.gif]. The pure colors red, yellow, green, and so on are on the equator of the sphere. They correspond to complex numbers with [Graphics:Images/thaller_gr_106.gif]. The points more to the north appear lighter, and the points on the southern hemisphere are darker. The following sequence of commands produces a visualization of the color map of the sphere. The color map of the complex plane will be shown in Section 4.








Finally, our color map for complex numbers (given in cylindrical coordinates [Graphics:Images/thaller_gr_114.gif] and [Graphics:Images/thaller_gr_115.gif]) can be defined as



3. Surface Plots and Density Plots

The color map defined in the previous section makes it easy to produce surface plots of complex-valued functions.





Here the height of the surface shows the absolute value [Graphics:Images/thaller_gr_122.gif] of a complex number [Graphics:Images/thaller_gr_123.gif], while the color is defined according to ComplexToColorMap.

Finer details or rapid oscillations of complex functions would be better represented in a sort of density plot. Unfortunately, the command DensityPlot can use only color functions which depend on a single real variable (the value of the real function to be plotted). Therefore, ComplexToColorMap cannot be used with the option ColorFunction. However, we can use the graphics primitive RasterArray to define a two-dimensional array of rectangles which are colored according to our convention.

The following command plots a "density graphic" of an array of complex numbers:


For this array of complex values,


the plot clearly shows the zeroes (black) and poles (white) of the complex function Tan[z].




The package ComplexPlot.m is based on the ideas and definitions described above.

4. The Package ComplexPlot.m

The main goal in designing the package has been to define a command ComplexDensityPlot that behaves like the built-in DensityPlot. Moreover, the package contains commands for colored surface plots and contour plots of complex functions. If the package and its documentation are placed in a subdirectory Graphics of the AddOns/Applications directory, it can be loaded with the command:


(We don't have to restart the kernel, because the package prevents shadowing [Wagner 1996]. Note, however, that the previously defined commands ComplexPlot3D and ListComplexDensityPlot will be overwritten.)

To make the documentation accessible through the Help Browser, create the directory AddOns/Applications/Graphics/Documentation/English and put the files BrowserCategories.m, BrowserIndex.nb, ComplexPlot.nb, and CPcmds.nb in this directory. After executing the command Rebuild Help Index in the Mathematica front end, the new help topics should be available.

The command ComplexDensityPlot [f [x,y],{x, xmin, xmax},{y,ymin,ymax}] implements an analogue of DensityPlot for complex-valued functions. It generates a two-dimensional raster of colors which are determined from the complex values of f according to the color map discussed in the previous section. For example, the following command gives a colored density plot of the identity function, which illustrates our color map of the complex plane.




Note that red represents positive real. The complex numbers on the unit circle have maximal brightness and saturation (lightness 1/2). For [Graphics:Images/thaller_gr_134.gif] on the unit circle, [Graphics:Images/thaller_gr_135.gif] has the complementary color. A complex zero appears black. The warning is printed because the argument (and hence the hue) of zero is not defined. Other exceptional values producing error messages are ComplexInfinity and Indeterminate. You can turn off these messages using the command


DensityPlots are well suited for displaying fine details of functions at high resolutions. We can increase the resolution using the option PlotPoints. For plots at high resolution, we should turn off the mesh lines. The following command takes a long time on slow computers.




The bright points indicate the 5 poles of the function [Graphics:Images/thaller_gr_140.gif].  One can see that the poles are of second order because all colors appear twice on a small circle around the pole.

The command ComplexDensityPlot returns a graphics object called ColorDensityGraphics, which can be transformed into other graphics objects, such as  ContourGraphics, DensityGraphics, or SurfaceGraphics.




The package provides the command ComplexPlot3D for three-dimensional surface plots of complex functions. The graphics above can also be generated using the command




Here is another example:




The package also handles singularities. For the following example we turn off the compiler in order to suppress error messages. The Arg::indet warning will still be generated because the arguments of zero, ComplexInfinity, and Indeterminate are not defined. Note that Indeterminate will appear as gray at GrayLevel[0.5].





You can specify your own color map by setting the option ComplexToColorMap.





The default color map is given by the symbol $ComplexToColorMap. There are various options for modifying this built-in color map. The option SphereRadius sets the radius of the sphere used for the stereographic projection. The default value is 1. Setting SphereRadius to [Graphics:Images/thaller_gr_158.gif] causes all complex numbers with [Graphics:Images/thaller_gr_159.gif] to be colored with lightness 1/2 (maximal brightness and saturation). This is useful if we want to investigate complex functions in regions where the absolute value is very small or very large compared to 1.

If we are interested only in a certain range of values, we can set the option ValueRange to a range {vmin,vmax}, which displays complex numbers with absolute values larger than [Graphics:Images/thaller_gr_160.gif] at maximal lightness and complex numbers with [Graphics:Images/thaller_gr_161.gif] at minimal lightness. The values for maximal and minimal lightness are controlled by the LightnessRange option and are set by default to LightnessRange->{0,1}.




It is also possible to set [Graphics:Images/thaller_gr_165.gif]. A plot of [Graphics:Images/thaller_gr_166.gif] with ValueRange set to [Graphics:Images/thaller_gr_167.gif] is the same as a plot of [Graphics:Images/thaller_gr_168.gif] with the default setting of [Graphics:Images/thaller_gr_169.gif].

The ColorDensityGraphics object stores the table of colors obtained by applying the color map to the complex values. Thus, we can quickly redraw the plot with modified Graphics options, such as AspectRatio, MeshStyle, or FrameLabels. (We can't modify the options that control the color map in this way.)




Sometimes, a contour plot displays the absolute value of a function more clearly than a density plot. The package ComplexPlot.m provides a command for combining ContourPlot with ComplexDensityPlot:




Note that the option PlotRange only affects the range of the contours. You have to use the ValueRange option in order to influence the colors. In the following plot, contours are used to represent values where [Graphics:Images/thaller_gr_176.gif], and colors are used for the range [Graphics:Images/thaller_gr_177.gif].




Finally, there is a command ColorArrayPlot for plotting a two-dimensional raster of color directives. This can be used to display color images in Mathematica. The following auxiliary function reads a picture file in noninterleaved "raw format," a stream of bytes describing the color information without a header. Many image-editing programs (such as Adobe Photoshop) are able to save pictures in raw format. Assuming that the color information is stored in non-interleaved raw format without header, the picture can be read with the following command:


Here is an example:





5. An Example: The Zeta Function

We will illustrate the usefulness of the package ComplexPlot.m by investigating graphically the famous Riemann zeta-function Zeta[z], which has a rich and interesting structure [Karatsuba and Voronin 1992]. This function is defined as the analytic continuation to the whole complex plane of the function


which is initially given only for [Graphics:Images/thaller_gr_187.gif]. The zeta function is meromorphic everywhere with a single pole at [Graphics:Images/thaller_gr_188.gif]. The Riemann hypothesis is the conjecture that all zeros with [Graphics:Images/thaller_gr_189.gif] lie on the "critical line" [Graphics:Images/thaller_gr_190.gif] [Aizenberg,]. The only zeros with [Graphics:Images/thaller_gr_191.gif] are simple zeros at the negative even integers. The Riemann zeta function plays a prominent role in number theory, because its properties are related to the distribution of prime numbers.

The function Zeta[z] has absolute values ranging over many orders of magnitude within small regions. Usually, SurfacePlots of such functions are not very useful, so we use a ColorDensityPlot for the visualization. (The following high-resolution plot takes several minutes on a Power Mac 8500. You should decrease the number of plot points on slower computers.)




We can see the pole at [Graphics:Images/thaller_gr_195.gif], and the first of the zeroes along the critical line [Graphics:Images/thaller_gr_196.gif]. The phase information is hidden in the regions that are black or white, where the absolute value of Zeta[z] is very small or large compared to 1.

We can make this information visible by restricting the maximal and minimal lightness of the colors in the graphic. If we set LightnessRange->{0.5,0.5}, the information on the absolute value is totally suppressed. The following plot gives the phase information for the same region as in the plot above.




We can have a closer look at the region of [Graphics:Images/thaller_gr_200.gif] for which Abs[Zeta[z]] is very small by setting SphereRadius to some small value. The following plot     enlarges the black region of the first graphics and displays the values with Abs[Zeta[z]]=0.005 with maximal brightness/saturation:.




This plot shows clearly the zeros (all of first order) of the zeta function in the region under consideration. The zeros are located at the negative even numbers. The zeros at -16, -18, and -20 are already visible in the first plot. In order to look at the behavior of the zeta function near the zero at [Graphics:Images/thaller_gr_204.gif], we have to increase SphereRadius. Otherwise, the following plot would show only a tiny black point in a white field.




This is an even more extreme example:




To display the behavior near the critical line and the precise position of the zeros, a contour plot seems most appropriate.




The colors of the graphic show that the poles and zeros of the Riemann zeta function are of first order. Finally, we show a high-resolution surface plot of the zeta function in the same region.





L. Aizenberg., V. Adamchik, and V. E. Levit, "Approaching the Riemann Hypothesis with Mathematica," The Mathematica Journal, 7 (1) (1997).

A. A. Karatsuba and S. M. Voronin, "The Riemann Zeta-Function," De Gruyter Expositions in Mathematics 5. Walter de Gruyter, 1992.

K. McIsaac, ComplexPlot3D.m [Mathematica application package], 1998. Available:

J. Pauschenwein and B. Thaller, "Visualizing quantum-mechanical wavefunctions in three dimensions with AVS," Computers in Physics, 10 (6) (1996), pp. 558-66.

C. Smith and N. Blachman, The Mathematica Graphics Guidebook, Addison-Wesley, 1995.

D. B. Wagner, "Contexts and shadowing," The Mathematica Journal, 10 (6) (1996), pp. 41-51.

B. Thaller, "Visualization of complex-valued functions," [personal web site], 1997. Available:

Converted by Mathematica      July 1, 1999