1
$\begingroup$

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:

enter image description here

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.

$\endgroup$
1
$\begingroup$

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]

enter image description here

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]

enter image description here

Original answer:

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

enter image description here

Alternatively,

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

enter image description here

Or use the option PlotRange:

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

enter image description here

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

enter image description here

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – Wiliam Nov 27 '18 at 17:23
  • $\begingroup$ @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? $\endgroup$ – kglr Nov 27 '18 at 17:27
  • $\begingroup$ 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. $\endgroup$ – Wiliam Nov 27 '18 at 17:30
  • $\begingroup$ @William, please see the update. $\endgroup$ – kglr Nov 27 '18 at 17:40
2
$\begingroup$
Plot[Interpolation[l1][x], {x, 0, l2[[1, 1]]}, 
  PlotRange -> {{0, Length[l1]}, Automatic}, 
  Epilog -> {Red, AbsolutePointSize[4], Point /@ l2}] // Quiet

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.