3
$\begingroup$

I have the following data points:

SeedRandom[1];

array = RandomReal[1, {1000, 2}];

ListPlot[array, Frame -> True, FrameLabel -> {"x", "y"}]

enter image description here

a) How can I select the data points in the range x = [0.4, 0.7]?

b) What would be the solution if I need the data in the rectangle: x = [0.4, 0.7] and y = [0.2, 0.6]?

$\endgroup$
9
$\begingroup$

You can use Select:

a)

ListPlot[#, Frame -> True, FrameLabel -> {"x", "y"},
         PlotRange -> {{0, 1}, {0, 1}}]& @
          Select[array, 0.4 < #[[1]] < 0.7 &]

enter image description here

b)

ListPlot[#, Frame -> True, FrameLabel -> {"x", "y"}, 
         PlotRange -> {{0, 1}, {0, 1}}]& @
          Select[array, 0.4 < #[[1]] < 0.7 && 0.2 < #[[2]] < 0.6 &]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you for this solution ... $\endgroup$ – lio Nov 8 '16 at 13:41
10
$\begingroup$

Using ImplicitRegion to define regions

SeedRandom[1];

array = RandomReal[1, {1000, 2}];

rgn1 = ImplicitRegion[0.4 <= x <= 0.7, {x, y}];
rgn2 = ImplicitRegion[0.4 <= x <= 0.7 &&
    0.2 <= y <= 0.6, {x, y}];

ListPlot[
 {array, Select[array, # ∈ rgn1 &]},
 PlotStyle -> {
   Directive[LightGray, PointSize[Small]],
   Directive[Red, PointSize[Medium]]},
 Frame -> True,
 FrameLabel -> (Style[#, 14, Bold] & /@ {"x", "y"}),
 ImageSize -> Medium]

enter image description here

ListPlot[
 {array, Select[array, # ∈ rgn2 &]},
 PlotStyle -> {
   Directive[LightGray, PointSize[Small]],
   Directive[Red, PointSize[Medium]]},
 Frame -> True,
 FrameLabel -> (Style[#, 14, Bold] & /@ {"x", "y"}),
 ImageSize -> Medium]

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.