# How do you make a 3D city population graphic?

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:

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


## 1 Answer

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}]


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.

• How would you go about drawing the shape of the country on the "ground" of the graphic? Dec 7, 2014 at 20:56
• @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] ... Dec 8, 2014 at 10:23