2
$\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$
5
  • $\begingroup$ ListPlot@Flatten[xyPairsAll, 1]? $\endgroup$ Jul 8, 2018 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$
    – brienna
    Jul 8, 2018 at 19:30
  • $\begingroup$ It is not labeling accurately by sublist. $\endgroup$
    – brienna
    Jul 8, 2018 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$ Jul 8, 2018 at 21:43
  • $\begingroup$ Edited my question. Please let me know if more information is needed. @ThiesHeidecke $\endgroup$
    – brienna
    Jul 9, 2018 at 0:41

1 Answer 1

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