1
$\begingroup$

enter image description here

I wish to visualize a 3D layout of country data using the following code. How can the output be improved as currently it looks smeared to one side.

ListPlot3D[({CountryData[#1, "BirthRateFraction"],
CountryData[#1, "AnnualDeaths"], CountryData[#1, "GDP"]} & )/@CountryData["Countries"]]
$\endgroup$
  • $\begingroup$ You can try PlotRange -> All as a Lisplot3D option $\endgroup$ – Enrique Pérez Herrero Aug 21 '15 at 21:38
6
$\begingroup$

There seem to be a few problems, first, country data returns Quantities, so strip those off.

cDat = (QuantityMagnitude /@ {CountryData[#1, "BirthRateFraction"], 
     CountryData[#1, "AnnualDeaths"], CountryData[#1, "GDP"]} &) /@ 
 CountryData["Countries"];

Then you need to get rid of all the Missing[NotAvailable] entries.

cDat = Cases[cDat, {x_Real,y_Real,z_Real}:>{x,y,z}];

There also seem to be pretty large ranges in the last two elements, so I plotted those two on a log scale. I also think a ListPointPlot3D looks nicer with this data, but you can change that.

ListPointPlot3D[{#1,Log@#2,Log@#3}&@@@cDat,Filling->Axis]

Which gives:

enter image description here


EDIT

Color by country:

In order to color by country, you need to keep track of the country as well. There are almost certainly more robust ways of doing this, but since this request was in addition to the original question, I'll hack it into the answer as written.

Change how you define cDat

cDat = ({
      QuantityMagnitude@CountryData[#1, "BirthRateFraction"],
      QuantityMagnitude@CountryData[#1, "AnnualDeaths"],
      QuantityMagnitude@CountryData[#1, "GDP"],
      Last@#1
      } &) /@ CountryData["Countries"];

cDat = Cases[
   cDat, {x_Real, y_Real, z_Real, country_} :> {x, y, z, country}];

Then plot it, creating a list for the plot style with a red in place of the country you want to be red, which I've given in a list, so you can select more than one red country,

redCountries = {"Germany", "China"};
ListPointPlot3D[
 List /@ (
   {#1, Log@#2, Log@#3} & @@@ cDat
   )
 , Filling -> Axis
 ,PlotStyle -> (Last /@ cDat) /. {Alternatives @@ redCountries :> Red, _String :> Blue}
 ]

enter image description here

$\endgroup$
  • $\begingroup$ @Evans Looks good, only other request is how do you highlight (using a red code) the attributes of a single country like Germany? $\endgroup$ – thils Aug 21 '15 at 22:37

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.