Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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 attempting to plot the most populous cities on a world map.

Currently, I have the line of code:

numberofpeople = 
  Map[{#, Length[CityData[#, "Population"]]} &, CityData[All]];

which should give me the City name, and the population. However, when I type in the line of code

Reverse[SortBy[numberofpeople, Last]][[1 ;; 20]]

I am getting odd data, which is that the most populous cities have 1 person.

Could somebody debug my code, and answer any future questions I have on the matter? Much appreciated.

EDIT: How do I plot these points on a World Map? Currently I have

Graphics[{EdgeForm[Black], , CountryData[#, "SchematicPolygon"]} & /@ CountryData[]]

but I have no idea what goes between the points.

share|improve this question
up vote 2 down vote accepted

Leave out Length in Length[CityData[#, "Population"]], and delete cases of Missing data:

numberofpeople = Map[{#, CityData[#, "Population"]} &, CityData[All]];

Reverse[SortBy[DeleteCases[numberofpeople, {_, _Missing}], Last]] ~Take~ 20 // Grid

enter image description here

share|improve this answer
Thank you very much for your help. I have one more question: how do I plot these points on a world map? My edit should have the information required for assistance. – user9876 Oct 21 '13 at 3:02
@user9876 You're welcome. Rather than extending your question please consider posting a new one; also, I don't understand what you want to do with the "world map" -- have you seen this question? – Mr.Wizard Oct 21 '13 at 3:06
I figured that since my new question is related to this one, I would just have an add-on to this one, instead of filling up the site with another question. I will make a new question regarding this topic. – user9876 Oct 21 '13 at 3:10

I was gonna post this as a comment but it'll be too long. As Mr. Wizard has shown you how to obtain the most popluar cities, here is how to plot them:

 Graphics[{{White, EdgeForm[Black],  CountryData[#, "FullPolygon"] & /@ CountryData[]}, 
{PointSize[Large], Red,    Tooltip[Point[Reverse[CityData[#, "Coordinates"]]], 
          CityData[#, "Name"]] & /@ largeCities}}]

Mathematica graphics

Here, largeCities are the names of the cities without their populations. So you can do something like

largeCities = Take[SortBy[numberofpeople, Last] // Reverse, 20][[All, 1]]
share|improve this answer
FWIW the OP already asked this as a new question, which was subsequently marked as a duplicate. – Mr.Wizard Oct 21 '13 at 15:38

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.