Using Plotly Express Sunburst Charts to Explore Geological Data

Data visualisation plays a vital role in the geoscience and Data Science domains. It can allow us to gain deeper insights into the subsurface, understanding geological structures and hierarchical relationships. The subsurface is often subdivided into different categories ranging from the most extensive scope of geological time, such as Eras, Periods and Epochs, all the way down to lithological differences, such as sandstone, limestone and shale.
When working with geological hierarchical data, the data can be visualised in several ways. This includes conventional geological timescale charts and tables to interactive sunburst charts.
Sunburst charts can be used to present data in a unique way and are a great way to visualise hierarchical data such as geological hierarchical data. They do so by using multi-level concentric doughnut charts, which, depending on the tool used, can be fully interactive and help with drilling down from the highest to the lowest level.
To demonstrate these charts, we will use Plotly Express, a high-level data visualisation Python library, to take some data from a well on the Norwegian Continental Shelf and visualise the geological hierarchy, along with the lithological make-up of each formation. We will also see how to prepare the data from a well before creating the chart.
Importing Libraries and Loading Data
To begin, we will need two libraries: pandas for loading and manipulating our data and plotly_express for creating our visualisation.
import pandas as pd
import plotly_express as px
Next, we will load our data from a CSV file. Details of the data used can be found at the bottom of the article.
If you have a LAS file instead, you can quickly load the LAS file using the LASIO library and then convert the data to a pandas dataframe.
Python">df = pd.read_csv('Data/Xeek_Well_15-9-15.csv')
Cleaning and Preparing the Data
Now that we have the data loaded, we need to do a little cleaning.
First, we will drop any rows with missing information for the Formation column to simplify things. Be aware that doing this could skew the counts of lithologies and formations later on, and any missing data should be thoroughly checked and understood.
An alternative to dropping that data is to replace the missing data with a placeholder value, such as ‘unknown'.
data_cleaned = df.dropna(subset=['FORMATION'])
When we view the first five rows of the dataframe, we get the following.

The next step is to structure the data hierarchically to plot it correctly on the sunburst plot.
To do this, we need to carry out the following
- Group the data in the following order: Group, Formation, and Lith
- Count the occurrences for each unique combination of Group, Formation and Lith
grouped_data = data_cleaned.groupby(['GROUP', 'FORMATION', 'LITH']).size().reset_index(name='count')
When we view the header of the new grouped_data
dataframe, we get the following overview of lithologies per geological formation, which is then associated with the Group.

Creating the Sunburst Chart with Plotly Express
Now that we have the data in the right format, we can finally create the Sunburst plot with Plotly Express.
All we have to do is call upon px.sunburst
and pass in some parameters.
First, we pass in the grouped dataframe, and then for the path
parameter, we specify the columns from that dataframe in the order we want the rings to be displayed. In our case, we will go from Group to Formation to Lithology.
Next, we set the values
parameter to the count column. This will allow us to control the slice size for each lithology. The greater the amount of that lithology, the wider the slice will be.
Then, we set the width
and height
of our figure, along with the title
.
Finally, we want the colour of the lithologies to be distinct; however, we could equally set this to any other column in the dataframe or a custom colour scale.
fig = px.sunburst(grouped_data,
path=["GROUP", "FORMATION", "LITH"],
values='count',
title="Geological Distribution within well 15/9-15",
width=800,
height=800,
color='LITH')
fig.show()
When we run the above code, we get the following figure, which provides an excellent overview of each geological group and formation and the distribution and contribution of the different lithologies.

One of the great things about using Plotly is that it is interactive by default. We do not need to add extra Python code to make the plots interactive like with matplotlib.
In the example below, we can drill down our data to specific formations and understand their lithological makeup.

Summary
Sunburst charts created with Plotly are a great way to display geological information. They create an excellent visualisation and allow you to interactively drill down through the data to understand the composition of each layer within the geological hierarchy.
With some pre-processing, it is easy to take existing geological information from a well and convert it into a format suitable for plotting. Additionally, when working with interactive figures, you have a much better sense of your data compared to studying static figures. Plus, they are more fun to play around with.
Dataset Used Within this Tutorial
Training dataset used as part of a Machine Learning competition run by Xeek and FORCE 2020 (Bormann et al., 2020). This dataset is licensed under Creative Commons Attribution 4.0 International.
The full dataset can be accessed at the following link: https://doi.org/10.5281/zenodo.4351155.
Thanks for reading. Before you go, you should definitely subscribe to my content and get my articles in your inbox. You can do that here!
Secondly, you can get the full Medium experience and support thousands of other writers and me by signing up for a membership. It only costs you $5 a month, and you have full access to all of the fantastic Medium articles, as well as the chance to make money with your writing.
If you sign up using my link, you will support me directly with a portion of your fee, and it won't cost you more. If you do so, thank you so much for your support.