6
$\begingroup$

What's the code to list all the world's flags?

CountryData["G8", "Flag"]

works to get those particular flags, but what about all the world's flags?

CountryData["World", "Flag"]

(* Missing["NotApplicable"] *)

This works, but seems contrived; is there a shortcut?

CountryData[#, "Flag"] & /@ CountryData[]

More importantly, is it possible to insert the flags as ChartLabels in, say, BarChart?

EDIT:

Following up on Mr. Wizard's solution, I find that some flags, when inserted as ChartLabels, are shifted down:

chart with flag labels

The rule used to create the flags is:

# -> Image[CountryData[#, "Flag"], ImageSize -> {15, 20}] & /@ 
  CountryData[];

which is necessary in this application since the data is associated to country code.

The code to generate the bar chart is somewhat lengthy and irrelevant, but essentially, it just calls ChartLabels to insert the images.

Any idea why Philippines and United Kingdom are translated downwards?

A related question is how to rotate the flags $90^\circ$ to render the BarChart on its side.

$\endgroup$
8
$\begingroup$

For the first try:

CountryData["Countries", "Flag"]

For the second it appears to work directly:

Mathematica graphics

$\endgroup$
  • $\begingroup$ How to control the size? For example in BarChart, if inserted directly, the flags look gigantic and overlap $\endgroup$ – alancalvitti Sep 23 '12 at 7:16
  • $\begingroup$ @alancalvitti either resize them with the mouse (drag the orange frame handle) or apply an Image[#, ImageSize -> n] & operation or similar. Perhaps I don't understand? $\endgroup$ – Mr.Wizard Sep 23 '12 at 7:18
  • $\begingroup$ Thanks that just about works. The issues are: flag aspect ratios differ, so it appears that specifying a constant ImageSize (or even both dimensions, ie, ImageSize->{x,y}) results in a ragged array ... Also, some flags, like Philippines and UnitedKingdom are somehow translated down by almost half the vertical spacing. $\endgroup$ – alancalvitti Sep 23 '12 at 7:25
  • $\begingroup$ @alancalvitti if you include specific examples of that in your question I shall try to address them. $\endgroup$ – Mr.Wizard Sep 23 '12 at 7:26
  • $\begingroup$ @alancalvitti I'm not seeing the shifted flags, and I need to sleep. Hopefully someone has a simple solution for you. It might help to include complete code to produce your chart. $\endgroup$ – Mr.Wizard Sep 23 '12 at 8:00
4
$\begingroup$

Re your last two questions:

I can't reproduce the flags misalignment. All flags are shown OK in my machine

enter image description here

The other question is how to rotate the BarChart and flags.

im = Image[CountryData[#, "Flag"], ImageSize -> {15, 20}] & /@ CountryData[];

BarChart[Range[5], BarOrigin -> Left, 
 ChartLabels -> (Image[#,  ImageSize -> {15, 20}] & /@ (ImageRotate /@ (im[[180 ;; 185]])))]

Mathematica graphics

$\endgroup$
3
$\begingroup$

GoSquared has made available a 2400 flag icon set

enter image description here

https://www.gosquared.com/resources/2400-flags

Here is how you can quickely grab the 64px versions:

flags = Import["http://www.gosquared.com/download/pixels/flags.zip", 
   "flags/64/*.png"];
countries = 
 StringDrop[#, 9] & /@ (Rest@
    DeleteDuplicates[
     StringCases[#, "flags/64/" ~~ __] & /@ 
      Import["http://www.gosquared.com/download/pixels/flags.zip", 
       "FileNames"]])
CreateDirectory[
 FileNameJoin[{$UserDocumentsDirectory, "Flags64pxPNG"}]]
MapThread[Export, {FileNameJoin[{$UserDocumentsDirectory, 
      "Flags64pxPNG", #[[1]]}] & /@ countries, flags}]
$\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.