0
$\begingroup$

Let us assume I have two lists of the same lenght and in the form:

mesh ={{0.1, 0.3}, {0.1, 0.8}, {0.1, 1.3}, {0.6, 0.3}, {0.6, 0.8}, {0.6,1.3}, {1.1, 0.3}, {1.1, 0.8}, {1.1, 1.3}, {1.6, 0.3}, {1.6, 0.8}, {1.6, 1.3}};

colors={1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0}

I'd like to make a ListPlot with the values form mesh and make every point green if the corresponding point in colors is 1 or elese red. If it helps you can assume that there exists a function f that for every element of mesh gives the corresponding element color.

My idea was to generate all the plots separately and save them into some variable names and then just use Show to display them all at once. I tried implementing this with

Do[
 If[f[k] == 1,
  Symbol["plot" <> ToString[Flatten[Position[mesh, k]][[1]]]] = 
   ListPlot[{k}, PlotStyle -> Green],
  Symbol["plot" <> ToString[Flatten[Position[mesh, k]][[1]]]] = 
   ListPlot[{k}, PlotStyle -> Red]], {k, mesh}]

but this gives me the error

Set::write: Tag Symbol in Symbol[plot1] is Protected.

and in general seems like a rather cluncky methode to get this done... Can anybody suggest a (more sophisticated) solution.

$\endgroup$
2
$\begingroup$

If I understand you correctly, you can use a Style wrapper to control the colors:

ListPlot[Thread @ Style[mesh, Lookup[{1->Green, 0->Red}, colors]]]

enter image description here

Or you can segregate the points using GroupBy:

ListPlot[
    Lookup[
        GroupBy[Thread[{mesh, colors}],Last->First],
        {1,0}
    ],
    PlotStyle->{Green,Red}
]

enter image description here

Or you can use Point with a VertexColors option in Graphics:

Graphics[
    {
    PointSize[Medium],
    Point[mesh, VertexColors -> Lookup[{1->Green, 0->Red}, colors]]
    },
    Axes->True, AxesOrigin->{0,0}
]

enter image description here

$\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.