Map Trove newspaper results by state

Version 2 of the Trove API adds the state facet to the newspapers zone. This means we can easily get the number of articles from a search query published in each state.

If you haven't used one of these notebooks before, they're basically web pages in which you can write, edit, and run live code. They're meant to encourage experimentation, so don't feel nervous. Just try running a few cells and see what happens!.

Some tips:

  • Code cells have boxes around them.
  • To run a code cell click on the cell and then hit Shift+Enter. The Shift+Enter combo will also move you to the next cell, so it's a quick way to work through the notebook.
  • While a cell is running a * appears in the square brackets next to the cell. Once the cell has finished running the asterix will be replaced with a number.
  • In most cases you'll want to start from the top of notebook and work your way down running each cell in turn. Later cells might depend on the results of earlier ones.
  • To edit a code cell, just click on it and type stuff. Remember to run the cell once you've finished editing.

Setting things up

First we'll import the packages we need.

In [18]:
# Import the libraries we need
# <-- Click the run icon
import json
import os

import altair as alt
import pandas as pd
import requests
In [19]:
# Load variables from the .env file if it exists
# Use %%capture to suppress messages
%load_ext dotenv

You need an API key to get data from Trove. Insert your key below.

In [20]:
# Insert your Trove API key

# Use api key value from environment variables if it is available
if os.getenv("TROVE_API_KEY"):
    API_KEY = os.getenv("TROVE_API_KEY")

Set up some default parameters for our API query.

In [21]:
# Set up default parameters for our API query
# <-- Click the run icon
params = {
    "zone": "newspaper",
    "encoding": "json",
    "facet": "state",
    "n": "1",
    "key": API_KEY,

API_URL = ""

This is where you set your search keywords. Change 'weather' in the cell below to anything you might enter in the Trove simple search box. For example:

params['q'] = 'weather AND wragge'

params['q'] = '"Clement Wragge"'

params['q'] = 'text:"White Australia Policy"'

params['q'] = 'weather AND date:[1890-01-01T00:00:00Z TO 1920-12-11T00:00:00Z]'

You can also limit the results to specific categories. To only search for articles, include this line:

params['l-category'] = 'Article'

In [22]:
# Enter your search parameters
# This can be anything you'd enter in the Trove simple search box
params["q"] = "radio"

# Remove the "#" symbol from the line below to limit the results to the article category
params["l-category"] = "Article"

Get the data from Trove

Everything's set up, so just run the cells!

Make an API request

In [23]:
# <-- Click the run icon
response = requests.get(API_URL, params=params)
data = response.json()

Reformat the results

In [24]:
# <-- Click the run icon
def format_facets(data):
    facets = data["response"]["zone"][0]["facets"]["facet"]["term"]
    df = pd.DataFrame(facets)
    df = df[["display", "count"]]
    df.columns = ["state", "total"]
    df["total"] = pd.to_numeric(df["total"], errors="coerce")
    df = df.replace("ACT", "Australian Capital Territory")
    df = df[(df["state"] != "National") & (df["state"] != "International")]
    return df

df = format_facets(data)
state total
0 New South Wales 547572
1 Queensland 333583
2 Western Australia 206961
3 Victoria 161047
4 South Australia 132064
5 Tasmania 91574
6 Australian Capital Territory 80686
9 Northern Territory 5908

Make some charts!

Just run the cells!

In [25]:
# Create a bar chart
# <-- Click the run icon
chart = (
        x=alt.X("total", axis=alt.Axis(title="Total articles")),
        y=alt.Y("state", axis=alt.Axis(title="")),
        tooltip=[alt.Tooltip("total", title="Total articles")],
    .properties(width=300, height=200)
In [26]:
# Make a chloropleth map
# <-- Click the run icon
with open("data/aus_state.geojson", "r") as geo_file:
    geo_data = json.load(geo_file)
map = (
    .mark_geoshape(stroke="black", strokeWidth=0.2)
            legend=alt.Legend(title="Total articles"),
        lookup="properties.STATE_NAME", from_=alt.LookupData(df, "state", ["total"])
    .properties(width=400, height=400)
In [27]:
# Display the charts side by side
# <-- Click the run icon
alt.hconcat(map, chart).resolve_legend(color="independent")