# 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? Feb 20 '16 at 4:34
• Or Prolog? Or use Show instead of ImageAdd? Feb 20 '16 at 4:42
• I have tried these options.. Feb 20 '16 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! Feb 20 '16 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 (?) Feb 21 '16 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]
{"ft_lang_label":"__label__en","ft_lang_prob":0.7764243,"math_prob":0.9384653,"size":768,"snap":"2021-43-2021-49","text_gpt3_token_len":267,"char_repetition_ratio":0.12565444,"word_repetition_ratio":0.031007752,"special_character_ratio":0.4153646,"punctuation_ratio":0.25698325,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9939388,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,3,null,3,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-08T01:11:32Z\",\"WARC-Record-ID\":\"<urn:uuid:dbabe898-3b08-42f5-b5c8-7952028dfa69>\",\"Content-Length\":\"162269\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:1d8613ab-785c-44df-89b8-be9978440263>\",\"WARC-Concurrent-To\":\"<urn:uuid:35bc1ee3-675d-497b-aff2-8c343cb2f264>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://mathematica.stackexchange.com/questions/107836/create-graphic-object-using-x-y-points-and-plot-a-contour-over-it\",\"WARC-Payload-Digest\":\"sha1:TVJIBODF47SNMZDOCLONGNFI2KGADLHJ\",\"WARC-Block-Digest\":\"sha1:Y4SBJYFCE43A4JR5TNIWJPUTELADDEYV\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964363420.81_warc_CC-MAIN-20211207232140-20211208022140-00592.warc.gz\"}"}