Introduction to Visualization¶
Right click to download this notebook from GitHub.
In the previous step of the tutorial we made some plots using hvplot
, which provides interactive plots using a syntax similar to the Pandas .plot()
method. In addition to Pandas, hvPlot
supports XArray, Dask, GeoPandas, and a variety of other libraries. The result of an hvplot
call is a holoviews
object. This might seem a little mysterious, so we'll take a minute to show how this works. In this tutorial step we'll use generated data for simplicity.
import numpy as np
import pandas as pd
index = pd.date_range('1/1/2000', periods=1000)
np.random.seed(11) # seeding the state to make the output identical each time
data = np.random.randn(1000, 4)
df = pd.DataFrame(data, index=index, columns=list('ABCD')).cumsum()
df.head()
Use hvplot
to product an interactively explorable plot with panning, zooming, hovering, and clickable/selectable legends:
import hvplot.pandas
df.hvplot()
# Exercise: Scroll in and out of the plot, click on an item in the legend, save a png
Selection¶
To inspect this plot more closely we can assign it to a variable and print that variable. We'll see that we in fact have a holoviews object of type NdOverlay
and subtype Curve
.
plot = df.hvplot()
print(plot)
But what does that mean? What we got out of hvplot was actually a mapping.
# Exercise: Use plot.keys() to inspect the keys of the mapping.
We can select just one of the variables from the mapping just like you would from a dictionary:
plot['A']
# Exercise: select a different label from the plot.
We can create an overlay of just a few of the plots using the *
operator
plot['B'] * plot['C']
Subplots¶
We can also switch it around so that instead of having all the plots overlayed, we can display them next to each other. For this we will use the '+' operator. Note that we also specified that there should only be one column, otherwise they would be added row-wise.
(plot['B'] + plot['C']).cols(1)
Another way to achieve a similar result is to use the subplots
keyword argument in hvplot
.
df.hvplot(subplots=True, width=300).cols(2)
# Exercise: Try to create something other than a line plot. Perhaps a scatter - df.hvplot.scatter().
# Challenge: Use tab completion to explore other plot types.
Visualizing images¶
In the Data Ingestion tutorial, we saw how you can view Landsat imagery loaded with intake. First let us import intake
and hvplot.xarray
to load hvplot
support for xarray
:
import intake
import hvplot.xarray
Now we can open the catalog and read in our data for Landsat 5 and 8. We'll be using the small versions in this tutorial, but feel free to change them to cat.landsat_5
and cat.landsat_8
to use the large versions instead.
cat = intake.open_catalog('../catalog.yml')
landsat_5 = cat.landsat_5_small.read_chunked()
landsat_8 = cat.landsat_8_small.read_chunked()
Using geo=True
in hvplot
, we can ensure the appropriate coordinate reference system is used. In the next cell, we use this along with cmap='fire'
to change the color map:
landsat_5.hvplot(x='x', y='y', rasterize=True, geo=True, width=400, cmap='fire')
# Exercise: Try adjusting the width and adding a height option to make the overall plot bigger or smaller.
The 'fire' colormap is one of the perceptually uniform colormaps provided by colorcet. These colormaps are especially designed to help reveal features of the data that may be lost when using colormaps that are not perceptually uniform.
Combining images with +
¶
In the same way you can combine curves with +
(as shown above using data loaded from a pandas DataFrame), you can do the same using images created from xarray
objects.
In the next cell, we use different colormaps for the Landsat 5 and Landsat 8 data, datashade them and position them next to each other:
landsat_5_plot = landsat_5.hvplot(x='x', y='y', width=300, datashade=True, geo=True, cmap='Reds')
landsat_8_plot = landsat_8.hvplot(x='x', y='y', width=300, datashade=True, geo=True, cmap='Blues')
landsat_5_plot + landsat_8_plot
As datashade=True
, the plots refresh with the available data when the zoom tool is used. Note that the plots have linked axes making comparison between plots easier. Note that the plots will only update to show higher resolution if a live Python process is running; on a web site zooming will only ever show the original set of pixels, getting larger on a zoom rather than revealing more data as you'll see if Python is available.
hvPlot also supports a large range of plot types and options not shown here. A more detailed description of visualization options will follow in the Data Visualization tutorial. See the hvplot.pyviz.org website for further details, and holoviews.org, geoviews.org, and datashader.org for information about the underlying functionality and advanced usage. You can also work through the extensive tutorials at pyviz.org