Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
2  
Have you seen Epilog? – J. M. Feb 20 at 4:34
1  
Or Prolog? Or use Show instead of ImageAdd? – Michael E2 Feb 20 at 4:42
    
I have tried these options.. – A_K_a Chaos Feb 20 at 21:29

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.

share|improve this answer
    
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! – A_K_a Chaos Feb 20 at 21:30

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?

share|improve this answer
    
Thanks for your response. I guess the automatic meshing over the surface causes numerical artifacts that make the plot look messy (?) – A_K_a Chaos Feb 21 at 23:36

@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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.