Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to implement the MATLAB imshow function in Mathematica.

This is a DensityPlot[] from Mathematica

This pic was Densityplot by Mathematica

And this is imshow() image generated by MATLAB

This pic was print by imshow() from 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.

share|improve this question
    
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
1  
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

4 Answers 4

up vote 12 down vote accepted

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]

densityplot

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]

contourplot

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]

arrayplot

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

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):

MATLAB-and-Mathematica

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!)

enter image description here

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).

share|improve this answer
3  
Elaine is cuter then Lena. +1 –  Öskå Aug 31 '13 at 19:40
1  
Colorize[img, ColorFunction -> "Rainbow"] would be more faster. –  chyaong Sep 1 '13 at 7:01
1  
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

Gauss beam Simulink

share|improve this answer

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
*)
share|improve this answer
    
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
1  
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 Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.