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

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

can be written

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

How can I do this?

  • 4
    $\begingroup$ 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! $\endgroup$
    – halirutan
    Feb 6 '13 at 12:17
  • $\begingroup$ Play around a bit with BinCounts and MatrixPlot $\endgroup$
    – ssch
    Feb 6 '13 at 12:24
  • 2
    $\begingroup$ @halirutan, I think ArrayPlot worked more faster and easy to set ColorFunction. $\endgroup$
    – chyanog
    Feb 6 '13 at 13:31
  • 1
    $\begingroup$ If your question is about setting the ColorFunction to the ListPlot, this is a duplicate of this: mathematica.stackexchange.com/q/1300/5 $\endgroup$
    – rm -rf
    Feb 6 '13 at 15:11

Here is one way:

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

We get:


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


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


Much more cool! :)

  • $\begingroup$ @chyanog Hi! Thank you for the accept. $\endgroup$
    – Murta
    Feb 8 '13 at 2:31

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];


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!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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