Message Boards Message Boards

0
|
2603 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

BarChart using EntityList?

Posted 2 years ago

Trying to make a barchart, but not sure what I am doing wrong. I attached my code and the example,

Please advise.

POSTED BY: Jon Locke
2 Replies
Posted 2 years ago

Hi Jon,

It seems your main problem is with getting the data. Here is one way

greenhouseGases = EntityClass["Chemical", "GreenhouseGases"] // EntityList
euCountries = EntityClass["Country", "EU"] // EntityList

Helper function to get emissions for a country

emissionsFor[country_, gases_] := 
 AssociationMap[
   country[EntityProperty["Country", 
      "GreenhouseGasEmissions", {"GreenhouseGas" -> CanonicalName@#}]] &, gases] // 
 DeleteMissing

e.g. for Austria

emissionsFor[Entity["Country", "Austria"], greenhouseGases]

(* 
 <|Entity["Chemical", "Methane"] -> 
  Quantity[7.05709*10^6, IndependentUnit["metric tons of carbon dioxide equivalent"]/("Years")], 
  Entity["Chemical", "CarbonDioxide"] -> 
  Quantity[7.96504*10^7, IndependentUnit["metric tons of carbon dioxide equivalent"]/("Years")], 
  Entity["Chemical", "NitrousOxide"] -> 
  Quantity[5.25581*10^6, IndependentUnit["metric tons of carbon dioxide equivalent"]/("Years")]|>
*)

Emissions for all countries

AssociationMap[emissionsFor[#, greenhouseGases] &, euCountries]

You should be able to generate the BarChart from this data.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

You asked a related question yesterday but it was deleted before I could answer.

greenhouseGases = EntityClass["Chemical", "GreenhouseGases"] // EntityList
countries = {Entity["Country", "France"],
  Entity["Country", "Germany"],
  Entity["Country", "Italy"],
  Entity["Country", "Poland"],
  Entity["Country", "Spain"],
  Entity["Country", "Sweden"]}

emissionsFor[country_, gases_] := AssociationMap[
   country[EntityProperty["Country", "GreenhouseGasEmissions",
      {"GreenhouseGas" -> CanonicalName@#}]] &, gases] // 
  DeleteMissing

data = AssociationMap[emissionsFor[#, greenhouseGases] &, countries]

data // BarChart[#,
   ChartLabels -> {CanonicalName@Keys@#, None},
   ChartLegends -> CanonicalName@DeleteDuplicates@Keys@Values@#,
   PlotTheme -> "Detailed",
   FrameLabel -> {"Country", "Metric tons of carbon dioxide equivalent / Year"},
   ImageSize -> 500] &

enter image description here

POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract