Message Boards Message Boards

4
|
6692 Views
|
0 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Chicago Loop: Godzilla of Energy Consumption

Posted 10 years ago
The Wolfram Language is a good platform to work with data analysis and visualization. Here is an example about using Mathematica to get JSON data and visualize energy distribution among the Chicago region.

The public data source is from data.cityofchicago.org. We can use the powerful build-in data functions to plot the energy consumption as a heat map. 
In the new Wolfram Language, Mathematica can deal with JSON type easily
data = Import[
   "https://data.cityofchicago.org/resource/usfg-z9mx.json", "JSON"];
Get the names of the region or community area: 
In[3]:= area=("community_area_name"/.#&)/@data;
In[4]:= area[[1;;5]]
Out[4]= {Albany Park,Archer Heights,Armour Square,Ashburn,Auburn Gresham}
Convert the power from string form to numerics
power = ToExpression /@ (("avg_kwh_total_sqft" /. # &) /@ data);
Get the geographic polygon from the chosen location: 
GeoGraphics[{GeoStyle["OutlineMap"], LightGray, EdgeForm[Thin],
  Polygon[Entity["Neighborhood",
    "AlbanyPark::Chicago::Illinois::UnitedStates"]]}]
and it should look like the one from a Wolfram Alpha query

In most cases we can convert the name of the community into the entity form directly: 
strData = (StringReplace[ToString[#], " " -> ""] <> "::Chicago::Illinois::UnitedStates") & /@ area;
But there are several exceptions: 
 Position[check, x_ /; Head[x] == Missing] // Flatten
 (*{3, 9, 16, 43, 56, 65, 68, 75}*)
 
 strData[[{3, 9, 16, 43, 56, 65, 68, 75}]] = (#[[2]] & /@ {
      Entity["Neighborhood", "ArmourSquareChinatown::Chicago::Illinois::UnitedStates"],
      Entity["Neighborhood", "Cragin::Chicago::Illinois::UnitedStates"],
      Entity["Neighborhood", "ChicagoLawnMarquettePark::Chicago::Illinois::UnitedStates"], {None, None},
      Entity["Neighborhood", "OHare::Chicago::Illinois::UnitedStates"],
      Entity["Neighborhood", "LittleVillageSouthLawndale::Chicago::Illinois::UnitedStates"],
     Entity["Neighborhood", "WashingtonHeightsBrainerd::Chicago::Illinois::UnitedStates"],{None, None}});
(* Use ctrl+= to call the Entity parameters from Wolfram Alpha *)
(* Another exception is Hermosa community, cannot be visualized here somehow*)
Once you have converted all region names into the entity form, we can visualize the energy consumption on the map:
 {pMin, pMax} = {Min[power], Max[power]};
 colorList = ColorData["TemperatureMap"][Sqrt[(# - pMin)/(pMax - pMin)]] & /@ power;
 GeoGraphics[
     Table[
         Tooltip[
             {GeoStyle["OutlineMap"], colorList[[i]], EdgeForm[Thin], Polygon[Entity["Neighborhood", strData[[i]]]]},
              ToString[power[[i]]/1000.] <> " MWH"
         ],
     {i, DeleteCases[Range[77], x_ /; MemberQ[{32, 43, 51, 75}, x]]}
   ]
]

Other visualization tools available: 


 n = 1;
 Default[subTotal] = 1;
 subTotal[l_List, n_.] := Total[Sort[l][[;; -n]]]
 areaPer =
   MapThread[(# <> " (" <>
       ToString[NumberForm[#2/subTotal[power]*100, 3]] <>
       "%)") &, {area, power}];
 consumptionList =
   Reverse[SortBy[Transpose[{areaPer , power}], #[[2]] &]];
legendsTop10 = consumptionList[[n ;; 9 + n, 1]];

Wolfram language also provides strong List manipulation function so we can apply to the same data and get more detailed representation. Notices the Chicago Loop, the Near North Side and the Near South Side really squeeze the room handling other data spot, we can remove them and have a detailed look on the others. Lets start from the community with the 4th most power consumption with the Span function.
n = 4;
The Pie chart uses the same code above with this new n: 


The sorted barchart:
 subtotal = ToString[Total[consumptionList[[n ;;, 2]]]/1000.] <> " Million Watt Hour";
 BarChart[
  consumptionList[[n ;;, 2]],
  ChartLabels ->
   Placed[(Rotate[#, 90 Degree] & /@ (consumptionList[[n ;;, 1]])),
    Above], AxesLabel -> {"", Style["KWH", 17, Italic]},
  Epilog ->
   Inset[Framed[Style["SubTotal: " <> total, 20],
     Background -> LightYellow], {40, 65000}]]
POSTED BY: Shenghui Yang
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