Mathematica Asked by Vibin Ram Narayan on May 13, 2021
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
.
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]
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
Answered by kglr on May 13, 2021
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}]] ]
Answered by andre314 on May 13, 2021
Table
is OK.
n = 10;
list = Range[Range[2, n]]
Table[Table[{i, j}, {j, list[[i]]}], {i, 1, Length@list}]
ListPlot[%]
Answered by cvgmt on May 13, 2021
f[{a___, b_}] := {b - 1, #} & /@ {a, b}
f /@ {{1, 2}, {1, 2, 3}, {1, 2, 3, 4}, {1, 2, 3, 4, 5}} // ListPlot
Answered by wuyudi on May 13, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP