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

Exercise 1: Plotting

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this exercise we will try making some of our own visualizations using hvPlot.\n", "\n", "#### Loading the data \n", "\n", "We will be building a new visualization based on the same data we have cleaned and filtered in the rest of the tutorial. First we load the earthquakes `DataFrame` and filter to those with `>=7` magnitude:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pathlib\n", "import pandas as pd \n", "import xarray as xr\n", "\n", "import hvplot.pandas # noqa: adds hvplot method to pandas objects\n", "import hvplot.xarray # noqa: adds hvplot method to xarray objects\n", "\n", "df = pd.read_parquet(pathlib.Path('../../data/earthquakes-projected.parq'))\n", "most_severe = df[df.mag >= 7]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And next we load the population density raster data:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ds = xr.open_dataarray(pathlib.Path('../../data/raster/gpw_v4_population_density_rev11_2010_2pt5_min.nc'))\n", "cleaned_ds = ds.where(ds.values != ds.nodatavals).sel(band=1)\n", "cleaned_ds.name = 'population'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing the raster data\n", "\n", "Start by using `hvplot.image` to visualize the whole of the population density data using the Datashader `rasterize` support in hvPlot. Grab a handle on this `Image` HoloViews object called `pop_density` and customize it in the `.hvplot.image` call, enabling a logarithmic color scale and a blue colormap (specifically the `'Blues'` colormap). At the end of the cell, display this object.\n", "\n", "
Hint
\n", "\n", "Don't forget to include `rasterize=True` in the `hvplot.image` call. You can also use `logz=True` (or `cnorm=\"log\"`) with `clim=(1, np.nan)`, then add `cmap='Blues'`.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pop_density = ... # Use hvplot here to visualize the data in cleaned_ds\n", "pop_density # Display it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", "\n", "```python\n", "pop_density = cleaned_ds.hvplot.image(rasterize=True, logz=True, clim=(1, np.nan), cmap='Blues') \n", "pop_density\n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Visualizing the earthquake positions\n", "\n", "Now visualize the tabular data in `most_severe` by building a `hv.Points` object with `.hvplot`. This will be very similar to the approach shown in the tutorial but this time we want all the earthquakes to be marked with red crosses of size 100 at their geographic location. As above, get a handle on the resulting object and display it where the handle is now called `quake_points`.\n", "\n", "_Hint: To figure out which arguments you need to pass, try `help(most_severe.hvplot.points)`, then if you can find the right option for marking the points but don't know what value to pass, try passing an invalid value to see from the error message what the valid options are._\n", "\n", "
Hint
\n", "\n", "Don't forget to map the longitude and latitude dimensions to `x` and `y` in the call to `hvplot.points`.\n", "\n", "
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "quake_points = ... # Use hvplot here to visualize the data in most_severe \n", "quake_points" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", "\n", "```python\n", "quake_points = most_severe.hvplot.points(x='longitude', y='latitude', marker='+', size=100, color='red')\n", "quake_points \n", "```\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Composing these visualizations together\n", "\n", "Now overlay `quake_points` over `pop_density` to check everything looks correct. Make sure the box select tool is working as expected." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Solution
\n", " \n", "```python\n", "pop_density * quake_points\n", "```\n", "\n", "
" ] } ], "metadata": { "language_info": { "name": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 4 }