2
$\begingroup$

I am new to mathematica. I have been trying to plot a list of values. The list looks somewhat like this:

list = {{1, 2}, {1, 2, 3}, {1, 2, 3, 4}}

What I want is to plot, $\{1,2\}$ (both these points) at $x =1$, and points $\{1,2,3\}$ at $x =2$, etc. I tried converting the list to a form $\{\{1,1,1\},\{2,2,2\},\{3,3\},\{4\}\}$ and list plotting, but this puts $\{1,2\}$ at $x = 3$, rather than $x = 1$. So if I can make ListPlot plot first values at $x = 3$ and go backwards from there my problem can be solved. But I could not find a way to specify the $x$ values for ListPlot.

$\endgroup$
1
  • $\begingroup$ Please consider adding the ListPlot you used to your question, so that people can see what you've tried. $\endgroup$
    – creidhne
    Nov 28, 2020 at 17:50

4 Answers 4

4
$\begingroup$

One possible approach is to use PadRight to obtain a rectangular 4X3 array :

list = {{1, 2}, {1, 2, 3}, {1, 2, 3, 4}}
Transpose[PadRight[list, Automatic, {Null}]]

{{1, 1, 1}, {2, 2, 2}, {Null, 3, 3}, {Null, Null, 4}}

and then ListPlot :

ListPlot[Transpose[ PadRight[list, Automatic, {Null}]] ]  

enter image description here

$\endgroup$
1
  • $\begingroup$ I think Missing[] instead of Null would be preferable, even though any non numeric value works here. $\endgroup$
    – swish
    Nov 29, 2020 at 12:02
4
$\begingroup$
list = {{1, 2}, {1, 2, 3}, {1, 2, 3, 4}};

Add x-coordinates to each data point using MapIndexed + Thread:

list2 = MapIndexed[Thread[{#2[[1]], #}] &] @ list
{{{1, 1}, {1, 2}}, 
 {{2, 1}, {2, 2}, {2, 3}},
 {{3, 1}, {3, 2}, {3, 3}, {3, 4}}}
ListPlot[list2, PlotStyle -> PointSize[Large], PlotLegends -> InputForm /@ list]

enter image description here

You can also use MapIndexed + Tuples, or MapThread + Thread to construct list2:

list3 = MapIndexed[Tuples @* Reverse @* List] @ list;;

list4 = MapThread[Thread @* List] @ {Range @ Length @ list, list};

list2 == list3 == list4
True
$\endgroup$
1
$\begingroup$

Table is OK.

n = 10;
list = Range[Range[2, n]]
Table[Table[{i, j}, {j, list[[i]]}], {i, 1, Length@list}]
ListPlot[%]

enter image description here

$\endgroup$
1
$\begingroup$
f[{a___, b_}] := {b - 1, #} & /@ {a, b}
f /@ {{1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}} // ListPlot
$\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.