{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "\n", "

Exercises 2: Linking Plots

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise we will link plots generated with `hvplot` from the earthquake data using HoloViews linked selections.\n", "\n", "#### Loading the data as before" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pathlib\n", "import pandas as pd\n", "import holoviews as hv # noqa\n", "\n", "import hvplot.pandas # noqa: adds hvplot method to pandas objects\n", "import hvplot.xarray # noqa: adds hvplot method to xarray objects" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df = pd.read_parquet(pathlib.Path('../../data/earthquakes-projected.parq'))\n", "df = df.set_index(df.time)\n", "most_severe = df[df.mag >= 7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### The distribution of earthquakes over space \n", "\n", "##### Over latitude\n", "\n", "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.\n", "\n", "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.\n", "\n", "
Hint
\n", "\n", "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.\n", "\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lat_scatter = ... # Use hvplot here to visualize a latitude scatter from most_severe\n", "lat_scatter # Display it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", "\n", "```python\n", "lat_scatter = most_severe.hvplot(\n", " x='time', y='latitude', kind='scatter', color='red', marker='+', responsive=True, height=300)\n", "lat_scatter \n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "##### Combined with a longitude scatter plot\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Hint
\n", "\n", "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.\n", "
\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lon_scatter = ... # Use hvplot here to visualize a longitude scatter from most_severe\n", "# Display it to the left of lat_scatter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", "\n", "```python\n", "lon_scatter = most_severe.hvplot(\n", " x='time', y='longitude', kind='scatter', color='blue', marker='x', responsive=True, height=300)\n", "lon_scatter + lat_scatter\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have two scatter plots derived from the `most_severe` `DataFrame` but right now they are not linked.\n", "\n", "#### Linking the scatter plots\n", "\n", "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.\n", "\n", "\n", "
Hint
\n", "\n", "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.\n", "
\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display a longitude scatter on the left that is linked to a latitude scatter on the right" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", " \n", "Combining the plots as well as the linked selection:\n", "\n", "```python\n", "ls = hv.link_selections.instance()\n", "\n", "lat_scatter = most_severe.hvplot(\n", " x='time', y='latitude', kind='scatter', color='red', marker='+', responsive=True, height=300)\n", "\n", "lon_scatter = most_severe.hvplot(\n", " x='time', y='longitude', kind='scatter', color='blue', marker='x', responsive=True, height=300)\n", "\n", "ls(lon_scatter + lat_scatter)\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "#### Analysing the filtered selection\n", "\n", "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.\n", "\n", "
Hint
\n", "\n", "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.\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Display a summary of a linked selection in the plot above using the pandas .describe()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", "\n", "Assume the handle to the linked selection is called `ls`:\n", "\n", "```python\n", "ls.filter(most_severe).describe()\n", "```\n", "\n", "
" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 5 }