Skip to content

How to use Neptune with Altair#

Open in Colab

Altair is a declarative statistical visualization library for Python. With Neptune, you can log and display Altair charts as interactive HTML.

Altair chart in Neptune

See in Neptune  Example script 

Before you start#

Tip

To follow the guide without any setup, run the example notebook in Colab

Altair logging example#

  1. Import Neptune and start a run:

    import neptune
    
    run = neptune.init_run() # (1)!
    
    1. If you haven't set up your credentials, you can log anonymously:

      neptune.init_run(
          api_token=neptune.ANONYMOUS_API_TOKEN,
          project="common/plotting",
      )
      
  2. Install vega-datasets to access sample data:

    pip install vega_datasets
    
  3. Create a sample figure using data from vega_datasets.data.cars():

    import altair as alt
    from vega_datasets import data
    
    source = data.cars()
    
    brush = alt.selection_interval()
    
    points = (
        alt.Chart(source)
        .mark_point()
        .encode(
            x="Horsepower:Q",
            y="Miles_per_Gallon:Q",
            color=alt.condition(brush, "Origin:N", alt.value("lightgray")),
        )
        .add_params(brush)
    )
    
    bars = (
        alt.Chart(source)
        .mark_bar()
        .encode(y="Origin:N", color="Origin:N", x="count(Origin):Q")
        .transform_filter(brush)
    )
    
    chart = points & bars
    
  4. Upload the chart:

    run["interactive_img"].upload(chart)
    
  5. To stop the connection to Neptune and sync all data, call the stop() method:

    run.stop()
    
  6. Run your script as you normally would.

To open the run, click the Neptune link that appears in the console output.

[neptune] [info ] Neptune initialized. Open in the app: https://app.neptune.ai/workspace/project/e/RUN-1

Result

The resulting chart is logged as an HTML object.

You can view it in the All metadata section.