Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is it possible to use a background image in a polygon without rasterizing the polygon?

I tried with textures, but they seem to be viable only for simple polygons.

I want to put a flag as the background of the polygon that represents a country shape. I can do this for one country by rasterizing the polygon:

    Graphics[CountryData["Monaco", "Polygon"]], 
    CountryData["Monaco", "Flag"]


But by rasterizing the polygon, I make it difficult to apply this procedure to several neigboring countries and assemble them together.

I would like to put each country flag in the background of, for example:

     CountryData[#, "Polygon"] & /@ {"Venezuela", "Colombia"}}]  

colombia and venezuela


After seeing cormullion answer, I was able to do what I wanted:

  {{Texture[ImageReflect[CountryData[#, "Flag"], Top -> Right]], 
    CountryData[#, "Polygon"] /. 
      {Polygon[a_] :> Polygon[First[a], VertexTextureCoordinates -> 
       Transpose[Rescale /@ Transpose[First[CountryData[#, "Coordinates"]]]]]}} & /@ 


The key part is the use of Rescale[]. Thank you!

share|improve this question
I wish Mathematica had more advanced vector-graphics and layering; this is so simple in a drawing program. A good solution would preserve everything as vector with no rasterized textures, but I don't think it can be done in a general way. -- edit I didn't notice that the Flag data is already raster. I guess Texture makes the most sense in that case. –  Mr.Wizard Feb 4 '12 at 23:45
You may find ImagePad useful if you want small flags inside country borders –  Szabolcs Feb 5 '12 at 1:00
One caveat: This texture-based approach leads to a graphic that cannot be exported to PDF (on Macs, at least). This seems worth mentioning since you specifically asked for a method preserving the vector character of the borders. Just in case anyone encounters this, a workaround for this problem is to export to svg first, and then use Inkscape to convert to PDF. –  Jens Feb 5 '12 at 19:24
add comment

2 Answers

up vote 10 down vote accepted

This example is from the documentation for Texture in Mathematica version 8:

With[{vc = 
  Rescale /@ Transpose[First[CountryData[#, "Coordinates"]]]]}, 
Show[CountryData[#, "Shape"], 
 ImageSize -> {{100}, {100}}] /. {RGBColor[__] :> 
  Texture[ImageReflect[Image[CountryData[#, "Flag"]], 
    Top -> Right]], 
 Polygon[a_] :> 
  Polygon[First[a], VertexTextureCoordinates -> vc]}] & /@ 

but I don't know whether it does what you need. Still, it looks pretty.


share|improve this answer
add comment


Graphics[{EdgeForm[White], {{Texture[Image[CountryData[#, "Flag"]]], 
  Polygon[First@First[CountryData[#, "Polygon"]], 
   VertexTextureCoordinates ->Transpose[
     Rescale /@ Transpose[First@First[CountryData[#, 
         "Polygon"]]]]]}} & /@ {"Venezuela", "Colombia"}}]

which gives

maps and flags

share|improve this answer
Note that your country coordinates are somewhat rotated (transposed). –  Artes Feb 5 '12 at 0:33
Thank you @Artes. I need to change Coordinates to Polygon in the first argument of Polygon. –  kguler Feb 5 '12 at 0:36
Now, country polygons are correctly oriented while flags are rotated. –  Artes Feb 5 '12 at 0:50
@Artes, thank you again. Just noticed, that OP alrady put together the solution more than an hour ago. –  kguler Feb 5 '12 at 1:46
add comment

Your Answer


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.