4
$\begingroup$

I know I can use

Ctrl = us population

to get an estimate for the current US population. I also know

== us pollution

gives me a few plots showing the US population over time. One of the plots is the following: enter image description here But how can I get the data used to make the above graphic?

$\endgroup$
  • 1
    $\begingroup$ You can use <kbd>Ctrl</kbd> + <kbd>=</kbd> + "us population time series" to get the Wolfram Language code used by Wolfram Alpha to generate the time series. $\endgroup$ – Carl Woll Aug 6 '17 at 15:36
4
$\begingroup$

You can easily reproduce the graphics with

DateListPlot[
 CountryData["UnitedStates", {"Population", All}]["Path"] /. Quantity[a_, _] :> a/10^6.,
 AspectRatio -> 1/4,
 GridLines -> Automatic,
 ImageSize -> Large,
 Filling -> Bottom]

enter image description here

The underlying data being

pop = CountryData["UnitedStates", {"Population", All}]["Path"]

All stands for "all available years". You can limit the years by replacing All with f.e. {1900, 2016}.

To see the last year available:

pop[[-1]] /. {a_, b_} :> {First@DateList@a, b}

enter image description here

$\endgroup$
  • $\begingroup$ Alternatively: DateListPlot[CountryData["UnitedStates", {"Population", All}], AspectRatio -> 1/4, Filling -> Bottom, GridLines -> Automatic, ScalingFunctions -> {# 1*^-6 &, # 1*^6 &}] $\endgroup$ – J. M. will be back soon Aug 7 '17 at 3:38
7
$\begingroup$

You may use the Knowledge Representation & Access guide.

Query the CountryData "Population" EntityProperty for its "Qualifiers" and "QualifierValues".

EntityValue["Country", "Population", "Qualifiers"]
{"Age", "CitizenshipStatus", "Date", "Gender", "HispanicOrigin",
 "MarginOfError", "Percent", "Race", "TwoOrMore", "UrbanRural"}

Notice that there is a "Date" qualifier. Valid "QualifierValues" can be returned with EntityValue["Country", "Population", "QualifierValues"].

Using this information we can query the "UnitedStates" "Country" entity using the CountryData function. The EntityValue function could also be used on a "Country" Entity.

ts = CountryData["UnitedStates", EntityProperty["Country", "Population", {"Date" -> All}]]

Mathematica graphics

This returns a TimeSeries object that appears to contain forecasts as well as historic data. With ts the dates can be returned with ts["Dates"] and the dates and values with ts["DatePath"].

ts["Dates"] // Short

Mathematica graphics

ts["DatePath"] // Short

Mathematica graphics

Thankfully, DateListPlot understands TimeSeries. With a rescaling to millions;

DateListPlot[ts/10^6, 
 AspectRatio -> 1/4, 
 GridLines -> Automatic, 
 Filling -> Bottom]

Mathematica graphics

Hope this helps.

$\endgroup$
  • $\begingroup$ CountryData["UnitedStates", EntityProperty["Country", "Population", {"Date" -> All}]] gives me (V10) only the last year (2014). Maybe you could clarify which parts of your solution are V11-specific? $\endgroup$ – eldo Aug 6 '17 at 15:45
  • 1
    $\begingroup$ @eldo I uninstalled my v10 versions a while ago so can't comment on that at the moment. However, it is entity framework which should reside on WRI servers so I am surprised that you get a different outcome in a different version. Calls must be version specific or perhaps v10 had a bug that has since been fixed. $\endgroup$ – Edmund Aug 6 '17 at 16:01
2
$\begingroup$

This can be done as follows.

Table[{j, CountryData["UnitedStates", {"Population", j}]}, {j, 1950,2014}]

{{1950,151325800people},{1951,154287000people},{1952,156954000people},{1953,159565000people},{1954,162391000people},{1955,165275000people},{1956,168221000people},{1957,171274000people},{1958,174141000people},{1959,177073000people},{1960,179323200people},{1961,183691000people},{1962,186538000people},{1963,189242000people},{1964,191889000people},{1965,194303000people},{1966,196560000people},{1967,198712000people},{1968,200706000people},{1969,202677000people},{1970,209463865people},{1971,211355529people},{1972,213250350people},{1973,215164616people},{1974,217113584people},{1975,219108358people},{1976,221161377people},{1977,223274855people},{1978,225436004people},{1979,227624232people},{1980,229825004people},{1981,232035086people},{1982,234261242people},{1983,236510688people},{1984,238794274people},{1985,241119752people},{1986,243495805people},{1987,245920994people},{1988,248381577people},{1989,250857751people},{1990,253339097people},{1991,255807342people},{1992,258275919people},{1993,260803255people},{1994,263468979people},{1995,266323717people},{1996,269393632people},{1997,272643340people},{1998,275986072people},{1999,279300030people},{2000,282496310people},{2001,285544778people},{2002,288467308people},{2003,291290823people},{2004,294063120people},{2005,296820296people},{2006,299564470people},{2007,302284564people},{2008,304989064people},{2009,307686729people},{2010,310383948people},{2011,313232044people},{2012,316265537people},{2013,319330342people},{2014,322422965people}}

$\endgroup$
  • 1
    $\begingroup$ CountryData["UnitedStates", {"Population", {1950, 2015}}] will yield a TimeSeries[] that can be easily fed into DateListPlot[]. CountryData["UnitedStates", {"Population", {1950, 2015}}] @ "DatePath" will return a conventional list instead of a TimeSeries[]. $\endgroup$ – J. M. will be back soon Aug 6 '17 at 13:05
  • $\begingroup$ @J.M: That was answered what was asked. $\endgroup$ – user64494 Aug 6 '17 at 13:10
  • $\begingroup$ Using version 10.0 on a Mac, CountryData[...] returns a TimeSeries, but the TimeSeries doesn't know what to do with @"DatePath". $\endgroup$ – Ted Ersek Aug 6 '17 at 21:26
1
$\begingroup$

In the output of WolframAlpha click on the plus sign in the pod with the plot and select Computable data

data = {#[[1, 1]], 
     QuantityMagnitude[#[[2]]]} & /@ (WolframAlpha[
      "US population", {{"LongTermHistory:Population:CountryData", 1}, 
       "ComputableData"}, 
      PodStates -> {"LongTermHistory:Population:CountryData__Show \
projections", "LongTermHistory:Population:CountryData__Linear scale"}][[1]]);

ListLinePlot[data]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ How do you figure out what string arguments to use in that code? $\endgroup$ – Ted Ersek Aug 6 '17 at 21:18
  • $\begingroup$ It is pasted into the notebook when you select Computable data from the pod. $\endgroup$ – Bob Hanlon Aug 6 '17 at 21:24

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.