2
$\begingroup$

I have a 3D data that I want to visualise its each slice with ArrayPlot. However, there is a problem the way it represents the data. As,

ArrayPlot by default generates grayscale output, in which zero values are shown white, and the maximum positive or negative value is shown black.

It conflicts with the general idea, cause it should represent 0 values as black, max value(if it is scaled, equals to 1) as white and the rest of the values as gray levels.

I am playing with it and I wrote a code sample:

ArrayPlot[data, ColorFunctionScaling -> True, ColorRules -> {1 -> White, 0 -> Black}]

I am not sure whether it is correct. Actually, I am not satisfied with the result.

Any help is appreciated.Thanks.

$\endgroup$
0

1 Answer 1

4
$\begingroup$

You have to set ColorFunction. Please compare the resulting graphics

data = Table[x, {200}, {x, 0, 1, 1/199.}];
ArrayPlot[data]
ArrayPlot[data, ColorFunction -> GrayLevel]

A word to your comment

Using strings as option value to ColorFunction lets you easily select one of the color gradients provided by Mathematica. In all other cases ColorFunction should really be a function, meaning that it takes a numeric value and results in a color like RGBColor, GrayLevel, Hue, CMYKColor or named colors like Red or even Opacity combinations.$^1$

Therefore, while "GrayTones" is color gradient, GrayLevel is a function. I hope this clears the confusion. To add some more confusion, there is a "Grayscale" too which is used in Image or Rasterize to set the ColorSpace. Just in case you stumble over it and wonder what's this all about.

$^1$ Actually, you can also return numeric values which are interpreted as colors:

  • a triple {r,g,b} is interpreted as a rgb-color
  • a number is interpreted as a gray level
  • a tuple {g,alpha} is interpreted as gray level with alpha channel
  • a list {c,m,y,k} is interpreted as CMYK-color
$\endgroup$
3
  • $\begingroup$ Thanks. I had tried this command with quotation marks which didn't make difference(i.e."GrayLevel") :( . By the way, I have just noticed that assigning ColorFunction to "GrayTones" gives the same result. $\endgroup$
    – cesm
    Jul 28, 2013 at 7:22
  • 1
    $\begingroup$ @cesm I cleared the confusion why "GrayLevel" is not working by adding some information to my answer. $\endgroup$
    – halirutan
    Jul 28, 2013 at 22:28
  • $\begingroup$ Thank you for the clearence. Here comes another confusion. I have also used Image for visualizing. However, although I assigned ColorSpace to Graylevel(or Grayscale), I got a binary image(evidenced by the histogram). After, I used ImageAdjust, I got gray-scale images as I got with ArrayPlot. Here is the code that I wrote Map[ImageAdjust[#] &, Image[data[[;; , ;; , #]], ColorSpace -> "Graylevel"] & /@ Range[1, Dimensions[data][[3]]], {1}]. $\endgroup$
    – cesm
    Jul 29, 2013 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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