1
$\begingroup$

I have a nested matrix xyPairsAll that contains {x, y} pairs:

{...
{{{1., 1812.}, {2., 10076.}, {3., 4764}},   
 {{1., 3475.}, {2., 3572.}, {3., 3985.}}}, 
{{{1., 6839.}, {2., 3849.}, {3., 2746}},    
 {{1., 3578.}, {2., 5629.}, {3., 3849.}}}, 
{{{1., 6839.}, {2., 3849.}, {3., 2746}},    
 {{1., 10092.}, {2., 1638.}, {3., 3728.}}}
...}

I need to plot xyPairsAll with ListPlot. It would be easy if I could just flatten it to one level, but I need to keep the sublists distinct so that I can color the plot according to sublist.

The following statement only plots the first sublist:

ListPlot[#, 
   PlotRange -> {{0, 4}, Full},
   Frame -> True,
   FrameTicks -> {{Automatic, None}, {{1, 2, 3}, None}},
   FrameLabel -> {"Time points", "Concentrations of CO2"},
   ImageSize -> Full,
   PlotLegends -> labels] & /@ xyPairsAll

How can I plot xyPairsAll so that both conditions are met:

  • each {x, y} pair gets its own dot
  • all {x, y} pairs from each sublist share the same color

EDIT: In the original data matrix, I had a header plus xyPairsAll. I set labels to part of the header for the columns for the y-coordinates which each represent values of CO2 for a location (like {"Toronto, ON", "Boston, MA", "Washington, DC"...}), and xyPairsAll to all the corresponding rows after the header. So each sublist in xyPairsAll represents {x, y} values for a location. If I flatten the xyPairsAll, I don't think I can tell which {x, y} values are for which location. It'd just be one long list of {x, y} pairs.

$\endgroup$
  • $\begingroup$ ListPlot@Flatten[xyPairsAll, 1]? $\endgroup$ – Henrik Schumacher Jul 8 '18 at 19:22
  • $\begingroup$ I've done that, but I can't tell if it labels accurately by sublist? Since the sublists change after doing Flatten[xyPairsAll, 1] @HenrikSchumacher $\endgroup$ – briennakh Jul 8 '18 at 19:30
  • $\begingroup$ It is not labeling accurately by sublist. $\endgroup$ – briennakh Jul 8 '18 at 19:39
  • 2
    $\begingroup$ Can you edit the question and give an example how you store the labels? Flatten preserves the order, so usually you can use something like (ListPlot[#[[1]],PlotLegends->#[[2]]]&)/@Transpose[{Flatten[xyPairsAll,1],Flatten[sublistlabels,1]}]. $\endgroup$ – Thies Heidecke Jul 8 '18 at 21:43
  • $\begingroup$ Edited my question. Please let me know if more information is needed. @ThiesHeidecke $\endgroup$ – briennakh Jul 9 '18 at 0:41
3
$\begingroup$

From the fact the ListPlot you tried produces only the first "sublist", I deduced that your sublists are the first level of the list. Then this must mean that you're trying to flatten the two levels below that. This can be done with Flatten too:

l = { {{{1., 1812.}, {2., 10076.}, {3., 4764}}, {{1., 3475.}, {2., 
     3572.}, {3., 3985.}}}, {{{1.1, 6839.}, {2.1, 3849.}, {3.1, 
     2746}}, {{1.1, 3578.}, {2., 5629.}, {3., 3849.}}}, {{{1., 
     6839.}, {2., 3849.}, {3., 2746}}, {{1., 10092.}, {2., 
     1638.}, {3., 3728.}}}}

ListPlot@Flatten[l, {{1}, {2, 3}}]

The second argument to Flatten says that level 1 should stay untouched, but that the next two levels should be merged.

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