When creating figures in matplotlib, there may be occasions where you want to add a smaller figure or axes inside of your main figure. This could be for a number of reasons, but most commonly, it is used to highlight an area of interest and enlarge it or to include additional information that is related to the main figure.
Matplotlib makes this process very simple by allowing us to add inset axes within the main figure. This can then be further tweaked to get the information we want displayed how we want.
In this short tutorial, we will see how to create a simple well location map, like the one below, using grid coordinates. We will then add inset axes to highlight and enlarge a smaller area of the main map.
The first step in our tutorial is to import the libraries we are going to be using.
The first two import statements should be familiar: importing pandas and matplotib. The third import from
mpl.toolkits.axes_grid1.inset_locator allows us to plot our inset figure within the bounds of our main figure.
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.inset_locator import inset_axes, mark_inset
Once the libraries have been imported, we can import the data. The data used here is from the Force 2020 Xeek Machine Learning competition and contains around 100 wells worth of well logging measurements.
As this file is delimited by a semi-colon (
; ) we need to include an extra parameter to tell the
pd.read_csv() function that is what the separator is between the values.
df = pd.read_csv('../data/Xeek Force 2020/train.csv', sep=';')