1
$\begingroup$

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.

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

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]

Mathematica graphics

$\endgroup$
3
$\begingroup$

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,...}]

$\endgroup$
3
$\begingroup$

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

plot

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.

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

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.