3
$\begingroup$

I am trying to create a graphics object (polygon built with {x,y} points)so I can plot a contour over it. I would like the coordinates of the contour plot and graphics object be matched (ie, {0,0} in graphics = {0,0} in contourplot).

Below is an attempt

r1 = Graphics[
   Polygon[{{0, 0}, {0, -12}, {5, -12}, {5, 0}, {15, 20}, {10, 
      20}}]];

r2 = Graphics[
  Polygon[{{0, 0}, {0, -12}, {-5, -12}, {-5, 0}, {-15, 20}, {-10, 
     20}}]]

sum = Show[r1, r2, ImageSize -> 400]

im2 = ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5}, ImageSize -> 400, 
  Frame -> False]

ImageAdd[sum, im2]

Ideally, I would like to mesh the graphics object and directly plot over the meshed region.

Any help would be greatly appreciated!

$\endgroup$
  • 2
    $\begingroup$ Have you seen Epilog? $\endgroup$ – J. M. will be back soon Feb 20 '16 at 4:34
  • 1
    $\begingroup$ Or Prolog? Or use Show instead of ImageAdd? $\endgroup$ – Michael E2 Feb 20 '16 at 4:42
  • $\begingroup$ I have tried these options.. $\endgroup$ – A_K_a Chaos Feb 20 '16 at 21:29
5
$\begingroup$

I may misunderstand the question but if you have a recent version of Mathematica this may help:

r1 = Polygon[{{0, 0}, {0, -12}, {5, -12}, {5, 0}, {15, 20}, {10, 20}}];

r2 = Polygon[{{0, 0}, {0, -12}, {-5, -12}, {-5, 0}, {-15, 20}, {-10, 20}}];

region = RegionUnion @@ DiscretizeGraphics /@ {r1, r2};

cp = ContourPlot[Sin[x y], {x, -5, 5}, {y, -5, 5}, ImageSize -> 400, Frame -> False];

RegionPlot[region, PlotStyle -> Texture[cp]]

enter image description here

If I didn't understand please try again to explain it for me.

$\endgroup$
  • $\begingroup$ The main problem I want to solve is the center of the contour plot Sin[x=0,y=0]=0 should coincide with the V-cleft of the Y-shape (which as you can see is Lower than than Sin[x=0,y=0]=0). That point is the {0,0} of my graphics object. Thanks for recommending region plot, I will try playing with that! $\endgroup$ – A_K_a Chaos Feb 20 '16 at 21:30
4
$\begingroup$

The polygons you gave seem a bit too large for the function you are interested in. For this solution, I work with a scaled down version, and also took the liberty of merging your two polygons into one.

A feature of ContourPlot[] in version 10 you might not be aware of is that it supports plotting over regions like polygons. We can thus do this:

poly = Polygon[{ScalingTransform[{1/3, 1/3}] /@ {5, -12}, {5, 0}, {15, 20},
                {10, 20}, {0, 0}, {-10, 20}, {-15, 20}, {-5, 0}, {-5, -12}}]];
ContourPlot[Sin[x y], {x, y} ∈ poly, PlotPoints -> 55]

hey Y, where's MCA?

$\endgroup$
  • $\begingroup$ Thanks for your response. I guess the automatic meshing over the surface causes numerical artifacts that make the plot look messy (?) $\endgroup$ – A_K_a Chaos Feb 21 '16 at 23:36
2
$\begingroup$

@J.M. 's answer was accurate. The regions are too large for mathematica to plot, because it automizes mesh sizes. But this can be controlled, albeit at the cost of computation time. My solution is to reduce the mesh size for more accurate plotting -

poly = Polygon[{{5, -12}, {5, 0}, {15, 20},
                {10, 20}, {0, 0}, {-10, 20}, {-15, 20}, {-5, 0}, {-5, -12}}];
newpoly=TriangulateMesh[BoundaryDiscretizeRegion[poly], MaxCellMeasure -> 10^(-2)];
ContourPlot[Sin[x y], {x, y} ∈ newpoly, ImageSize -> 400, Frame -> False]

Result: enter image description here

Thanks all for your help!

$\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.