# Median of multiple lists of points

I have three lists of points that I want to combine, take the median of and then plot. I'm not entirely certain how to combine and median them. They are data points from three different months, and they repeat in cycles of 0.5 to 149.5 on the y component. Each list is in the format

{{0.023,0.5},{0.059,1.5},{0.012,3.5}}


with y going up up to 149.5. I want to take the median of each x component that has the same y. As in grouping all the points with a y value of 0.5 and taking the median of the x, and repeating for all y values.

Any help would be appreciated!

• Could you please write down an example of how the list should look after the combination? Commented Jan 24, 2019 at 19:35

ListPlot@(x = Flatten[{{0.023, 0.5}, {0.059, 1.5}, {0.012, 3.5}}])


Can use Flatten or Join or Union.

Median@x


First, some fake data with a structure similar to yours:

SeedRandom[1]
ys = RandomChoice[Range[5], {50, 3}];
xs = RandomReal[1, {50, 3}];
data = Transpose /@ Transpose[{xs, ys}];


Use GroupBy with Median as the third argument to get the median x coordinate for each group:

groupmeds = GroupBy[Join @@ data, Last -> First, Median]


<|5 -> 0.541191, 3 -> 0.432168, 1 -> 0.691349, 2 -> 0.341261, 4 -> 0.425522|>

meddata = Reverse /@ (List @@@ Normal@groupmeds);

Show[ListPlot[meddata, PlotTheme -> "OpenMarkersThick",
PlotStyle -> Red, PlotLegends -> {"Medians"}],
ListPlot[data, PlotLegends -> {"month1", "month2", "month3"},
PlotStyle -> PointSize[Medium]], PlotRange -> All, Frame -> True,
Axes -> False, ImageSize -> Large]


Note: You can also use GatherBy to get group medians:

{Median@#[[All, 1]], #[[All, 2]][[1]]} & /@ GatherBy[Join @@ data, #[[-1]] &] == meddata


True