# Plotting entries of one table vs entries of another with a condition on a third table

I have three Tables, say, A={1,2,3,4,5}, B={1,2,3,4,5}, C={1.1,2.2,17,1.5,5} of the same length. I'd like to plot (using ListPlot) entries of table A vs the corresponding entries of table B if the corresponding entries of C lie between, say, 1 and 3.

That is, the list of points to plot is {{1,1},{2,2},{4,4}}.

How do I do that?

As a next step, I'd like to produce multiple plots (on one diagram) of entries of A vs entries of B for various conditions on C.

• Something like Pick[Transpose[{A, B}], Thread[1 <= C <= 3]] // ListPlot would work, I guess. – J. M. is in limbo Jun 7 '15 at 1:11
• @Guesswhoitis, Thanks. Works perfectly. – Physicsworks Jun 7 '15 at 1:19
• Good to hear. May I suggest answering your own question, then? – J. M. is in limbo Jun 7 '15 at 1:44

Use Position first to find the index, then use Extract

a = {1, 2, 3, 4, 5};
b = {1, 2, 3, 4, 5};
c = {1.1, 2.2, 17, 1.5, 5};
idx = Position[c, x_ /; 1 < x < 3];
ListLinePlot[Transpose[{Extract[a, idx], Extract[b, idx]}], Mesh -> All] Based on Guesswhoitis's comment, the solution is to use Pick with Thread:

Pick[Transpose[{A, B}], Thread[1 <= C <= 3]] // ListPlot

ListPlot also allows to plot several plots using

ListPlot[{list1,list2,...}]

If you are going to plot A and B over several conditions derived from C, it would probably be worth writing a function that not only takes A, B, and C, but also a list of the conditions expressed as pure functions and does the selection for every function on the list. This is not a very difficult extension of what you already have.

a = {1, 2, 3, 4, 5};
b = {1, 2, 3, 4, 5};
c = {1.1, 2.2, 17, 1.5, 5};
f = {1 <= # <= 3 &, # > 2. &};

pickByC[a_, b_, c_, funcs_] :=
Pick[Transpose[{a, b}], #] & /@ Table[f /@ c, {f, funcs}]

ListPlot[pickByC[a, b, c, f],
PlotMarkers ->
{Graphics[{Red, Disk[{0, 0}, Scaled@.03]}],
Graphics[{Black, Disk[{0, 0}, Scaled@.017]}]}] Note: because the points from the two sets plotted were both selected from the same underlying data, I took some care to make sure that when one data point was selected twice, the duplication was visible in the plot. If there were a large number of criteria functions, the visibility of multiples could become an significant issue.

### Update

As Guess who it is. points out in his comment

pickByC[a_, b_, c_, funcs_] := Table[Pick[Transpose[{a, b}], f /@ c], {f, funcs}]


is a better formulation.

• Why Map[] twice? Table[Pick[Transpose[{a, b}], f /@ c], {f, funcs}]. But if you must, Pick[Transpose[{a, b}], # /@ c] & /@ funcs. – J. M. is in limbo Jun 7 '15 at 5:04
• @Guesswhoitis. My only excuse is that I was very tired when I wrote this answer. – m_goldberg Jun 7 '15 at 14:22