xrview

Visualizing xarray data with bokeh.

The code is hosted on GitHub: https://github.com/phausamann/xrview

Installation

Latest release

xrview is currently unreleased, but can be installed from the GitHub repository via pip:

$ pip install git+https://github.com/phausamann/xrview.git

This will also install the minimal dependencies for creating HTML plots.

Optional dependencies

For plotting in a jupyter notebook, you will also need the notebook and requests packages:

$ pip install notebook requests

Bokeh server support depends on a specific version of tornado:

$ pip install tornado<=4.5.3

Usage

xrview provides several utilities for automatically creating interactive bokeh plots from xarray data types.

Basic plotting with HTML output

xrview.plot will create a plot of an xarray.DataArray or Dataset given the name of the dimension that represents the x coordinate in the plot.

All examples in this section assume the following imports:

>>> import numpy as np
>>> import xarray as xr
>>> import xrview

Minimal example

The following code will open a browser tab with the figure shown below.

>>> x = np.linspace(0, 1, 100)
>>> y = np.sqrt(x)
>>> da = xr.DataArray(y, {'x': x}, 'x')
>>> plot = xrview.plot(da, x='x')
>>> plot.show() 

Overlaying and tiling plots

When passing a Dataset, each variable will be plotted in a separate figure. If the variable has a second dimension, each element along this dimension will be plotted as a separate line and a legend will be automatically created based on the coordinates of this dimension.

>>> x = np.linspace(0, 1, 100)
>>> y = np.vstack([np.sqrt(x), x, x ** 2]).T
>>> ds = xr.Dataset({'Clean': (['x', 'f'], y),
...                  'Noisy': (['x', 'f'], y + 0.01*np.random.randn(100, 3))},
...                 {'x': x, 'f': ['sqrt(x)', 'x', 'x^2']})
>>> plot = xrview.plot(ds, x='x', ncols=2)
>>> plot.show() 

Alternatively, you can show the elements of the dimension in separate figures and overlay the variables by specifying overlay='data_vars':

>>> plot = xrview.plot(ds, x='x', ncols=2, overlay='data_vars')
>>> plot.show() 

You can add additional figures to the plot with the add_figure() method by providing data as a DataArray. The DataArray has to contain a coordinate with the same name as the x coordinate, but they do not need to have the same values.

>>> da = xr.DataArray(np.ones(20), {'x': np.linspace(0, 1, 20)}, 'x')
>>> plot = xrview.plot(ds, x='x', ncols=2)
>>> plot.add_figure(da)
>>> plot.show() 

Data can also be overlaid onto existing figures with the add_overlay() method. With the onto parameter, you can select a figure by index or title on which to overlay the data. By default, the data will be overlaid onto all figures.

>>> plot = xrview.plot(ds, x='x', ncols=2)
>>> plot.add_overlay(da, onto='Clean')
>>> plot.show() 

Customizing glyphs and annotations

xrview supports plotting with many of the standard bokeh glyphs as well as some custom composite glyphs such as error bars and boxes for box plots. Glyphs can be passed to plot(), add_overlay() and add_figure() as strings, instances of a glyph class or iterables of any combination of both. When you pass a glyph instance, you can specify additional keyword arguments.

In this example, one array is plotted with circles and a second with a blue line and red squares:

>>> from xrview.glyphs import Square
>>> x = np.linspace(0, 1, 100)
>>> y = np.sqrt(x)
>>> da_sqrt = xr.DataArray(y, {'x': x}, 'x')
>>> da_const = xr.DataArray(np.ones(20), {'x': x[::5]}, 'x')
>>> plot = xrview.plot(da_sqrt, x='x', glyphs='circle')
>>> plot.add_overlay(da_const, glyphs=['line', Square(color='red')])
>>> plot.show() 

xrview also provides a straightforward way of

Note

See Glyphs for a list of available glyphs.

Categorical and time series data

Fine-tuning bokeh figures

Interactive plotting

Note

Interactive plotting is so far only supported in jupyter notebooks.

Adding interactions

Sub-sampled timeseries plots

API Reference

Top-level functions

Module: xrview

plot

Create a plot from xarray data.

HTML

Module: xrview.html

HtmlPlot

Base class for HTML plots.

HtmlGridPlot

An HTML grid plot.

HtmlSpacer

An HTML spacer.

Notebook

Module: xrview.notebook

NotebookPlot

Base class for notebook plots.

NotebookGridPlot

A notebook grid plot.

NotebookSpacer

A notebook spacer.

NotebookViewer

Base class for notebook viewers.

NotebookTimeseriesViewer

A notebook time-series viewer.

Glyphs

Module: xrview.glyphs

Line

A line glyph.

Circle

A circle glyph.

Diamond

A diamond glyph.

Square

A square glyph.

Triangle

A triangle glyph.

Ray

A ray glyph.

HBar

A horizontal bar glyph.

VBar

A vertical bar glyph.

Rect

A rectangle glyph.

Whisker

A whisker annotation.

Band

A band annotation.

VLine

A collection of vertical lines.

ErrorLine

A line with an error bar.

ErrorCircle

A circle with an error bar.

BoxWhisker

A box-whisker glyph.

Interactions

Module: xrview.interactions

CoordValSelect

A list widget for selecting unique values of a certain coordinate.

Class member details

html

HtmlPlot
Plot modifiers

HtmlPlot.add_annotation

Add an annotation to a figure in the layout.

HtmlPlot.add_figure

Add a figure to the layout.

HtmlPlot.add_overlay

Add an overlay to a figure in the layout.

HtmlPlot.modify_figures

Modify the attributes of a figure.

Show and export

HtmlPlot.export

Export the layout as as png or svg file.

HtmlPlot.show

Show the plot in an HTML file.

Internal

HtmlPlot.copy

Create a copy of this instance.

HtmlPlot.make_doc

Make the document.

HtmlPlot.make_layout

Make the layout.

notebook

NotebookPlot
Plot modifiers

NotebookPlot.add_annotation

Add an annotation to a figure in the layout.

NotebookPlot.add_figure

Add a figure to the layout.

NotebookPlot.add_overlay

Add an overlay to a figure in the layout.

NotebookPlot.modify_figures

Modify the attributes of a figure.

Show and export

NotebookPlot.export

Export the layout as as png or svg file.

NotebookPlot.show

Show the plot in a jupyter notebook.

Internal

NotebookPlot.copy

Create a copy of this instance.

NotebookPlot.make_doc

Make the document.

NotebookPlot.make_layout

Make the layout.

NotebookViewer
Plot modifiers

NotebookViewer.add_annotation

Add an annotation to a figure in the layout.

NotebookViewer.add_figure

Add a figure to the layout.

NotebookViewer.add_interaction

Add an interaction to the layout.

NotebookViewer.add_overlay

Add an overlay to a figure in the layout.

NotebookViewer.modify_figures

Modify the attributes of a figure.

Show and export

NotebookViewer.export

Export the layout as as png or svg file.

NotebookViewer.show

Show the app in a jupyter notebook.

Internal

NotebookViewer.copy

Create a copy of this instance.

NotebookViewer.make_doc

Make the document.

NotebookViewer.make_layout

Make the layout.

NotebookViewer.reset_handlers

Reset handlers.

NotebookViewer.update_handler

Update a single handler.

NotebookViewer.update_handlers

Update handlers.

NotebookViewer.update_inplace

Update this instance with the properties of another layout.

Callbacks

NotebookViewer.on_reset

Callback for reset event.

NotebookViewer.on_selected_points_change

Callback for selection event.

NotebookTimeseriesViewer
Plot modifiers

NotebookTimeseriesViewer.add_annotation

Add an annotation to a figure in the layout.

NotebookTimeseriesViewer.add_figure

Add a figure to the layout.

NotebookTimeseriesViewer.add_interaction

Add an interaction to the layout.

NotebookTimeseriesViewer.add_overlay

Add an overlay to a figure in the layout.

NotebookTimeseriesViewer.modify_figures

Modify the attributes of a figure.

Show and export

NotebookTimeseriesViewer.export

Export the layout as as png or svg file.

NotebookTimeseriesViewer.show

Show the app in a jupyter notebook.

Internal

NotebookTimeseriesViewer.copy

Create a copy of this instance.

NotebookTimeseriesViewer.make_doc

Make the document.

NotebookTimeseriesViewer.make_layout

Make the layout.

NotebookTimeseriesViewer.reset_handlers

Reset handlers.

NotebookTimeseriesViewer.update_handler

Update a single handler.

NotebookTimeseriesViewer.update_handlers

Update handlers.

NotebookTimeseriesViewer.update_inplace

Update this instance with the properties of another layout.

Callbacks

NotebookTimeseriesViewer.on_reset

Callback for reset event.

NotebookTimeseriesViewer.on_selected_points_change

Callback for selection event.

NotebookTimeseriesViewer.on_xrange_change

Callback for xrange change event.

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/phausamann/xrview/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.

  • Any details about your local setup that might be helpful in troubleshooting.

  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is open to whoever wants to implement it.

Write Documentation

xrview could always use more documentation, whether as part of the official xrview docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/phausamann/xrview/issues.

If you are proposing a feature:

  • Explain in detail how it would work.

  • Keep the scope as narrow as possible, to make it easier to implement.

  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up xrview for local development.

  1. Fork the xrview repo on GitHub.

  2. Clone your fork locally:

    $ git clone git@github.com:your_name_here/xrview.git
    
  3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

    $ mkvirtualenv xrview
    $ cd xrview/
    $ python setup.py develop
    
  4. Create a branch for local development:

    $ git checkout -b name-of-your-bugfix-or-feature
    

    Now you can make your changes locally.

  5. When you’re done making changes, check that your changes all tests and linters with tox:

    $ tox
    

    To get tox, just pip install it into your virtualenv.

  6. Commit your changes and push your branch to GitHub:

    $ git add .
    $ git commit -m "Your detailed description of your changes."
    $ git push origin name-of-your-bugfix-or-feature
    
  7. Submit a pull request through the GitHub website.

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines:

  1. The pull request should include tests.

  2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.

  3. The pull request should work for Python 3.6, 3,7 and 3.8. Check https://travis-ci.org/phausamann/xrview/pull_requests and make sure that the tests pass for all supported Python versions.

Tips

To run a subset of tests:

$ py.test tests.test_xrview

Deploying

A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.rst). Then run:

$ bumpversion patch # possible: major / minor / patch
$ git push
$ git push --tags

Travis will then deploy to PyPI if tests pass.

Credits

Development Lead

Contributors

None yet. Why not be the first?

History

0.2.1 (Oct 19, 2020)

Bug fixes

  • Fixed tooltip rendering for datetime axis.

0.2.0 (Sep 8, 2020)

Breaking changes

  • Dropped compatibility with Python 2.7 and 3.5 and bokeh versions < 2.2

0.1.0 (Sep 8, 2020)

  • First official release

  • Last release compatible with Python 2.7/3.5

Indices and tables