Plotting

When trying to make sense of data, there are many representations to choose from, including data tables, textual summaries and so on. We'll mostly focus on plotting data to get an intuitive visual representation, using a simple but powerful plotting API.

If you have tried to visualize a pandas.DataFrame before, then you have likely encountered the Pandas .plot() API. These plotting commands use Matplotlib to render static PNGs or SVGs in a Jupyter notebook using the inline backend, or interactive figures via %matplotlib widget, with a command that can be as simple as df.plot() for a DataFrame with one or two columns.

The Pandas .plot() API has emerged as a de-facto standard for high-level plotting APIs in Python, and is now supported by many different libraries that use various underlying plotting engines to provide additional power and flexibility. Learning this API allows you to access capabilities provided by a wide variety of underlying tools, with relatively little additional effort. The libraries currently supporting this API include:

  • Pandas -- Matplotlib-based API included with Pandas. Static or interactive output in Jupyter notebooks.
  • xarray -- Matplotlib-based API included with xarray, based on pandas .plot API. Static or interactive output in Jupyter notebooks.
  • hvPlot -- HoloViews and Bokeh-based interactive plots for Pandas, GeoPandas, xarray, Dask, Intake, and Streamz data.
  • Pandas Bokeh -- Bokeh-based interactive plots, for Pandas, GeoPandas, and PySpark data.
  • Cufflinks -- Plotly-based interactive plots for Pandas data.
  • Plotly Express -- Plotly-Express-based interactive plots for Pandas data; only partial support for the .plot API keywords.
  • PdVega -- Vega-lite-based, JSON-encoded interactive plots for Pandas data.

In this notebook we'll explore what is possible with the default .plot API and demonstrate the additional capabilities provided by .hvplot, which include seamless interactivity in notebooks and deployed dashboards, server-side rendering of even the largest datasets, automatic small multiples and widget selectors for exploring complex data, and easy composition and linking of plots after they are generated.

To show these features, we'll use a tabular dataset of earthquakes and other seismological events queried from the USGS Earthquake Catalog using its API. Of course, this particular dataset is just an example; the same approach can be used with just about any tabular dataset, and similar approaches can be used with gridded (multidimensional array) datasets.

Read in the data

Here we will focus on Pandas, but a similar approach will work for any supported DataFrame type, including Dask for distributed computing or RAPIDS cuDF for GPU computing. This dataset is relatively large (2.1 million rows), but should still fit into memory on any recent machine, and thus won't need special out-of-core or distributed approaches like Dask provides.

In [1]:
import pandas as pd
In [2]:
%%time
df = pd.read_parquet('../data/earthquakes-projected.parq')
df.time = df.time.astype('datetime64[ns]')
df = df.set_index(df.time)
CPU times: user 9 s, sys: 1.62 s, total: 10.6 s
Wall time: 10.6 s
In [3]:
print(df.shape)
df.head()
(2116537, 25)
Out[3]:
index depth depthError dmin gap horizontalError id latitude locationSource longitude ... net nst place rms status time type updated easting northing
time
2000-01-31 23:52:00.619 0 7.800 1.400 0.09500 245.14 NaN nn00001936 37.1623 nn -116.6037 ... nn 5.0 Nevada 0.0519 reviewed 2000-01-31 23:52:00.619 earthquake 2018-04-24T22:22:44.135Z -1.298026e+07 4.461754e+06
2000-01-31 23:44:54.060 1 4.516 0.479 0.05131 52.50 NaN ci9137218 34.3610 ci -116.1440 ... ci 0.0 26km NNW of Twentynine Palms, California 0.1300 reviewed 2000-01-31 23:44:54.060 earthquake 2016-02-17T11:53:52.643Z -1.292909e+07 4.077379e+06
2000-01-31 23:28:38.420 2 33.000 NaN NaN NaN NaN usp0009mwt 10.6930 trn -61.1620 ... us NaN Trinidad, Trinidad and Tobago NaN reviewed 2000-01-31 23:28:38.420 earthquake 2014-11-07T01:09:23.016Z -6.808523e+06 1.197310e+06
2000-01-31 23:05:22.010 3 33.000 NaN NaN NaN NaN usp0009mws -1.2030 us -80.7160 ... us NaN near the coast of Ecuador 0.6000 reviewed 2000-01-31 23:05:22.010 earthquake 2014-11-07T01:09:23.014Z -8.985264e+06 -1.339272e+05
2000-01-31 22:56:50.996 4 7.200 0.900 0.11100 202.61 NaN nn00001935 38.7860 nn -119.6409 ... nn 5.0 Nevada 0.0715 reviewed 2000-01-31 22:56:50.996 earthquake 2018-04-24T22:22:44.054Z -1.331836e+07 4.691064e+06

5 rows × 25 columns

To compare HoloViz approaches with other approaches, we'll also construct a subsample of the dataset that's tractable with any plotting or analysis tool, but has only 1% of the data:

In [4]:
small_df = df.sample(frac=.01)
print(small_df.shape)
small_df.head()
(21165, 25)
Out[4]:
index depth depthError dmin gap horizontalError id latitude locationSource longitude ... net nst place rms status time type updated easting northing
time
2005-05-11 06:12:30.050 6558 5.413 1.76 0.005405 112.00 0.57 nc21455869 35.980167 nc -120.552500 ... nc 10.0 Central California 0.0300 reviewed 2005-05-11 06:12:30.050 earthquake 2017-01-11T04:45:00.669Z -1.341984e+07 4.297893e+06
2013-10-17 08:53:52.000 4621 1.000 0.00 NaN NaN 0.20 ak10825673 61.482100 ak -140.648900 ... ak NaN 185km NNE of Cape Yakataga, Alaska 0.6600 reviewed 2013-10-17 08:53:52.000 earthquake 2019-05-16T00:24:04.287Z -1.565696e+07 8.737371e+06
2010-07-25 07:38:35.167 2819 15.200 2.30 NaN NaN NaN ak0109gt3qvw 51.071200 ak 179.268400 ... ak NaN Rat Islands, Aleutian Islands, Alaska 0.3000 reviewed 2010-07-25 07:38:35.167 earthquake 2018-07-06T21:17:26.722Z 1.995607e+07 6.633898e+06
2009-10-17 13:35:39.077 3576 2.400 4.60 0.084000 178.81 NaN nn00295496 36.770800 nn -116.069700 ... nn 23.0 Nevada 0.0971 reviewed 2009-10-17 13:35:39.077 earthquake 2018-06-13T20:45:48.861Z -1.292082e+07 4.407207e+06
2015-02-15 04:29:41.300 5290 3.143 1.82 0.102400 162.00 0.54 ci37316776 35.433333 ci -118.725833 ... ci 14.0 26km NE of Lamont, CA 0.1900 reviewed 2015-02-15 04:29:41.300 earthquake 2016-03-11T08:07:39.647Z -1.321650e+07 4.222926e+06

5 rows × 25 columns

We'll switch back and forth between small_df and df depending on whether the technique we are showing works well only for small datasets, or whether it can be used for any dataset.

Using Pandas .plot()

The first thing that we'd like to do with this data is visualize the locations of every earthquake. So we would like to make a scatter or points plot where x is longitude and y is latitude.

We can do that for the smaller dataframe using the pandas.plot API and Matplotlib:

In [5]:
%matplotlib inline
In [6]:
small_df.plot.scatter(x='longitude', y='latitude');

Exercise:

Try changing inline to widget and see what interactivity is available from Matplotlib. In some cases you may have to reload the page and restart this notebook to get it to display properly.

Using .hvplot

As you can see above, the Pandas API gives you a usable plot very easily, where you can start to see the structure of the edges of the tectonic plates, which in many cases correspond with the visual edges of continents (e.g. the westward side of Africa, in the center). You can make a very similar plot with the same arguments using hvplot, after importing hvplot.pandas to install hvPlot support into Pandas:

In [7]:
import hvplot.pandas # noqa: adds hvplot method to pandas objects