Plotting USGS Earthquake Data with Folium

Example of Folium data plotting in Python

Aaron Lee
Level Up Coding

--

Final map

One of the many services provided by the US Geological Survey (USGS) is the monitoring and tracking of seismological events worldwide. I recently stumbled upon their earthquake datasets provided at the website below.

The site has data feeds that contain ‘live’ csv data for every significant earthquake over the past hour, day, week, or month. The data is updated every minute and contains magnitudes, lat/long, depth, and other earthquake descriptors.

While there are lots of earthquake visualizations out there, I thought it would be a fun exercise to see what could be easily created in Folium from the raw data. For this project, we will be plotting every earthquake worldwide using just Pandas and Folium. We will also add some tectonic plate boundaries with geoJSON just for fun.

Importing Our Data

I will be using the data feed located at the following URL.

https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.csv

That is the direct link to a csv formatted file containing every earthquake greater than magnitude 2.5, worldwide, over the last month. I encourage you to try out the other data feeds, but I found that the size of the dataset really balloons when you include smaller quakes.

In the code below, we import our libraries, and use the pandas.read_csv() function to create a DataFrame object directly from the URL.

Available columns from earthquake DataFrame

The data column descriptions are well documented on the USGS website. The relevant columns for our project will be: latitude, longitude, and mag (magnitude of the quake). All are stored as float objects and there is no preprocessing necessary. Thank you USGS for keeping these datasets clean and user friendly.

Making a Base Map in Folium

We will start by making a simple map in Folium using the code below.

We chose a lat/long of (0, 0) since we are plotting the whole world. A zoom value of 2 worked well for me to see the entire Earth in openstreetmap. My resulting earthquakes.html file looked like this.

Basic world map

Adding Earthquake Data using Folium Circles

Now that we have a working base map, let’s plot our earthquakes. We will use Folium’s Circle object to represent each quake. To start, we will just make all the earthquakes the same size.

In the code above, we iterated through each earthquake in the DataFrame, created a Circle object at that location, and added it to my map using the add_to() method. We end up with an already impressive map showing all of the significant earthquakes over the past month. The data is constantly changing, so your attempt might look different.

Visualizing Earthquake Magnitudes

To give us a quick visual representation of the magnitudes, I chose to alter the size of each circle based on the size of the quake. (alternately, you could experiment with colormaps or heatmaps)

For the last map, I used a radius of 10 for every Circle and we saw each earthquake represented by a blue dot. The Circle object’s radius is displayed in meters on your map, so each Circle marker shows up as a 10m ring when you zoom all the way in on your interactive map.

We will make the radius a function of the earthquake’s magnitude. Large magnitude quakes will be represented by large radius Circle markers.

I chose to set my radius equal to 50,000 times the magnitude. A 4.0 earthquake would show on my map as having a radius of 200,000m or 200km. That value felt right for me, but you could certainly change it, especially if you were plotting regional data.

Now we can clearly see the relative size of the plotted earthquakes, although we certainly have some work to do on the formatting.

Making it Pretty

The map is now functional with minimal coding. We can now use the Circle object’s keyword arguments to make them more attractive.

This time, we specified five new keyword arguments (weight, color, opacity, fill_color, and fill_opacity) in our Circle objects. We now see multiple earthquakes on top of each other. In addition to the relative sizes to represent magnitude, the darker red now represents hot spots (multiple quakes), and gives it a heatmap effect.

Adding Tectonic Plates Using GeoJSON

When we look at the resulting map, we see a visualization of the Pacific rim’s ‘ring of fire’. I immediately had the thought of laying the actual tectonic plate boundaries as an overlay to my map.

A quick google search led me to this file on github with the polygons for the tectonic boundaries stored in geoJSON format.

GeoJSON is my personal favorite filetype for shapes when using Python and Folium, but you could use other shape files as well. The geoJSON format has the advantage of working as a JSON file and can be treated like a dictionary in Python should you need to.

The code below shows how Folium can easily handle a GeoJson file to add a map overlay.

The GeoJson object is added directly to the map we just created. Resaving the map gives the following result.

The map results are exactly as you might expect. Earthquakes are neatly placed along the boundaries of our tectonic plates, just like they were in my middle school science textbook.

Going Further

Now that we have an attractive earthquake map, you may want to do some additional work to create something even more amazing.

Consider making a web application with controls for magnitude, time ranges, and locations. In the photo below, we see all of the earthquakes in San Francisco over the past seven days.

Cisco 7 day map

Consider looking into the significant formatting options of the Folium Circle class. With the popup kwarg, you can insert html tags for every earthquake. You could then add the magnitude and description for every quake worldwide. The image below is an example of a popup that displays the string from the ‘place’ column

example of popup

See what you can create with these fantastic datasets. If you make something beautiful, let me know. Good luck!

--

--