Create Interactive Globe + Earthquake Plot in Python with Plotly

Here we create an interactive Globe like Google Earth using the topography data with an amazing visualization tool, Plotly.

Ryota Kiuchi, Ph.D.
Towards Data Science

--

How Does It Look?

Image by Author

Enjoy in here!

What is your “take-home message”?

Through creating this interactive plot, you can get the following ideas.
- Deeper insights and more realistic application example to use Plotly
- Especially, handle with the spherical plot in which Matplotlib poorly handle with
- Get familiar with the global data characterized by latitude and longitude through the topography and earthquake data
- The most important point is that visualizing earthquake distribution in this post makes you imagine where most of the earthquakes occurred (which is along the plate boundary).

Before we get started

We need to install Plotly via pip:

pip install plotly

Or if you want to install via Anaconda:

conda install plotly

My Environment

- macOS 10.14 Mojave
- Python 3.6 (Anaconda)
- Jupyter notebook

Two main contents in this post

1. Create the Interactive Globe
2. Plot the Global Earthquake Distribution on the Interactive Globe

1. Create the Interactive Globe

3 steps for achievement

1–1. Download and import the topography data
1–2. Convert geographical coordinates (latitude/longitude) into spherical coordinates to plot on the sphere.
(Plotly Chart Studio: Heatmap plot on a spherical map)
1–3. Create the map using Plotly

1–1. Download and import the topography data

Download the Topography Data

The 1 arc-minute global relief model of Earth’s surface (ETOPO1 Global Relief Model) is published by NOAA (National Oceanic and Atmospheric Administration).
Download the ETOPO1 data from here.
* In here, we use the “ETOPO1_Ice_g_gdal.grd” file which is a grid-registered netCDF file of ETOPO1 Ice Surface.

Image by NOAA: NOAA ETOPO1 Global Relief Model

Import the Topography Data

Firstly, importing the values of latitude, longitude, and topography from the downloaded NetCDF file using the Etopo function.

Input: longitude (lon_area) and latitude (lat_area) based on the selected area, and resolution.
Output: mesh shape longitude (lon), latitude (lat) within the selected area, and topography (topo) based on resolution.
* resolution has to be in degree unit and the highest resolution will be 0.0167 degree

1–2. Converting to the spherical coordinates

To make the spherical plot using the imported longitude (lon) and latitude (lat) data, we convert these data into the spherical coordinates. The idea is not complicated. Just converting from the orthogonal coordinate to the spherical coordinate using the basic math functional form.

How to make it in Python? See details described at
Plotly Chart Studio: Heatmap plot on a spherical map

1–3. Plot the Interactive Globe using Plotly

Here we plot the interactive Globe using the data of latitude, longitude described as the spherical coordinated, and topography.

Firstly, we call the Etopo function defined in Step. 1 to read the array of longitude (lon_topo), latitude (lat_topo), and topography (topo) covering the whole globe. If the resolution is too high, the size of data becomes huge due to the 3-dimensional data. Here we adopt 0.8 degrees as the value of resolution.

And then, converting to the spherical coordinates using the created function of mapping_map_to_sphere

We also need to define the color scale of the topography we plot. The color scale in this example is defined as follows:

We prepare the plot using Plotly as follows:

To make our plot more brilliant, the axes are removed as follows:

Figure title (‘3D spherical topography map’), font (‘Courier New’), and its associated settings are described here.

Note that, black and white are selected as the background and font color, respectively, to make our globe being in the universe:

Finally, we create the plot and export as an HTML file.
Once you create the HTML file, you can check your plot using the Web browser (Google Chrome, Safari, …) in the local environment.

Image by Author

Demo

In the above plot, the topography data was displayed as the color without any three-dimensional shapes. Here we create those shapes by converting xs, ys, and zs. The following code describes how to create and make the figure just like what we did in the previous plot:

Image by Author

Demo

2. Plot the Global Earthquake Distribution on the Interactive Globe

3 steps for achievement

2–1. Prepare the earthquake data
2–2. Prepare plate boundary plot and convert coordinates
2–3. Plot the earthquake distribution on the interactive globe created above

2–1. Prepare the earthquake data

Download the earthquake data

In this post, we download the earthquake data from the U.S. Geological Survey (USGS) website. This earthquake catalog data includes the dates, location parameters (longitude, latitude, and depth), magnitudes that describe the size of the earthquake, and so on. You can select the earthquake by filtering these parameters. I would recommend downloading only for larger magnitude events (e.g. M > 5) due to the huge numbers of earthquakes you have to handle (For a given frequency of magnitude 4.0 or larger events there will be 10 times as many magnitude 3.0 or larger quakes and 100 times as many magnitude 2.0 or larger quakes ! — Gutenberg-Richter law)

  • In this post, I downloaded the catalog based on the following options:
  1. M > 5
  2. Date between 2000/1/1 to 2020/8/31
  3. Worldwide region
Image by USGS: USGS Search Earthquake Catalog

Import the earthquake data

Here we import the earthquake data downloaded above using pandas. And then create NumPy arrays for five parameters we will use (date, longitude, latitude, depth, magnitude). Three location parameters (longitude, latitude, and depth) are used as the x, y, and z, and magnitude is used for the plot size. For the plot color, we create two cases using depth, and Timefrom_RefYears (years since 2000) which is calculated using np.datetime64 and np.timedelta64 function.

Convert the earthquake location to the spherical coordinates and create a color bar

Here we convert earthquake longitude and latitude to the spherical coordinates as described in 1–2, and create a color bar used for depth and Timefrom_RefYears. For a color bar, we adopt matplotlib color bar and convert to Plotly format.

Create the earthquake distribution plot

Firstly, we slightly change the location parameters using depth to create a three-dimensional effect for the earthquake plot. Although earthquakes occur under the ground (of course!), we plot the earthquake distribution above the interactive globe to make them visible.

2–2. Prepare the plate boundary plot and convert coordinates

In this post, we will plot the earthquake distribution on the interactive globe created in Step 1. However, in order to make an earthquake distribution more clear, we also plot this distribution on just a simple plate boundaries map. In this step, we prepare that plate boundary map. This procedure is already clearly explained in these posts:

Plotly chart_studio posted by Dreamshot: https://plot.ly/~Dreamshot/9152

2–3. Plot the earthquake distribution on the interactive globe and the plate boundary plot created above

Using these new location parameters, we make the three different ways of Plotly function as follows:

  • 3D earthquake distribution on the interactive globe with the color of depth
  • 3D earthquake distribution on the interactive globe with the color of years since 2000
  • 3D earthquake distribution on the plate boundary plot

3D earthquake distribution on the interactive globe with the color of depth

  • The color describes earthquake depths ranging from 0 (red) to 700 km (blue)
Image by Author

Demo

3D earthquake distribution on the interactive globe with the color of years since 2000

  • The color describes how many years passed since 2000 from 0 year (red) to 20 years (blue)
Image by Author

Demo

3D earthquake distribution on the plate boundary plot without interactive globe

Firstly, we create an inner black sphere to hide inside of the globe:

Then create the 3D plot using the plate boundary data we prepared at 2.2:

Image by Author

Demo

That’s about it!

Thanks for reading this article and hope you enjoy your own plot. I’m happy to get any comments from all of you!

Links

Other articles

Personal website

--

--