Linking Plots#

In this exercise we will link plots generated with hvplot from the earthquake data using HoloViews linked selections.

Loading the data as before#

import pathlib
import pandas as pd
import holoviews as hv # noqa

import hvplot.pandas # noqa: adds hvplot method to pandas objects
import hvplot.xarray # noqa: adds hvplot method to xarray objects
df = pd.read_parquet(pathlib.Path('../../data/earthquakes-projected.parq'))
df = df.set_index(df.time)
most_severe = df[df.mag >= 7]

The distribution of earthquakes over space#

Over latitude#

So far we have seen linked histograms but the same approach generalizes to many other plot types. This time we shall use kind='scatter' to generate scatter plots instead of histograms.

First create a scatter called lat_scatter that plots the latitudes of the most_severe earthquakes over time. Customize it by making the scatter points red plus markers (+) and the plot responsive with height of 300 pixels. At the end of the cell, display this object.


Hint

The time of the earthquakes are in the 'time' column while the latitudes are in the 'latitude' column of most_severe. The scatter point color is controlled by the color keyword argument and 'red' is a valid color specification. The height is controlled by a keyword of the same name and marker='+' will use that marker style.

lat_scatter = ... # Use hvplot here to visualize a latitude scatter from most_severe
lat_scatter  # Display it
Ellipsis
lat_scatter = most_severe.hvplot(
     x='time', y='latitude', kind='scatter', color='red', marker='+', responsive=True, height=300)
lat_scatter 

Combined with a longitude scatter plot#

Now make a corresponding scatter plot over longitude called lon_scatter that plots the longitudes of the most_severe earthquakes over time. Customize it by making the scatter out of blue cross markers (x) with the same height of 300 pixels as before. It should be responsive and at the end of the cell, display this object in a layout with the previous lat_scatter plot. The longitude plot should be on the left and the latitude plot should be on the right.


Hint

This plot is identical to the previous one except the name of the handle and the fact that the 'longitude' column is now used and the points are colored blue. To combine the plots together, use the previous handle of lat_scatter to create a layout with lon_scatter and the HoloViews + operator.

lon_scatter = ... # Use hvplot here to visualize a longitude scatter from most_severe
# Display it to the left of lat_scatter
lon_scatter = most_severe.hvplot(
         x='time', y='longitude', kind='scatter', color='blue', marker='x', responsive=True, height=300)
lon_scatter + lat_scatter

Now we have two scatter plots derived from the most_severe DataFrame but right now they are not linked.

Linking the scatter plots#

Now we can use hv.link_selections to link these two scatter plots together. Create the same layout as before with the longitude scatter on the left and the latitude scatter on the right, but this time link them together.


Hint

You will need to make a linked selection instance with hv.link_selections.instance() and pass the scatter layout to that instance in order to link the plots.

# Display a longitude scatter on the left that is linked to a latitude scatter on the right

Combining the plots as well as the linked selection:

ls = hv.link_selections.instance()

lat_scatter = most_severe.hvplot(
     x='time', y='latitude', kind='scatter', color='red', marker='+', responsive=True, height=300)

lon_scatter = most_severe.hvplot(
         x='time', y='longitude', kind='scatter', color='blue', marker='x', responsive=True, height=300)

ls(lon_scatter + lat_scatter)

Use the box select tool to confirm that these plots are now linked. Note that you can reset the selection with the reset button in the Bokeh toolbar.

Analysing the filtered selection#

Now that we have linked plots, we can interactively select points in the visualization and then use that selection to filter the original DataFrame. After making a selection in the plot above, show a statistical summary of the points that have been selected.


Hint

The linked selection object has a .filter method that can filter your original DataFrame (most_severe). To compute statistics of a DataFrame, you can use the pandas .describe() method.

# Display a summary of a linked selection in the plot above using the pandas .describe()

Assume the handle to the linked selection is called ls:

ls.filter(most_severe).describe()
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Right click to download this notebook from GitHub.