Skip to content

Commit

Permalink
Merge pull request vega#749 from jakevdp/more-docs
Browse files Browse the repository at this point in the history
DOC: clean up HTML documentation
  • Loading branch information
jakevdp authored Apr 12, 2018
2 parents 704fa4c + a986cea commit d045cfb
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 187 deletions.
2 changes: 1 addition & 1 deletion altair/vega/v2/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
If you see this message, it means the renderer has not been properly enabled
for the frontend that you are using. For more information, see
https://altair-viz.github.io/user_guide/renderers.html
https://altair-viz.github.io/user_guide/troubleshooting.html
"""

renderers = PluginRegistry[RendererType](entry_point_group=ENTRY_POINT_GROUP)
Expand Down
2 changes: 1 addition & 1 deletion altair/vega/v3/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
If you see this message, it means the renderer has not been properly enabled
for the frontend that you are using. For more information, see
https://altair-viz.github.io/user_guide/renderers.html
https://altair-viz.github.io/user_guide/troubleshooting.html
"""

renderers = PluginRegistry[RendererType](entry_point_group=ENTRY_POINT_GROUP)
Expand Down
2 changes: 1 addition & 1 deletion altair/vegalite/v1/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
If you see this message, it means the renderer has not been properly enabled
for the frontend that you are using. For more information, see
https://altair-viz.github.io/user_guide/renderers.html
https://altair-viz.github.io/user_guide/troubleshooting.html
"""

renderers = PluginRegistry[RendererType](entry_point_group=ENTRY_POINT_GROUP)
Expand Down
2 changes: 1 addition & 1 deletion altair/vegalite/v2/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
If you see this message, it means the renderer has not been properly enabled
for the frontend that you are using. For more information, see
https://altair-viz.github.io/user_guide/renderers.html
https://altair-viz.github.io/user_guide/troubleshooting.html
"""

renderers = PluginRegistry[RendererType](entry_point_group=ENTRY_POINT_GROUP)
Expand Down
24 changes: 14 additions & 10 deletions doc/getting_started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Quick Start: Altair + JupyterLab
We recommend installing Altair with JupyterLab. If you would like to use it
with the classic notebook, see :ref:`installation-notebook`.

To install Altair with pip, run the following commands::
To install JupyterLab and Altair with pip, run the following commands::

$ pip install jupyterlab altair
$ jupyter labextension install @jupyterlab/vega3-extension # not needed for JupyterLab 0.32 or newer
Expand All @@ -32,7 +32,9 @@ Once this is finished, run::

$ jupyter lab

In the browser window that launches, under "Notebook" click "Python 3".
In the browser window that launches, under "Notebook" click the first available
kernel (it should say "Python 2" or "Python 3" depending on which Python version
you are running).

In the notebook that opens, you can run the following code to ensure everything
is properly set up:
Expand All @@ -52,7 +54,7 @@ is properly set up:

If the plot does not render, ensure you have installed the exact versions
mentioned above, and if it still does not work see
:ref:`display-troubleshooting` for help.
:ref:`troubleshooting-jupyterlab` for help.

Once things are up and running, you may wish to go through the tutorials at
:ref:`starting` and :ref:`exploring-weather`, read through the User Guide
Expand All @@ -75,7 +77,9 @@ Once the packages and extensions are installed, launch the notebook by running::
$ jupyter notebook

In the browser window that launches, click the *New* drop-down menu and
select "Python 3".under "Notebook" click "Python 3".
select either "Python 2" or "Python 3", depending on which version of Python
you are using (note that the kernel you choose *must* match the kernel where
you installed the vega3 extension).

In the notebook that opens, you can run the following code to ensure everything
is properly set up:
Expand All @@ -96,10 +100,6 @@ is properly set up:
color='species'
)

If the plot does not render, ensure you have installed the exact versions
mentioned above, and if it still does not work see
:ref:`display-troubleshooting` for help.

.. note::

For the classic Jupyter notebook (not JupyterLab), each time you launch a
Expand All @@ -110,6 +110,10 @@ mentioned above, and if it still does not work see
If you neglect this step, charts will not be rendered, but instead
displayed as a textual representation.

If the plot does not render, ensure you have installed the exact versions
mentioned above, and if it still does not work see
:ref:`troubleshooting-notebook` for help.

Once things are up and running, you may wish to go through the tutorials at
:ref:`starting` and :ref:`exploring-weather`, read through the User Guide
indexed in the left panel, or check out the :ref:`example-gallery` for more ideas.
Expand All @@ -126,7 +130,7 @@ following in a notebook cell:
# for colab only run this command once per session
alt.renderers.enable('colab')

With this set up, paste the following code to check if renderings are working
Once you have run this, paste the following code to check if renderings are working
correctly:

.. altair-plot::
Expand Down Expand Up @@ -167,7 +171,7 @@ Dependencies
Altair has the following dependencies, all of which are installed automatically
with the above installation commands:

- python 2.7 or 3.5 or higher
- python 2.7, 3.5 or newer
- entrypoints_
- IPython_
- jsonschema_
Expand Down
7 changes: 5 additions & 2 deletions doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ Altair: Declarative Visualization in Python
:seed: 31415

Altair is a declarative statistical visualization library for Python, based on
Vega_ and Vega-Lite_.
Vega_ and Vega-Lite_, and the source is available on
`GitHub <http://github.com/altair-viz/altair>`_.

With Altair, you can spend more time understanding your data and its meaning.
Altair's API is simple, friendly and consistent and built on top of the
Expand Down Expand Up @@ -56,7 +57,9 @@ beautiful and effective visualizations with a minimal amount of code.
:caption: Advanced Usage

user_guide/importing
user_guide/display
user_guide/display_frontends
user_guide/custom_renderers
user_guide/data_transformers

Bug Reports & Questions
-----------------------
Expand Down
8 changes: 6 additions & 2 deletions doc/user_guide/compound_charts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ In this example, we explicitly loop over different x and y encodings
to create a 2 x 2 grid of charts showing different views of the data.
The code is straightforward, if a bit verbose.

The :class:`RepeatChart` pattern makes this type of chart a bit easier
to produce:
The :class:`RepeatChart` pattern, accessible via the :meth:`Chart.repeat`
method, makes this type of chart a bit easier to produce:

.. altair-plot::

Expand Down Expand Up @@ -324,6 +324,10 @@ layered chart with a hover selection:
data=iris
)

Notice that we specify the data within the facet here; this is important, because
the top-level facet needs access to this data in order to know how to encode
the column.

Though each of the above examples have faceted the data across columns,
faceting across rows (or across rows *and* columns) is supported as
well.
Expand Down
62 changes: 62 additions & 0 deletions doc/user_guide/custom_renderers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

.. _customizing-renderers:

Customizing Renderers
=====================
Renderers in Altair are all based on the mimebundle representation accessed by
the ``_repr_mimebundle_`` method of the top-level Altair objects. When you enable
a renderer, functionally what that does is to define a new kind of mimebundle
output.

The ``alt.renderers`` registry allows the user to define and enable new mimetypes
for the chart.
As a simple example, imagine we would like to add a ``plaintext`` renderer that
renders a chart description in plain text. We could do it this way::

def plaintext_mimetype(spec):
return {'text/plain': "description: " + spec.get('description', 'none')}

alt.renderers.register('plaintext', plaintext_mimetype)

Now you can enable this mimetype, and then when your chart is displayed you
will see this description::

alt.renderers.enable('plaintext')

alt.Chart('data.txt').mark_point().encode(
x='x:Q',
y='y:Q'
).properties(
description='This is a simple chart'
)

.. code-block:: none
This is a simple chart
This is a simple example, but it shows you the flexibility of this approach.
If you have a frontend that recognizes ``_repr_mimebundle_`` as a means of
obtaining a MIME type representation of a Python object, then you can define
a function that will process the chart content in any way before returning
any mimetype.

The renderers built-in to Altair are the following:

- ``"default"``: default rendering, using the
``'application/vnd.vegalite.v2+json'`` MIME type which is supported
by JupyterLab and nteract.
- ``"jupyterlab"``: identical to ``"default"``
- ``"nteract"``: identical to ``"default"``
- ``"colab"``: renderer for Google's Colab notebook, using the
``"text/html"`` MIME type.
- ``"notebook"``: renderer for the classic notebook, provided by the ipyvega3_
package
- ``"json"``: renderer that outputs the raw JSON chart specification, using the
``'application/json'`` MIME type.
- ``"png"``: renderer that renders and converts the chart to PNG, outputting it
using the ``'image/png'`` MIME type.
- ``"svg"``: renderer that renders and converts the chart to an SVG image,
outputting it using the ``'image/svg+xml'`` MIME type.


.. _ipyvega3: https://github.com/vega/ipyvega/tree/vega3
106 changes: 106 additions & 0 deletions doc/user_guide/data_transformers.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
.. _data-transformers:

Data transformers
=================

Before a Vega-Lite or Vega specification can be passed to a renderer, it typically
has to be transformed in a number of ways:

* Pandas Dataframe has to be sanitized and serialized to JSON.
* The rows of a Dataframe might need to be sampled or limited to a maximum number.
* The Dataframe might be written to a ``.csv`` of ``.json`` file for performance
reasons.

These data transformations are managed by the data transformation API of Altair.

.. note::

The data transformation API of Altair should not be confused with the ``transform``
API of Vega and Vega-Lite.

A data transformer is a Python function that takes a Vega-Lite data ``dict`` or
Pandas ``DataFrame`` and returns a transformed version of either of these types::

from typing import Union
Data = Union[dict, pd.DataFrame]

def data_transformer(data: Data) -> Data:
# Transform and return the data
return transformed_data

Built-in data transformers
~~~~~~~~~~~~~~~~~~~~~~~~~~

Altair includes a default set of data transformers with the following signatures.

Raise a ``MaxRowsError`` if a Dataframe has more than ``max_rows`` rows::

limit_rows(data, max_rows=5000)

Randomly sample a DataFrame (without replacement) before visualizing::

sample(data, n=None, frac=None)

Convert a Dataframe to a separate ``.json`` file before visualization::

to_json(data, prefix='altair-data'):

Convert a Dataframe to a separate ``.csv`` file before visualiztion::

to_csv(data, prefix='altair-data'):

Convert a Dataframe to inline JSON values before visualization::

to_values(data):

Piping
~~~~~~

Multiple data transformers can be piped together using ``pipe``::

from altair import pipe, limit_rows, to_values
pipe(data, limit_rows(10000), to_values)

Managing data transformers
~~~~~~~~~~~~~~~~~~~~~~~~~~

Altair maintains a registry of data transformers, which includes a default
data transformer that is automatically applied to all Dataframes before rendering.

To see the registered transformers::

>>> import altair as alt
>>> alt.data_transformers.names()
['default', 'json', 'csv']

The default data transformer is the following::

def default_data_transformer(data):
return pipe(data, limit_rows, to_values)

The ``json`` and ``csv`` data transformers will save a Dataframe to a temporary
``.json`` or ``.csv`` file before rendering. There are a number of performance
advantages to these two data transformers:

* The full dataset will not be saved in the notebook document.
* The performance of the Vega-Lite/Vega JavaScript appears to be better
for standalone JSON/CSV files than for inline values.

There are disadvantages of the JSON/CSV data transformers:

* The Dataframe will be exported to a temporary ``.json`` or ``.csv``
file that sits next to the notebook.
* That notebook will not be able to re-render the visualization without
that temporary file (or re-running the cell).

In our experience, the performance improvement is significant enough that
we recommend using the ``json`` data transformer for any large datasets::

alt.data_transformers.enable('json')

We hope that others will write additional data transformers - imagine a
transformer which saves the dataset to a JSON file on S3, which could
be registered and enabled as::

alt.data_transformers.register('s3', lambda data: pipe(sample, to_s3('mybucket')))
alt.data_transformers.enable('s3')
Loading

0 comments on commit d045cfb

Please sign in to comment.