How to extract the second lowest and the second largest values of each row, and plot them as upper and lower graphs? I try with TakeLargest (or TakeSmallest) as discussed here, but I do not want them sorted in ascending/descending order.

I want the graph to be like this. Yes, we can do that with Min, Max, Mean. But how to manipulate/extract any data that I could have without changing the originality of its order?

Here is some example of the data.











I do try with MaximalBy, but I don't think my extraction technique is correct. I'm not expecting any overlap between upper and lower graphs.

 ListLinePlot[Mean[MaximalBy[data, Last, 6]], PlotRange -> Full, 
  DataRange -> {0, 30}, PlotStyle -> Red],
 ListLinePlot[Mean[MinimalBy[data, First, 4]], PlotRange -> Full, 
  DataRange -> {0, 30}, PlotStyle -> Blue]

This sample does not vary much, but it was part of a larger dataset.

Thanks in advance for the guidance and help.


2 Answers 2

  RankedMax[#, 2] & /@ data,
  RankedMin[#, 2] & /@ data
  • $\begingroup$ I was not aware of Ranked* functions. Thanks. Your solution is quite elegant. I guess it would also work with jagged arrays as well whereas a Transpose based solution would not. $\endgroup$
    – Syed
    Commented Apr 29, 2022 at 15:45

With data as presented in the OP, the second lowest and the second highest entries (slsh) in the Sorted data are:

slsh = Transpose[#[[{2, -2}]] & /@ (Sort /@ data)]

 , PlotStyle -> {{Thick, Blue}
   , {Thick, Red}
 , Mesh -> All
 , Filling -> 1 -> {2}

enter image description here

alldata = Transpose[(Sort /@ data)]

enter image description here

  • $\begingroup$ The filling region helps to highlight the area of interest. Is it possible to calculate the percentage of the shaded area? **Note: Thank you for showing the method with Transpose & Sort to plot the region, I couldn't think of that. $\endgroup$
    – nightcape
    Commented Apr 29, 2022 at 16:27
  • $\begingroup$ % of the area compared to what? The entire area? I think you can start a fresh post with this query. First, the shaded area will have to be converted to a polygon. Then the same will be done with the entire area before a percentage can be calculated. I say this because it is a general policy on stack sites to limit the Q/A to one focused discussion per page. $\endgroup$
    – Syed
    Commented Apr 29, 2022 at 17:14
  • $\begingroup$ Yes, I'll work on the problem, perhaps one with a more interesting dataset, and post another question. On another note, your answer here does help, but I may have to accept Henrik's for now $\endgroup$
    – nightcape
    Commented Apr 30, 2022 at 15:29
  • $\begingroup$ @nightcape That is the better answer for sure. $\endgroup$
    – Syed
    Commented Apr 30, 2022 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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