Skip to Main Content Skip to Search
Product Documentation

plot::Densitydensity plot

plot::Density(f(x, y), x = `x_{min}`..`x_{max}`, y = `y_{min}`..`y_{max}`) generates a regular 2D mesh of rectangles extending from the lower left corner (`x_{min}`, `y_{min}`) to the upper right corner (`x_{max}`, `y_{max}`). The rectangle with midpoint (x, y) is colored according to a color scheme based on the “density” value f(x, y).

→ Examples

Calls:

plot::Density(f, x = `x_{min}` .. `x_{max}`, y = `y_{min}` .. `y_{max}`, <a = amin .. amax>, Options)

plot::Density(A, <x = `x_{min}` .. `x_{max}`, y = `y_{min}` .. `y_{max}`>, <a = amin .. amax>, Options)

plot::Density(L, <x = `x_{min}` .. `x_{max}`, y = `y_{min}` .. `y_{max}`>, <a = amin .. amax>, Options)

Parameters:

f

the density values: an arithmetical expression in 2 variables x, y and the animation parameter a. Alternatively, a procedure that accepts 2 input parameters x, y or 3 input parameters x, y, a and returns a real density value. 
f is equivalent to the attribute DensityFunction.

x

name of the horizontal variable: an identifier or an indexed identifier
x is equivalent to the attribute XName.

`x_{min}` .. `x_{max}`

the range of the horizontal variable: `x_{min}`, `x_{max}` must be numerical real value or expressions of the animation parameter a
`x_{min}` .. `x_{max}` is equivalent to the attributes XRange, XMin, XMax.

y

name of the vertical variable: an identifier or an indexed identifier
y is equivalent to the attribute YName.

`y_{min}` .. `y_{max}`

the range of the vertical variable: `y_{min}`, `y_{max}` must be numerical real value or expressions of the animation parameter a
`y_{min}` .. `y_{max}` is equivalent to the attributes YRange, YMin, YMax.

A

an array of domain type DOM_ARRAY or a matrix of category Cat::Matrix (e.g., of type matrix or densematrix) providing numerical density values or expressions of the animation parameter a. Rows/columns of the array, respectively matrix, correspond to rows/columns of the graphical array.
A is equivalent to the attribute DensityData.

L

a list of lists of numerical density values or expressions of the animation parameter a. Each sublist of L represents a row of the graphical array. The number of sublists in L yields the value of the attribute XMesh. The (common) length of the sublists yields the value of the attribute YMesh.
L is equivalent to the attribute DensityData.

See Also:

import::readbitmap, plot, plot::copy, plot::Inequality, plot::Raster

Details:

Example 1

We generate a density plot:

p := plot::Density(cos(x^2 + y^2), x = -3..3, y = -2..2,
                   Mesh = [60, 40]):

The plot object is rendered:

plot(p, Axes = Frame):

MuPAD graphics

This turns into a black and white graphics when suitable colors are specified:

plot(plot::Scene2d(p, FillColor = RGB::White,
                      FillColor2 = RGB::Black),
     plot::Scene2d(p, FillColor = RGB::Black,
                      FillColor2 = RGB::White),
     Width = 120*unit::mm, Height = 45*unit::mm,
     Layout = Horizontal, Axes = Frame):

MuPAD graphics

delete p:

Example 2

We demonstrate the use of a user-defined color function:

mycolor := proc(x, y, f)
begin
   if   f >=  2/3 then RGB::Red
   elif f >=  1/3 then RGB::Orange;
   elif f >=   0  then RGB::Yellow;
   elif f >= -1/3 then RGB::BlueLight;
   elif f >= -2/3 then RGB::Blue;
   else RGB::SlateBlueDark;
   end_if;
end_proc:
plot(plot::Density(cos(x^2 + y^2), x = -3..3, y = -2..2,
                   Mesh = [60, 40],
                   FillColorFunction = mycolor),
     Axes = Frame):

MuPAD graphics

delete mycolor:

Example 3

In this example, we demonstrate how plot::Density can be used to plot gray data from an external source. Assume, there is an external PortableGrayMap text file Norton.pgm containing data such as


P2
240 180
255
249 237 228 231 245 218 229 195 ...
     

The first line contains the “magic value” P2 indicating that this is a PGM text file. The second line contains the pixel width and pixel height of the picture. The number 255 in the third line is the scale of the following gray values.

The remaining data consist of integers between 0 (black) and 255 (white), each representing the gray value of a pixel (row by row).

We import the text data via import::readdata:

graydata := import::readdata("Norton.pgm", NonNested):

This is a long list of all data items in the file. We extract the 4 items in the first three lines:

[magicvalue, xmesh, ymesh, maxgray] := graydata[1..4]

[P2, 240, 180, 255]

We delete the header from the pixel data. (If there are comments in the PGM file, they must be deleted, too).

for i from 1 to 4 do
  delete graydata[1];
end_for:

We transform the plain data list to a nested list containing the gray data of the rows as sublists. (The call to level is not really necessary, but it speeds up the conversion considerably on the interactive level.)

L := level([graydata[(i - 1)*xmesh + 1 .. i*xmesh] $ i=1..ymesh], 1):

This list can be passed to plot::Density:

plot(plot::Density(L, FillColor = RGB::White,
                   FillColor2 = RGB::Black),
     Width = 80*unit::mm, Height = 60*unit::mm):

MuPAD graphics

The image is upside down, because the PGM files stores the pixel data row by row in the usual reading order starting with the upper left corner of the image. The MuPAD® routine plot::Density, however, follows the mathematical orientation of the coordinate axes, i.e., the first pixel value is interpreted as the lower left corner of the image. We have to re-order the rows in the graydata list via revert:

plot(plot::Density(revert(L), FillColor = RGB::White,
                   FillColor2= RGB::Black),
     Width = 80*unit::mm, Height = 60*unit::mm):

MuPAD graphics

The routines import::readbitmap and plot::Raster provide an alternative way to import and display the bitmap image. See the helppage of plot::Raster for examples. This, however, takes more memory, because the bitmap data are imported as RGB color values, whereas only density values (gray data) are needed for plot::Density.

delete graydata, magicvalue, xmesh, ymesh, maxgray, i, L:

Example 4

The Mandelbrot set is one of the best-known fractals. It arises when considering the iteration z[n + 1] = z[n]^2 + c, z[0] = 0 in the complex plane. For sufficiently large values abs(c) of the complex parameter c, the sequence z[n] diverges to infinity; it converges for sufficiently small values of abs(c). The boundary of the region of those c values that lead to divergence of z[n] is of particular interest: this border is highly complicated and of a fractal nature.

In particular, it is known that the series z[n] diverges to infinity, whenever one of the iterates satisfies abs(z[n]) > 2. This fact is used by the following procedure f as stopping criterion. The return value provides information, how many iterates z[0], Symbol::hellip, z[n] it takes to escape from the region abs(z) <= 2 of (potential) convergence. These data are to be used to color the complex c plane (i.e., the (x,y) plane) by a density plot:

f := proc(x, y)
local c, z, n;
begin
   c := x + I*y:
   z := 0.0:
   for n from 0 to 100 do
       z := z^2 + c:
       if abs(z) > 2 then
          break;
       end_if;
   end_for:
   if n < 70 then
        n mod 5;
   else n - 70;
   end_if;
end_proc:

Depending on your computer, the following computations may take some time. On a very fast machine, you can increase the following values of xmesh, ymesh. This will use up more computing time but will lead to better graphical results:

xmesh :=  100: ymesh :=  100:

The following region in the x-y plane is to be considered:

xmin[1] := -2.0: xmax[1] := 0.5:
ymin[1] := -1.2: ymax[1] := 1.2:

The region (xmin_1 <= x) <= xmax_1, (ymin_1 <= y) <= ymax_1 is divided into xmesh x ymesh rectangles. Each rectangle is colored by a density plot according to the “escape times” computed by the procedure f. This procedure can be passed directly to plot::Density:

p1 := plot::Density(f, x = xmin[1].. xmax[1],
                    y = ymin[1] .. ymax[1],
                    Mesh = [xmesh, ymesh],
                    FillColor = RGB::Black,
                    FillColor2 = RGB::Red):

In addition, a rectangle is produced that indicates a region that is to be magnified in the following:

xmin[2] := -0.24: xmax[2] := -0.01:
ymin[2] :=  0.63: ymax[2] :=  0.92:
r1 := plot::Rectangle(xmin[2] .. xmax[2], ymin[2] .. ymax[2],
                      LineColor = RGB::White):

plot(p1, r1):

MuPAD graphics

The density values of the blow-up are not computed directly by plot::Density. They are computed separately and stored in an array A:

dx := (xmax[2] - xmin[2])/xmesh:
dy := (ymax[2] - ymin[2])/ymesh:
A := array(1..ymesh, 1..xmesh,
           [[f(xmin[2]+ (j - 1/2)*dx, ymin[2] + (i - 1/2)*dy)
             $ j = 1..xmesh] $ i = 1..ymesh]):
p2 := plot::Density(A, x = xmin[2] .. xmax[2], y = ymin[2] .. ymax[2],
                    FillColor = RGB::Black, FillColor2 = RGB::Red):

In addition, a further rectangle is produced to indicate a region of interest to be blown up lateron:

xmin[3] := -0.045: xmax[3] := -0.015:
ymin[3] :=  0.773: ymax[3] :=  0.815:
r2 := plot::Rectangle(xmin[3] .. xmax[3], ymin[3] .. ymax[3],
                      LineColor = RGB::White):

plot(p2, r2):

MuPAD graphics

The density values of the next blow-up are again computed separately and stored in a nested list L:

dx := (xmax[3] - xmin[3])/xmesh:
dy := (ymax[3] - ymin[3])/ymesh:
L := [[f(xmin[3] + (j - 1/2)*dx, ymin[3] + (i - 1/2)*dy)
      $ j= 1..xmesh] $ i = 1..ymesh]:
p3 := plot::Density(L, x = xmin[3] .. xmax[3], y = ymin[3] .. ymax[3],
                    FillColor = RGB::Black, FillColor2 = RGB::Red):

plot(p3):

MuPAD graphics

The density objects are to be placed in a single graphics. It consists of the Mandelbrot set p1 as computed above and of modifications of the density plots p2 and p3. Redefining the attributes XRange, YRange, we move p2, p3 to places in the x-y plane where they are not overlapped by p1. Note that this does not change the graphical content of p2, p3, because it is given by the data A and L, respectively, which remain unchanged. (If the ranges were changed in p1, another plot call of p1 would call the procedure f at different points of the plane resulting in a different graphics.)

p2::XRange :=  0.60 .. 1.60: p2::YRange :=  0.05 ..  1.15:
p3::XRange :=  0.60 .. 1.60: p3::YRange := -1.15 .. -0.05:

The Mandelbrot set and the two blow-ups are placed in one scene. In addition, some arrows are added to indicate the origin of the blow-ups. Note that it is quite important here that the arrows are passed to the plot command after the density plots. Otherwise, they would be hidden by the density plots: graphical objects are painted in the ordering in which they are passed to plot:

plot(p1, p2, p3,
     plot::Arrow2d([(xmin[2] + xmax[2])/2,
                    (ymin[2] + ymax[2])/2],
                   [(p2::XMin + p2::XMax)/2,
                    (p2::YMin + p2::YMax)/2],
                   LineColor = RGB::Blue),
     plot::Arrow2d([1.50, 0.65],
                   [(p3::XMin + p3::XMax)/2,
                    (p3::YMin + p3::YMax)/2],
                   LineColor = RGB::Blue)
    ):

MuPAD graphics

delete f, xmesh, ymesh, xmin, xmax, ymin, ymax,
       dx, dy, p1, p2, p3, r1, r2, A, L:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS