# Plotting two lists

I have the following lists:

l1={97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97,
97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97};

l2={{52.4362, 97}}


I would plot together them as:

Show[l1 // ListLinePlot,
l2 // ListPlot]


Note I used list line plot and list plot in format as appropriate to my general example which I would want to use this. The resulting plot is:

However, I want to cut the rest of x after 52.4362. So I want to plot this up until 52.4362. How is this possible? Note that I have simplified my example in real problem I have many lists so I can not simply cut l1 to desired length.

Update: A generalization to pairs of multiple lists:

trim = MapThread[#[[;;Floor[#2[[1]]]]] &, {If[Length@Dimensions@# == 1, {#}, #], #2}]&;


Examples:

Show[trim[l1, l2] // ListLinePlot, l2 // ListPlot]


l1 = {Table[95, {i, 97}], Table[96, {i, 97}], Table[97, {i, 97}]};
l2 = {{44.0866, 95}, {45.628, 96}, {52.4362, 97}};
Show[trim[l1, l2] // ListLinePlot, l2 // ListPlot]


Show[l1[[;; Floor[l2[[1, 1]]]]] // ListLinePlot, l2 // ListPlot]


Alternatively,

l1b = Select[#[[1]] <= l2[[1, 1]] &]@MapIndexed[{#2[[1]], #} &, l1];
ListPlot[{l2, l1b}, Joined -> {False, True}]


Or use the option PlotRange:

Show[l1 // ListLinePlot, l2 // ListPlot,
PlotRange -> {{0, l2[[1, 1]]}, All},


ListPlot[{l2, l1}, Joined -> {False, True},
PlotRange -> {{0, l2[[1, 1]]}, All}, PlotRangeClipping -> False,


• Thanks @kglr as always. I am trying to generalise this what if I have: l1={Table[95, {i, 97}], Table[96, {i, 97}], Table[97, {i, 97}]} and l2={{44.0866, 95}, {45.628, 96}, {52.4362, 97}} how can I generalise this method? Commented Nov 27, 2018 at 17:23
• @William, does Show[l1[[All, ;; Floor[l2[[-1, 1]]]]] // ListLinePlot, l2 // ListPlot] give the desired result? or do you need to cut Table[95, {i, 97}] at 44.0866 and Table[96, {i, 97}] at 45.628 etc?
– kglr
Commented Nov 27, 2018 at 17:27
• the latter is needed indeed, i.e Table[95, {i, 97}] cut at 44.0866, Table[95, {i, 97}] cut at 45.628 and so on. Commented Nov 27, 2018 at 17:30
• @William, please see the update.
– kglr
Commented Nov 27, 2018 at 17:40
Plot[Interpolation[l1][x], {x, 0, l2[[1, 1]]},
PlotRange -> {{0, Length[l1]}, Automatic},
Epilog -> {Red, AbsolutePointSize[4], Point /@ l2}] // Quiet