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 am trying to plot two lists, where one is the reference list, and the other is only used to select and highlight the first list. Basically, I want to use the selection list slist to highlight different points in the reference list reflist after using Manipulate:

reflist = Flatten[Table[{x, y, z, a}, {x, 0, 4}, {y, -4, -1}, {z, 0, 5}, {a,0, 3}], 3];
slist = Flatten[Table[{x, y, z, a}, {x, 0, 1}, {y, -2, -2}, {z, 0, 1}, {a, 0, 1}],3];

Manipulate[
 Show[ListPlot[
   Select[reflist, #[[1]] == x && #[[2]] == y &][[All, 3 ;; 4]], 
   PlotStyle -> Black, AxesLabel -> {"z", "a"}, PlotLabel -> {x, y}, 
   PlotRange -> All, Joined -> False], 
  ListLinePlot[
   Select[slist, #[[1]] == x && #[[2]] == y &][[All, 3 ;; 4]], 
   PlotStyle -> Red, AxesLabel -> {"z", "a"}, PlotRange -> All, 
   Joined -> False]], {x, 0, 4, 1}, {y, -4, -1, 1}]

Returns the following graph when x=1 and y=-2: enter image description here

The problem occurs when slist has no point in common with reflist: enter image description here

So my questions are: 1. Is there any way for me to avoid getting the error and just have no point highlighted when the two lists have no points in common, and just show all points in black? 2. I am also looking to highlight the whole regions where the points are (this would make a square in the first figure, with vertices in each red point). Is there any way to do it in ListPlot?

Thanks so much for all the help!

share|improve this question
add comment

2 Answers 2

up vote 4 down vote accepted

Here's one way:

Manipulate[
   q = Select[slist, #[[1]] == x && #[[2]] == y &][[All, 3 ;; 4]]; 
   Show[ListPlot[Select[reflist, #[[1]] == x && #[[2]] == y &][[All, 3 ;; 4]], 
         PlotStyle -> Black, AxesLabel -> {"z", "a"}, PlotLabel -> {x, y}, 
         PlotRange -> All, Joined -> False], 
       If[q == {}, {}, ListLinePlot[q, PlotStyle -> Red, AxesLabel -> {"z", "a"}, 
         PlotRange -> All, Joined -> False]]], {x, 0, 4, 1}, {y, -4, -1, 1}]

What this does is to test if the plot is going to be empty, if so, plot the empty set. If it's non-empty, plot the new one on top. Thanks to Istvan for the improvement.

share|improve this answer
    
Thanks! It helped me a lot! –  Sosi Apr 8 '13 at 16:32
add comment

Answering also your second part:

. I am also looking to highlight the whole regions where the points are (this would make a square in the first figure, with vertices in each red point)

reflist = Flatten[Table[{x, y, z, a}, {x, 0, 4}, {y, -4, -1}, {z, 0, 5}, {a,  0, 3}], 3];
slist =   Flatten[Table[{x, y, z, a}, {x, 0, 1}, {y, -4, -2}, {z, 0, -y}, {a, 0, x}], 3];
<< ComputationalGeometry`
filter[list_, x_, y_] := If[#=={}, {Undefined[]},#] &@ Cases[list, {x, y, __}][[All, 3 ;; 4]]
Manipulate[
 Show[ListPlot[{filter[reflist, x, y], filter[slist, x, y]}, 
               PlotStyle -> {{PointSize[Large], Black}, Red}, Joined -> False,
               AxesLabel -> {"z", "a"}, PlotLabel -> {x, y}, PlotRange -> All],
      If[# != {Undefined[]}, Graphics[GraphicsComplex[#, Polygon@ConvexHull[#]]], {}] &@
                                                                       filter[slist, x, y]], 
{x, 0, 4, 1}, {y, -4, -1, 1}]

enter image description here

share|improve this answer
    
Thanks @belisarius, it works like a charm! –  Sosi Apr 8 '13 at 16:32
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.