# Re: Plotting a 3D Array In IDL

```Hi,

"David Fanning" <david@dfanning.com> writes:
> K. Banerjee writes:
>
> > Say I have a 3D array whose elements are only 1 or 0. I'd like to plot
> > this array, say a white dot for the 1's and a red dot for the 0's. Is
there
> > a way I can do this in IDL?
> >
> > (I'm fairly new to IDL and thre's probably a straight forward
> > way to do this!)
>
> There are ways to do this in IDL alright. But
> none (I don't think) that can be explained
> adequately in a newsgroup article. Perhaps one
> of the brethren with time on his hands could
> code up an example. Preferably one using tiny
> spheres as the points. :-)

If you want to see the points in space, and spin them to get a sense of the
array (sorry, but there are many ways to interpret your problem
description). The following should give you an idea of how to go about it.
(code is also in attached scatterObj.pro)

;;    Set up a 3D array of points at each vertex of an [n x n x n] grid

n = 16

;;    For each of n^3 points, make array of x, y, z coordinates

n = Long(n)                             ; ensure proper calculations below
x = LIndGen(n^3) MOD n
y = LIndGen(n^3) MOD (n^2) / n
z = LIndGen(n^3)/(n^2)

;;    Make 'function' values for each point
;;    This makes a white octahedron inside red cube

f = Abs(x-n/2) + Abs(y-n/2) + Abs(z-n/2) LT n/2

;;    Make a palette with [red, white] as entries 0 and 1

oPal = Obj_New('IDLgrPalette',[255,255],[0,255],[0,255])

;;    Make a colored point cloud (Style 0 is points-only, no lines)

oPol = Obj_New('IDLgrPolygon', x, y, z, Style=0, Thick=2, \$ ; size of points
Palette=oPal, Vert_Colors=f)

;;    Make axes

oAxis = ObjArr(3)
oXtitle = obj_new('IDLgrText', '<- X ->', Color=[255B, 255B, 255B])
oAxis[0] = obj_new('IDLgrAxis', 0, /exact, Range=[0, n-1], \$
title=oXtitle, \$
Color=[255B, 255B, 255B])

oYtitle = obj_new('IDLgrText','<- Y ->', Color=[255B, 255B, 255B])
oAxis[1] = obj_new('IDLgrAxis', 1, /exact, Range=[0, n-1], \$
title=oYtitle, \$
Color=[255B, 255B, 255B])

oZtitle = obj_new('IDLgrText','<- Z ->', Color=[255B, 255B, 255B])
oAxis[2] = obj_new('IDLgrAxis', 2, /exact, Range=[0, n-1], \$
title=oZtitle, location=[0,1,0], \$
Color=[255B, 255B, 255B])

;;    View the point cloud and axes

XObjView, [oPol, oAxis], Background='Black'

END

=====

Hope this helps!

Cheers,
--
-Dick

Dick Jackson                   /            dick@d-jackson.com
D-Jackson Software Consulting /       http://www.d-jackson.com
Calgary, Alberta, Canada     / +1-403-242-7398 / Fax: 241-7392

begin 666 scatterObj.pro
M.SL@(" @4V5T('5P(&\$@,T0@87)R87D@;V8@<&]I;G1S(&%T(&5A8V@@=F5R
M=&5X(&]F(&%N(%MN('@@;B!X(&Y=(&=R:60-"@T*;B ](#\$V#0H-"CL[(" @
M(\$9O<B!E86-H(&]F(&Y>,R!P;VEN=',L(&UA:V4@87)R87D@;V8@>"P@>2P@
M>B!C;V]R9&EN871E<PT*#0IN(#T@3&]N9RAN*2 @(" @(" @(" @(" @(" @
M(" @(" @(" @(" @.R!E;G-U<F4@<')O<&5R(&-A;&-U;&%T:6]N<R!B96QO
M=PT*>" ](\$Q);F1'96XH;EXS*2!-3T0@;@T*>2 ](\$Q);F1'96XH;EXS*2!-
M3T0@*&Y>,BD@+R!N#0IZ(#T@3\$EN9\$=E;BAN7C,I+RAN7C(I#0H-"CL[(" @
M(\$UA:V4@)V9U;F-T:6]N)R!V86QU97,@9F]R(&5A8V@@<&]I;G0-"CL[(" @
M(%1H:7,@;6%K97,@82!W:&ET92!O8W1A:&5D<F]N(&EN<VED92!R960@8W5B
M90T*#0IF(#T@06)S*'@M;B\R*2 K(\$%B<RAY+6XO,BD@*R!!8G,H>BUN+S(I
M(\$Q4(&XO,@T*#0H[.R @("!-86ME(&\$@<&%L971T92!W:71H(%MR960L('=H
M:71E72!A<R!E;G1R:65S(# @86YD(#\$-"@T*;U!A;" ](\$]B:E].97<H)TE\$
M3&=R4&%L971T92<L6S(U-2PR-35=+%LP+#(U-5TL6S L,C4U72D-"@T*.SL@
M(" @36%K92!A(&-O;&]R960@<&]I;G0@8VQO=60@*%-T>6QE(# @:7,@<&]I
M;G1S+6]N;'DL(&YO(&QI;F5S*0T*#0IO4&]L(#T@3V)J7TYE=R@G241,9W)0
M;VQY9V]N)RP@>"P@>2P@>BP@4W1Y;&4],"P@5&AI8VL],BP@)" [('-I>F4@
M;V8@<&]I;G1S#0H@(" @(" @(" @(" @("!086QE='1E/6]086PL(%9E<G1?
M0V]L;W)S/68I#0H-"CL[(" @(\$UA:V4@87AE<PT*#0IO07AI<R ](\$]B:D%R
M<B@S*0T*;UAT:71L92 ](&]B:E]N97<H)TE\$3&=R5&5X="<L("<\+2!8("T^
M)RP@0V]L;W(]6S(U-4(L(#(U-4(L(#(U-4)=*0T*;T%X:7-;,%T@/2!O8FI?
M;F5W*"=)1\$QG<D%X:7,G+" P+" O97AA8W0L(%)A;F=E/5LP+"!N+3%=+" D
M#0H@(" @(" @(" @(" @(" @(" @=&ET;&4];UAT:71L92P@) T*(" @(" @
M(" @(" @(" @(" @(\$-O;&]R/5LR-35"+" R-35"+" R-35"72D-"@T*;UET
M:71L92 ](&]B:E]N97<H)TE\$3&=R5&5X="<L)SPM(%D@+3XG+"!#;VQO<CU;
M,C4U0BP@,C4U0BP@,C4U0ETI#0IO07AI<ULQ72 ](&]B:E]N97<H)TE\$3&=R
M07AI<R<L(#\$L("]E>&%C="P@4F%N9V4]6S L(&XM,5TL("0-"B @(" @(" @
M(" @(" @(" @("!T:71L93UO671I=&QE+" D#0H@(" @(" @(" @(" @(" @
M(" @0V]L;W(]6S(U-4(L(#(U-4(L(#(U-4)=*0T*#0IO6G1I=&QE(#T@;V)J
M7VYE=R@G241,9W)497AT)RPG/"T@6B M/B<L(\$-O;&]R/5LR-35"+" R-35"
M+" R-35"72D-"F]!>&ES6S)=(#T@;V)J7VYE=R@G241,9W)!>&ES)RP@,BP@
M+V5X86-T+"!286YG93U;,"P@;BTQ72P@) T*(" @(" @(" @(" @(" @(" @
M('1I=&QE/6]:=&ET;&4L(&QO8V%T:6]N/5LP+#\$L,%TL("0-"B @(" @(" @
M(" @(" @(" @("!#;VQO<CU;,C4U0BP@,C4U0BP@,C4U0ETI#0H-"CL[(" @
M(%9I97<@=&AE('!O:6YT(&-L;W5D(&%N9"!A>&5S#0H-"EA/8FI6:65W+"!;
I;U!O;"P@;T%X:7-=+"!"86-K9W)O=6YD/2=";&%C:R<-"@T*14Y\$#0H`
`
end

```