I have a list that looks like this:
list = {{2}, {3}, {5, 2}, {9, 3, 5}};
I want a plot that looks like this:
Is there a simple way to do this?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI have a list that looks like this:
list = {{2}, {3}, {5, 2}, {9, 3, 5}};
I want a plot that looks like this:
Is there a simple way to do this?
list = {{2}, {3}, {5, 2}, {9, 3, 5}};
ListPlot@Flatten[#, 1] &@(Tuples /@ MapIndexed[{#2, #1} &, list])
Or in case you want them to have different colors:
ListPlot[#, PlotStyle -> {Blue, Red, Green, Orange}] &@
(Tuples/@MapIndexed[{#2, #1} &, list])
How about this? At least until a better solution shows up?
list = {{2}, {3}, {5, 2}, {9, 3, 5}};
r = Table[{Red, PointSize[.02], Point[{i, #}] & /@ list[[i]]}, {i,Length[list]}];
Graphics[r, Axes -> True, AspectRatio -> 1, AxesOrigin -> {0, 0},
GridLines -> Automatic, GridLinesStyle -> LightGray]
list = {{2}, {3}, {5, 2}, {9, 3, 5}};
n = 1; list2 = Thread[{n++, #}] & /@ list
(* {{{1, 2}}, {{2, 3}}, {{3, 5}, {3, 2}}, {{4, 9}, {4, 3}, {4, 5}}} *)
ListPlot[list2, AxesOrigin -> {0, 0}]
This is similar to BlacKow's answer, in that MapIndexed[]
is the key function for adding the abscissas:
ListPlot[MapIndexed[Transpose[PadRight[{#2, #1}, Automatic, #2]] &, list],
AxesOrigin -> {0, 0}, GridLines -> Automatic]
If they are meant to be part of just one set of data, add a Flatten[]
:
ListPlot[Flatten[MapIndexed[Transpose[PadRight[{#2, #1}, Automatic, #2]] &, list], 1],
AxesOrigin -> {0, 0}, GridLines -> Automatic]
Another way to use MapIndexed
:
ListPlot[MapIndexed[Thread[{#2[[1]], #}] &, list],
PlotStyle -> PointSize[.03], PlotRange -> Full, Frame -> True, PlotRangePadding -> 1]
or MapThread
ListPlot[MapThread[Thread[{##}] &, {Range@Length@list, list}],
PlotStyle -> PointSize[.03], PlotRange -> Full, Frame -> True, PlotRangePadding -> 1]
Another method:
list = {{2}, {3}, {5, 2}, {9, 3, 5}};
ListPlot[PadLeft[list, Automatic, ""]\[Transpose]]
Add e.g. PlotStyle -> Blue
for uniform styling: