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 rewrite this code with ArrayPlot,

n = 10;
eqs = x^Range[0, n - 1].# & /@ Tuples[{-1, 1}, n];
g = ListPlot[{Re@#, Im@#} & /@ Flatten[x /. NSolve /@ eqs]]

enter image description here

just like

ListPlot[Reap[
   Do[If[x^2 + y^2 < 500, Sow@{x, y}], 
      {x, -100, 100}, {y, -100, 100}]][[2, 1]], AspectRatio -> 1]

can be written

ArrayPlot[
 Table[If[x^2 + y^2 < 500, 1, 0], {x, -100, 100}, {y, -100, 100}]]

How can I do this?

share|improve this question
4  
Why would someone do this? Your initial points are on real valued positions, while a raster for ArrayPlot have fixed pixel positions. You will loose information that way! –  halirutan Feb 6 '13 at 12:17
    
Play around a bit with BinCounts and MatrixPlot –  ssch Feb 6 '13 at 12:24
2  
@halirutan, I think ArrayPlot worked more faster and easy to set ColorFunction. –  chyaong Feb 6 '13 at 13:31
1  
If your question is about setting the ColorFunction to the ListPlot, this is a duplicate of this: mathematica.stackexchange.com/q/1300/5 –  rm -rf Feb 6 '13 at 15:11
add comment

2 Answers

up vote 7 down vote accepted

Here is one way:

n = 10;
eqs = x^Range[0, n - 1].# & /@ Tuples[{-1, 1}, n];
data={Re@#,Im@#}&/@Flatten[x/.NSolve/@eqs];
dataBin=Sign@Transpose@BinCounts[data,{-2,2,0.01},{-2,2,0.01}]
ArrayPlot[dataBin]

We get:

image

Now playing a little with new Mathematica 9 Image3D, we get it rendered for each n, from 1 to 10:

data3D=Table[
    eqs=x^Range[0,n-1].#&/@Tuples[{-1,1},n];
    data={Re@#,Im@#}&/@Flatten[x/.NSolve/@eqs];
    Sign@Transpose@BinCounts[data,{-2,2,0.01},{-2,2,0.01}]
,{n,1,10}]//Quiet;

Image3D[data3D, BoxRatios -> {1, 1, 1}]

image2

Much more cool! :)

share|improve this answer
    
@chyanog Hi! Thank you for the accept. –  Murta Feb 8 '13 at 2:31
add comment

I would not use ArrayPlot because it expects a matrix while you have free point positions. In the processing of pressing the real valued positions on a grid, you will lose information.

Regarding your comment:

@halirutan, I think ArrayPlot worked more faster and easy to set ColorFunction.

When your goal is to colorize your points and you think setting the ColorFunction in ListPlot is too slow, why don't you plot your points manually? I assume doing something like

n = 10;
eqs = x^Range[0, n - 1].# & /@ Tuples[{-1, 1}, n];
data = {Re@#, Im@#} & /@ Flatten[x /. NSolve /@ eqs];

Graphics[Point[data]]

Mathematica graphics

can never be slower than calling ListPlot. Now you want to colorize each point which means you want to transform a point {x,y} into a color like {r,g,b}. Let's say you try to make this function as fast as possible, because it is called very often with many points. Therefore, you use Compile in a parallel fashion. One simple example would be

colfunc = Compile[{{point, _Real, 1}},
  .5*{Sin[point[[1]]], -Sin[point[[2]]], Cos[point[[2]]]} + .5,
  Parallelization -> True, RuntimeAttributes -> {Listable}
  ]

And then you see that you can give a list of colors to Point, colorizing each point differently when you use the option VertexColors:

Graphics[Point[data, VertexColors -> colfunc[data]]]

Mathematica graphics

This runs in no time compared to your NSolve call, which is about 1000 times slower!

share|improve this answer
add comment

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.