0
$\begingroup$

I would like to exclude the region outside the yellow Polygon in this plot, how can I do that?

datr = Flatten[
   ParallelTable[{x, y, Sin[x y]}, {x, -3, 3, 0.1}, {y, -3, 3, 0.1}], 
   1];
plotTowD = 
  ListDensityPlot[datr, 
   ColorFunction -> (Blend[{Red, Gray, Blue}, Rescale[#, {-1, 1}]] &),
    ColorFunctionScaling -> False, ClippingStyle -> Automatic];
Boundrs = {{-(\[Pi]/3), -(\[Pi]/Sqrt[3])}, {-((2 \[Pi])/3), 
    0}, {-(\[Pi]/3), \[Pi]/Sqrt[3]}, {\[Pi]/3, \[Pi]/
     Sqrt[3]}, {(2 \[Pi])/3, 
    0}, {\[Pi]/3, -(\[Pi]/Sqrt[3])}, {\[Pi]/
     3, -(\[Pi]/Sqrt[3])}, {-(\[Pi]/3), -(\[Pi]/Sqrt[3])}};
plot = ListLinePlot[Boundrs, PlotStyle -> Yellow];
Show[{plotTowD, plot}]      

enter image description here

update

when adding InterpolationOrder -> 0 to ListDensityPlot the answers provided below takes too long to give the output?!

$\endgroup$
1
  • $\begingroup$ use the option RegionFunction -> (RegionMember[Polygon@Boundrs][{#, #2}] &) in ListDensityPlot? $\endgroup$
    – kglr
    Jan 8 at 1:06
2
$\begingroup$

that is great, but why does it take too long time (84 sec) when adding InterpolationOrder -> 0 to ListDensityPlot?

As I already demonstrated in the answer to another question of yours, using precomputed RegionMemberFunction as the value for the RegionFunction option gives huge speedup. In this particular case with InterpolationOrder -> 0:

Boundrs = {{-(π/3), -(π/Sqrt[3])}, {-((2 π)/3), 
    0}, {-(π/3), π/Sqrt[3]}, {π/3, π/
     Sqrt[3]}, {(2 π)/3, 
    0}, {π/3, -(π/Sqrt[3])}, {π/
     3, -(π/Sqrt[3])}, {-(π/3), -(π/Sqrt[3])}};
region = Polygon[Boundrs];
rf = RegionMember[region];
datr = Flatten[ParallelTable[{x, y, Sin[x y]}, {x, -3, 3, 0.1}, {y, -3, 3, 0.1}], 1];
plotTowD0 = ListDensityPlot[datr, 
   ColorFunction -> (Blend[{Red, Gray, Blue}, Rescale[#, {-1, 1}]] &), 
   ColorFunctionScaling -> False, ClippingStyle -> Automatic, 
   RegionFunction -> Function[{x, y}, rf[{x, y}]], InterpolationOrder -> 0] // EchoTiming

output

As to why computing with InterpolationOrder -> 0 takes more time than with the default InterpolationOrder -> 1, let us compare the complexity of the obtained graphics (plotTowD is the same as plotTowD0, but is evaluated without the option InterpolationOrder):

Through[{LeafCount, ByteCount}[plotTowD]]
Through[{LeafCount, ByteCount}[plotTowD0]]
{28976, 177384}
{83650, 492160}

We see that the difference in complexity of obtained expressions is substantial! Let us check what is inside, using my shortInputForm function:

Import["http://raw.github.com/AlexeyPopkov/shortInputForm/master/shortInputForm.m"]

plotTowD // First // shortInputForm
plotTowD0 // First // shortInputForm

screenshot

We immediately see from the values of the option VertexColors that plotTowD contains 2204 colored vertex points between which the interiors of the Polygon objects are interpolated, while plotTowD0 contains 8589 such points. We also see that in the case of plotTowD0 the obtained graphics is composed of 5663 Polygon objects, while plotTowD contains only 2566 of them! Apparently, with the option InterpolationOrder -> 0 ListDensityPlot does a lot more work even though the resulting image looks simpler/rougher!

$\endgroup$
2
  • $\begingroup$ thanks for the detailed answer! $\endgroup$ Jan 9 at 13:41
  • $\begingroup$ @valarmorghulis You are welcome! $\endgroup$ Jan 9 at 13:43
1
$\begingroup$
Boundrs = {{-(π/3), -(π/Sqrt[3])}, {-((2 π)/3), 
    0}, {-(π/3), π/Sqrt[3]}, {π/3, π/
     Sqrt[3]}, {(2 π)/3, 
    0}, {π/3, -(π/Sqrt[3])}, {π/
     3, -(π/Sqrt[3])}, {-(π/3), -(π/Sqrt[3])}};
region = Polygon[Boundrs]
datr = Flatten[
   ParallelTable[{x, y, Sin[x y]}, {x, -3, 3, 0.1}, {y, -3, 3, 0.1}], 
   1];
plotTowD = 
 ListDensityPlot[datr, 
  ColorFunction -> (Blend[{Red, Gray, Blue}, Rescale[#, {-1, 1}]] &), 
  ColorFunctionScaling -> False, ClippingStyle -> Automatic, 
  RegionFunction -> Function[{x, y}, {x, y} ∈ region]]

enter image description here

$\endgroup$
1
  • $\begingroup$ that is great, but why does it take too long time (84 sec) when adding InterpolationOrder -> 0 to ListDensityPlot? $\endgroup$ Jan 8 at 1:21

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.