3
$\begingroup$

I want to attach one of two values (1 or -1) to some coordinates in the plane and plot these points in a color corresponding to this value, e.g. 1 shall correspond to Blue and -1 to Red. My coordinates and values are structured like this:

position = Table[{i, j}, {i, 0, 1}, {j, 0, 1}]
value = {{-1, -1}, {1, -1}}

In this example the coordinates {0, 0}, {0, 1} and {1, 1} have the value -1 attached to them and I want to plot them Red, the coordinate {1, 0} has the value 1 attached to it, so I want to plot it Blue and so on. How can I do this?

$\endgroup$
  • $\begingroup$ So in value, a tuple {a, b, c} denotes a point {b, c} with color determined by a? $\endgroup$ – C. E. Jan 25 '17 at 18:35
  • $\begingroup$ No, the table {{{a,b},{c,d}},{{e,f},{g,h}}} generated by position denotes the points {a,b}, {b,c}, {e,f} and {g,h} and value generates the corresponding values {{i,j},{k,l}}. So for example the value i corresponds to {a,b}, the value j to {c,d} and so on. $\endgroup$ – freddy90 Jan 25 '17 at 18:45
  • 1
    $\begingroup$ Something like Graphics[{PointSize[Medium], MapThread[{#2 /. {-1 -> Red, 1 -> Blue}, Point[#]} &, {position, value}, 2]}], then? $\endgroup$ – J. M. will be back soon Jan 25 '17 at 18:49
3
$\begingroup$

I'd use Style:

position = Table[{i, j}, {i, -1, 1}, {j, -1, 1}];
value = {{1, -1, -1}, {1, 1, -1}, {1, 1, -1}};

data = Style[{#1, #2}, If[#3 == -1, Red, Blue]] & @@@ (* coloring *)
  Flatten /@ Transpose@{Catenate@position, Catenate@value} (* I find this data
                                                             structure convenient *)

enter image description here

ListPlot[data, Frame -> True, PlotStyle -> PointSize[Large]]

enter image description here


Or

red  = Catenate@Pick[position, value, -1]
blue = Catenate@Pick[position, value, 1]

ListPlot[{red, blue}, Frame -> True, 
 PlotStyle -> {Directive[PointSize[Large], Red], 
               Directive[PointSize[Large], Blue]}]

plot as above

$\endgroup$
1
$\begingroup$

There are many solutions. Here is mine:

Graphics[{
    PointSize[Large],
    Point[
        Flatten[position,1],
        VertexColors->Replace[Flatten@value,{-1->Red,1->Blue},1]
    ]
}]
$\endgroup$

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.