# MATLAB To Mathematica: How to implement imshow()

I want to implement the MATLAB imshow function in Mathematica.

This is a DensityPlot[] from Mathematica

And this is imshow() image generated by MATLAB

As you can see there's a difference between the two plots. I want to obtain smooth transition in Mathematica as I can in MATLAB.

-
newcolor2 = {RGBColor[0, 0, 0], RGBColor[0.2, 0, 0], RGBColor[0.4, 0, 0], RGBColor[0.6, 0, 0], RGBColor[0.8, 0, 0], RGBColor[1, 0, 0], RGBColor[1, 0.2, 0], RGBColor[1, 0.4, 0], RGBColor[1, 0.6, 0], RGBColor[1, 0.8, 0], RGBColor[1, 1, 0], RGBColor[1, 1, 0.2], RGBColor[1, 1, 0.4], RGBColor[1, 1, 0.6], RGBColor[1, 1, 0.8], RGBColor[1, 1, 1]};DensityPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> newcolor2, PlotLegends -> Automatic, PlotPoints -> 300] –  FromBaidu Aug 31 '13 at 13:27
This is the code of pic1 –  FromBaidu Aug 31 '13 at 13:27
The equivalent of imshow is Image, not DensityPlot. DensityPlot calculates a function, Image constructs an image form a matrix. –  Szabolcs Aug 31 '13 at 19:16
If you clarify your question a bit or describe your application, I'll update (or remove) my answer. I'm not sure it answers as it is. –  Szabolcs Aug 31 '13 at 20:01

Blend gives smooth transition between a list of colors:

cf[v_] := Blend[{Black, Red, Yellow, White}, v]
DensityPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> cf, PlotPoints -> 300]


For a clear distinction between some set of contours you can use ContourPlot:

ContourPlot[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2},
ColorFunction -> cf,
ContourStyle -> None]


If you start out with a matrix of data like you would for imshow you can use ArrayPlot:

data = Table[Exp[-x^2 - y^2], {x, -2, 2, 0.02}, {y, -2, 2, 0.02}];
ArrayPlot[data, ColorFunction -> cf, Frame -> False]


-
thanks a lot!^.^ –  FromBaidu Aug 31 '13 at 14:04
+1 Enlightened badge –  Mr.Wizard Sep 1 '13 at 23:39

Alternatively:

newcolor =
RGBColor /@ {{0, 0, 0}, {0.2, 0, 0}, {0.4, 0, 0}, {0.6, 0, 0},
{0.8, 0, 0}, {1, 0, 0}, {1, 0.2, 0}, {1, 0.4, 0},
{1, 0.6, 0}, {1, 0.8, 0}, {1, 1, 0}, {1, 1, 0.2},
{1, 1, 0.4}, {1, 1, 0.6}, {1, 1, 0.8}, {1, 1, 1}};

dat = With[{y = Range[-2, 2, .04]}, Table[Exp[-x^2 - y^2], {x, y}]];
ListDensityPlot[dat, ColorFunction -> (Blend[newcolor, #] &)]

dat = With[{y = Range[-2, 2, .01]}, Table[Exp[-x^2 - y^2], {x, y}]];
Colorize[Image[Rescale@dat], ColorFunction -> (Blend[newcolor, #] &)]


Note:

dat1 = Table[ Exp[-x^2 - y^2], {x, -2, 2, 0.002}, {y, -2, 2, 0.002}]; // Timing
dat2 = With[{y = Range[-2, 2, .002]},  Table[Exp[-x^2 - y^2], {x, y}]]; // Timing
dat1 == dat2
(*
{0.904806, Null}

{0.202801, Null}

True
*)

-
Nice note regarding vectorization. –  Mr.Wizard Sep 1 '13 at 23:43
With is better than Table! WA! –  FromBaidu Sep 2 '13 at 5:21
FWIW, dat3 = Outer[Times, #, #] &@ Exp[-Range[-2, 2, 0.002]^2] is a bit faster (taking even more advantage of the function at hand). –  Michael E2 Sep 2 '13 at 15:03

Your question is a bit confusing to me because imshow and DensityPlot do different things. imshow will take a matrix of values and show it as an image. The Mathematica equivalent is Image. DensityPlot will take a two-argument function and plot it in 2D.

Here's a direct comparison (using MATLink to pass the data to MATLAB, for convenience):

If you need to use an alternative colour scheme, you can map the corresponding function to the data to generate RGB values, and construct the image afterwards:

Image@Map[List @@ ColorData["Rainbow"][#] &, data, {2}]


Update: Colorize[Image[data], ColorFunction -> "Rainbow"] is better (thanks to @chyanog!)

If the input matrix has a smooth gradation of values, so will the image (provided that the colours scheme is smooth---most of them are).

-
Elaine is cuter then Lena. +1 –  Öskå Aug 31 '13 at 19:40
Colorize[img, ColorFunction -> "Rainbow"] would be more faster. –  chyaong Sep 1 '13 at 7:01
Be careful with doing Colorize[Image[data], ColorFunction->...] you lose the normalization step: data = Table[ 100 Exp[-x^2 - y^2], {x, -2, 2, 0.02}, {y, -2, 2, 0.02}]; Colorize[Image[#], ColorFunction -> "Rainbow"] & /@ {data, data/Max[data]} and not even ColorFunctionScaling->True will help –  ssch Sep 2 '13 at 14:34

Here is my way to simulink a Gauss beam:

 H[m_, x_] := (-1)^m*D[Exp[-x^2], {x, m}];

Table[ArrayPlot[Table[Evaluate[H[i, x] H[j, y]],
{x, -3, 3, .005}, {y, -3, 3, .005}]
]
, {i, 1, 3}, {j, 1, 3}] // MatrixForm


-