3
$\begingroup$

Given a few lists as below

    l1 = {x1 -> 0.5, x2 -> 0.7, x3 -> 0.1, x4 -> 1};
l2 = {x1 -> 0.5, x2 -> 0.7, x3 -> 0.2, x4 -> 1.1};
l3 = {x1 -> 0.5, x2 -> 0.7, x3 -> 0.3, x4 -> 1.2};
l4 = {x1 -> 0.5, x2 -> 0.7, x3 -> 0.4, x4 -> 1.3};

How can one plot x3 vs x4 from these? I am dealing with a large number of such lists, so its not possible to copy past the entries.

$\endgroup$

2 Answers 2

5
$\begingroup$

If your lists are together in one larger list like this,

lists = {l1, l2, l3, l4, <... potentially many more lists ...>};

then you can use Lookup to extract the data you need and just plot it with ListPlot:

ListPlot[Lookup[lists, {x3, x4}]]

Or if you just want to reference the individual lists:

ListPlot[Lookup[{l1, l2, l3, l4}, {x3, x4}]]
$\endgroup$
2
  • $\begingroup$ Thanks, @lericr, that's simple and elegant! Just on more thing. How about if I want plot, say, x1+x2 vs x3+x4 ? $\endgroup$ Jun 3 at 5:25
  • $\begingroup$ One way would be to use the strategy @ydd showed: ListPlot[{x1 + x2, x3 + x4} /. lists]. You could also run a query-operator on your data. What's best kinda depends on the semantics of your lists, the origin of your lists, and the context of your situation. $\endgroup$
    – lericr
    Jun 4 at 12:52
4
$\begingroup$

If I am understanding correctly, you have a large number of lists that look like {x1 -> val1, x2 -> val2, x3 -> val3, x4 -> val4}. But if you gave them all variable names by hand (l1, l2 ,l3,etc.) it can't be a huge amount of lists. I would make a list of the lists and then use ReplaceAll to get the x3 and x4 values from each list

data = {l1, l2, l3, l4};
ListPlot[{x4,x3} /. data];

You probably want to make a list of the lists right from the start, instead of having a bunch of lists with separate variable names.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.