I want to make a graphic that has the shape of a country, finds all the cities of that country, gets their coordinates and populations and turns that into a SmoothHistogram3D with the population being the z value.

I tried this, but apparently that's not how it works:

  CityData[#, "Coordinates"] & /@ CityData[{All, "Mexico"}], 
 DeleteCases[CityData[#, "Population"] & /@ CityData[{All, "Mexico"}],

1 Answer 1


Something like this is probably what you're after:

dat = DeleteCases[({CityData[#, "Population"], 
    CityData[#, "Coordinates"]} & /@ 
    CityData[{All, "Mexico"}]), {___, _Missing, ___}];
func = PDF[SmoothKernelDistribution[
    WeightedData[dat[[All, 2]], dat[[All, 1]]]], {x, y}];
ra = func[[2, 0, 1]];
Plot3D[Log10@func, {x, ra[[1, 1]], ra[[1, 2]]}, {y, ra[[2, 1]], 
    ra[[2, 2]]}, PlotRange -> {-5, All}]

enter image description here

Note that you were deleting the Missing cases for the Population and Coordinates separately, but these two lists of data do not correspond to the same lists of cities, you can see this by checking that their lengths are different. The expression above only includes cities that have both datum.

I took the log of the function to get better depth contrast, otherwise the image is dominated by huge spikes from the very largest cities.

  • 1
    $\begingroup$ How would you go about drawing the shape of the country on the "ground" of the graphic? $\endgroup$
    – Ivo3185
    Dec 7, 2014 at 20:56
  • 1
    $\begingroup$ @Guillochon: have you tested that code with version 10? It looks like you need to replace Quantities for it to work in version 10, e.g. add a line dat =dat /. q_Quantity :> QuantityMagnitude[q] ... $\endgroup$ Dec 8, 2014 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.