Here I have the codes for my Listplot.

α[δ_, s13_, s12_, θ12_, 
   c13_] := (-180/2 π  ) ArcTan[
    s13^2 s12^2 Sin[
       2 δ Degree]/(c13^2 Cos[2 θ12 Degree] s12^2 + 
        s13^2 s12^2 Sin[2 δ Degree])];
Tab1 = Table[{δ, α[δ, 
     Random[Real, {0.1363, 0.157}], Random[Real, {0.5, 0.595}], 
     Random[Real, {30.002, 36.6}], 
     Random[Real, {0.9906, 0.9875}]]}, {δ, 0, 190, 1 Degree}];
P1 = ListPlot[Tab1, PlotRange -> {-25, 25}, 
  FrameLabel -> {δ, α}, Frame -> True,  PlotStyle -> Red,]

It is \alpha vs \delta plot

Here I want to omit the points from the plot obtained for the ranges of delta {20,50} and {125,150}. Please suggest how do I do that.

  • 1
    $\begingroup$ welcome to MMA SE! Your table Tab1 uses a step size of 1 Degree. This means that in computing \[Alpha], you wind up with something of units Degree^2. Is this intentional? If the bounds 0 and 190 represent degrees, I suspect the step size should be 1. $\endgroup$ – thorimur Feb 18 at 7:32
  • $\begingroup$ ListPlot[Select[Tab1, Not@IntervalMemberQ[Interval[{20, 50}, {125, 150}], First[#]] &]] $\endgroup$ – LouisB Feb 18 at 7:35

thorimur's answer explains various ways to remove parts of input data before plotting.

An alternative approach is to hide (rather than remove) parts of the data. This can be done by constructing white rectangles with appropriate coordinates and combine them with the original plot using Show:

listplot = ListPlot[Tab1, PlotStyle -> Red, PlotRange -> {-25, 25}, 
   FrameLabel -> {δ, α}, Frame -> True];

hiddenranges = {{20, 50}, {125, 150}};

Show[listplot, Graphics @ {White, 
  Rectangle[Scaled[{0, -1}, {#, 0}], Scaled[{0, 1}, {#2, 0}]] & @@@ hiddenranges}]

enter image description here

  • 1
    $\begingroup$ learned the Scaled trick (which obviates the need to find vertical coordinates for the rectangles) from this answer by J.M.. $\endgroup$ – kglr Feb 18 at 10:54

One way is to change the construction of Tab1 in the first place, and simply Join together the ranges that you are going to keep.

Another is to use Select, Cases, or DeleteCases to modify Tab1. With Select, one could do that as

Select[Tab1, Not[20 <= #[[1]] <= 50] && Not[125 <= #[[1]] <= 150] &]

This applies the function in the second argument to each element of the list, and throws away the ones that give False, keeping ("selecting") only those that give True.

As @Louis B mentioned in a comment, you can also represent the interval as Interval[{20, 50}, {125, 150}], and test for membership via IntervalMemberQ. As @Louis B says, the corresponding function to use in the second argument would be Not @ IntervalMemberQ[Interval[{20,50}, {125,150}], First[#]] &.

With DeleteCases, one could do that as

DeleteCases[Tab1, {_?(20 <= # <= 50 || 125 <= # <= 150 &), _}]

This deletes all parts of the expression that match the pattern in the second argument, which matches any list of two expressions, the first of which yields true under the function enclosed in parentheses after ?. Another way to write that pattern is

DeleteCases[Tab1, {x_, _} /; (20 <= x <= 50 || 125 <= x <= 150)]

where x now names the first blank pattern, and /; can be read as "on the condition that".

Hope this helps; let me know if anything is unclear or mysterious!

  • $\begingroup$ Where should I write the code for the DeleteCases i.e in the next line of Tab1 or in the same line with tab 1. Or should I write it along with the line of P1 $\endgroup$ – Sangeeta Dey Feb 18 at 8:05
  • $\begingroup$ Anywhere that results in it being used by ListPlot in the definition of P1! You could write it at the beginning in the def of Tab1; you could define Tab2 = DeleteCases[...]; and use that in P1; or you could write it right inside ListPlot as ListPlot[DeleteCases[...], ...]; etc. $\endgroup$ – thorimur Feb 18 at 8:16
  • $\begingroup$ Thank you so so much. It really worked $\endgroup$ – Sangeeta Dey Feb 18 at 8:26

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.