Skip to main content

Plotting data using Jupyter Notebooks

In this tutorial, we will go through the steps necessary to create a Jupyter Notebook, and use it to plot data fetched using the Tracking History API.

Source code#

You can find the source code of the following tutorial here.

If you wish to see the final result of the tutorial, please head here.

Jupyter Notebook installation#

Using Python#

Let start by creating a new Jupyter Notebook. Here assume that you have Python installed on your machine. Let's start with upgrading pip:

pip3 install --upgrade pip

And then:

pip3 install jupyter

Using Anaconda#

info

For less experienced users, using Anaconda will be a simpler solution.

Anaconda will install both Python and Jupyter Notebook all at once. To install Anaconda, simply head here and download the program for your OS.

Creating your Notebook#

Running Jupyter Notebook#

Now that everything is installed, you can now simply run:

jupyter notebook

Your browser will open up with the Jupyter Notebook interface, that looks like this:

Empty jupyter notebook
info

You should run that command in a folder where you wish your Notebooks to be created.

Now that your Jupyter interface is up and running you can simply create a new Notebook by clicking New and then Python 3. You are now ready!

Fetching data#

First, we need to install a few dependencies for our Notebook to work properly. We will start by installing requests, the package that will allow us to query the Tracking History API.

pip install requests

In our Notebook, we can add a new Cell and add the following:

cell_1.py
import requests
response = requests.get(
"https://api.airsafe.spire.com/v2/targets/history",
params={
"start": "2021-03-20T12:00:00Z",
"end": "2021-03-20T14:30:00Z",
"latitude_between": "31.02988460440661,31.36374539559339",
"longitude_between": "121.1632295670742,121.52071043292581",
},
# Replace your token here
headers={"Authorization": f"Bearer <your_api_token>"},
)
print(response)

Here, we are simply calling the API with the query parameters to get data around Shanghai (SHA), and printing the response we received.

If everything is set up correctly, you should see <Response [200]> appear as a result after running your Notebook.

Introduction to Pandas#

Pandas is a very useful Python data manipulation library that will save us some time when filtering, organizing and plotting our data after fetching it from the API.

Let's install it so we can use it in our Notebook.

pip install pandas

Now, we can update our Cell and parse the response from the query, and create a new Panda Dataframe with the results:

cell_1.py
import requests
import pandas as pd
import json
response = requests.get(
"https://api.airsafe.spire.com/v2/targets/history",
params={
"start": "2021-03-20T12:00:00Z",
"end": "2021-03-20T14:30:00Z",
"latitude_between": "31.02988460440661,31.36374539559339",
"longitude_between": "121.1632295670742,121.52071043292581",
},
headers={"Authorization": f"Bearer <your_api_token>"},
)
data = []
for line in response.iter_lines(decode_unicode=True):
if line and '"target":{' in line:
data.append(json.loads(line)["target"])
df = pd.DataFrame(data)

Introduction to Plotly#

For this example, we are gonna use Plotly to plot the retrieved data points. However, you could also make this work with a number of other libraries, such as Matplotlib for example.

pip install plotly

Now, we can update our Cell to plot our results on a map! Let's go over the changes:

cell_1.py
import requests
import plotly.express as px
import pandas as pd
import json
pd.options.plotting.backend = "plotly"
response = requests.get(
"https://api.airsafe.spire.com/v2/targets/history",
params={
"start": "2021-03-20T12:00:00Z",
"end": "2021-03-20T14:30:00Z",
"latitude_between": "31.02988460440661,31.36374539559339",
"longitude_between": "121.1632295670742,121.52071043292581",
},
headers={"Authorization": f"Bearer <your_api_token>"},
)
data = []
for line in response.iter_lines(decode_unicode=True):
if line and '"target":{' in line:
data.append(json.loads(line)["target"])
df = pd.DataFrame(data)
fig = px.scatter_mapbox(df, lat='latitude', lon='longitude', hover_data=['icao_address',"altitude_baro", "on_ground"], zoom=9,
mapbox_style="carto-positron", color="icao_address")
fig.show()

Here is a breakdown of the modifications we did here:

  • We modified the default plotting library by modifying the default Plotly configuration.
  • We created a Mapbox instance and filled in the parameters by defining the latitude and longitude fields name, as well as some other details such as the data appearing when hovering on a point.
  • We display that instance

It should now look something like that:

Result of jupyter notebook