1
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – user9876 Oct 21 '13 at 3:02
  • $\begingroup$ @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? $\endgroup$ – Mr.Wizard Oct 21 '13 at 3:06
  • $\begingroup$ 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. $\endgroup$ – user9876 Oct 21 '13 at 3:10
1
$\begingroup$

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]]
$\endgroup$
  • $\begingroup$ FWIW the OP already asked this as a new question, which was subsequently marked as a duplicate. $\endgroup$ – Mr.Wizard Oct 21 '13 at 15:38

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.