Create graphic object using {x,y} points, and plot a contour over it

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]



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

Any help would be greatly appreciated!

• Have you seen Epilog? Commented Feb 20, 2016 at 4:34
• Or Prolog? Or use Show instead of ImageAdd? Commented Feb 20, 2016 at 4:42
• I have tried these options.. Commented Feb 20, 2016 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]]


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

• 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! Commented Feb 20, 2016 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]


• Thanks for your response. I guess the automatic meshing over the surface causes numerical artifacts that make the plot look messy (?) Commented Feb 21, 2016 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: