Data Science: How To — Plot 3D Interactive Globe Map

  1. My Github
  2. For all my posts

How To

In this “How To” series I share part of the knowledge I learned, with the intention of strengthen the Data Science community and create free and useful content for everyone.

Libraries

  1. import requests
  2. import plotly.graph_objects as go

Objective

In this post we’ll use two API’s, one to get my internet provider IP and other to retrieve informations about the IP.

In this range of informations retrieved from the second API, there are longitude and latitude information, and starting from this information we’ll make a plot using the geo location of my internet provider.

Selecting an API

  1. First API: =api.ipify.org #returns my internet provider IP.
  2. Second API: =ip-api.com #returns informations about my internet provider.
#getting my public IP (from the internet provider)# returning json
my_ip = requests.get('https://api.ipify.org?format=json').json()
#assinging only the IP adress to te my_ip variable
my_ip = my_ip['ip']
my_ip

Now we have our internet provider IP.

With our internet provider IP, we can pass it to the second API, and it’ll return us the information we need:

#returning information of my ID
ip_json = requests.get('http://ip-api.com/json/' + my_ip).json()
ip_json

It will return something like this:

{'status': 'success',
'country': 'Brazil',
'countryCode': 'BR',
'region': 'MG',
'regionName': 'Minas Gerais',
'city': 'Jacutinga',
'zip': '37590',
'lat': -22.2974,
'lon': -46.6062,
'timezone': 'America/Sao_Paulo',
'isp': 'Hardonline Ltda',
'org': 'Hardonline Ltda',
'as': 'AS28229 HARDONLINE LTDA',
'query': '189.127.242.149'}

We only need lat and lon informations, so let’s assing them to new variables:

latitude = ip_json['lat']
longitude = ip_json['lon']

Plotting 3D Interactive Global Map with Plotly

#making the plot#if you are passing just one lat and lon, put it within "[]"
fig = go.Figure(go.Scattergeo(lat=[latitude], lon=[longitude]))
#editing the marker
fig.update_traces(marker_size=20, line=dict(color='Red'))
# this projection_type = 'orthographic is the projection which return 3d globe map'
fig.update_geos(projection_type="orthographic")
#layout, exporting html and showing the plot
fig.update_layout(width= 800, height=800, margin={"r":0,"t":0,"l":0,"b":0})
fig.write_html("3d_plot.html")
fig.show()

This plot will return it:

Image 1 — Plotly 3D Globe Map Plot

As I said, this map is interactive, but Medium is limited, if you want to see and explore the result, please follow this link.

After that click in download, if it doesn’t download, try clicking with the right button and click in “Save link as…”.

Hope I helped you to learn something new.

Igor Comune.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Igor Comune

Igor Comune

An "under construction" Data Scientist with a love for numbers and analysis!