In [1]:
import altair.vega.v3 as vg
from altair.datasets import load_dataset

In [2]:
spec = {
  "$schema": "https://vega.github.io/schema/vega/v3.0.json",
  "width": 400,
  "height": 200,
  "padding": 5,

  "data": [
    {
      "name": "table",
      "values": [
        {"category": "A", "amount": 28},
        {"category": "B", "amount": 55},
        {"category": "C", "amount": 43},
        {"category": "D", "amount": 91},
        {"category": "E", "amount": 81},
        {"category": "F", "amount": 53},
        {"category": "G", "amount": 19},
        {"category": "H", "amount": 87}
      ]
    }
  ],

  "signals": [
    {
      "name": "tooltip",
      "value": {},
      "on": [
        {"events": "rect:mouseover", "update": "datum"},
        {"events": "rect:mouseout",  "update": "{}"}
      ]
    }
  ],

  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "table", "field": "category"},
      "range": "width",
      "padding": 0.05,
      "round": True
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "amount"},
      "nice": True,
      "range": "height"
    }
  ],

  "axes": [
    { "orient": "bottom", "scale": "xscale" },
    { "orient": "left", "scale": "yscale" }
  ],

  "marks": [
    {
      "type": "rect",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {
          "fill": {"value": "steelblue"}
        },
        "hover": {
          "fill": {"value": "red"}
        }
      }
    },
    {
      "type": "text",
      "encode": {
        "enter": {
          "align": {"value": "center"},
          "baseline": {"value": "bottom"},
          "fill": {"value": "#333"}
        },
        "update": {
          "x": {"scale": "xscale", "signal": "tooltip.category", "band": 0.5},
          "y": {"scale": "yscale", "signal": "tooltip.amount", "offset": -2},
          "text": {"signal": "tooltip.amount"},
          "fillOpacity": [
            {"test": "datum === tooltip", "value": 0},
            {"value": 1}
          ]
        }
      }
    }
  ]
}

To render in the classic notebook run this line:


In [3]:
vg.renderers.enable('notebook')

To render in JupyterLab and nteract, run this


In [ ]:
vg.renderers.enable('default')

In [3]:
vg.renderers.get()(spec)


Out[3]:
({'application/vnd.vega.v3+json': {'$schema': 'https://vega.github.io/schema/vega/v3.0.json',
   'axes': [{'orient': 'bottom', 'scale': 'xscale'},
    {'orient': 'left', 'scale': 'yscale'}],
   'data': [{'name': 'table',
     'values': [{'amount': 28, 'category': 'A'},
      {'amount': 55, 'category': 'B'},
      {'amount': 43, 'category': 'C'},
      {'amount': 91, 'category': 'D'},
      {'amount': 81, 'category': 'E'},
      {'amount': 53, 'category': 'F'},
      {'amount': 19, 'category': 'G'},
      {'amount': 87, 'category': 'H'}]}],
   'height': 200,
   'marks': [{'encode': {'enter': {'width': {'band': 1, 'scale': 'xscale'},
       'x': {'field': 'category', 'scale': 'xscale'},
       'y': {'field': 'amount', 'scale': 'yscale'},
       'y2': {'scale': 'yscale', 'value': 0}},
      'hover': {'fill': {'value': 'red'}},
      'update': {'fill': {'value': 'steelblue'}}},
     'from': {'data': 'table'},
     'type': 'rect'},
    {'encode': {'enter': {'align': {'value': 'center'},
       'baseline': {'value': 'bottom'},
       'fill': {'value': '#333'}},
      'update': {'fillOpacity': [{'test': 'datum === tooltip', 'value': 0},
        {'value': 1}],
       'text': {'signal': 'tooltip.amount'},
       'x': {'band': 0.5, 'scale': 'xscale', 'signal': 'tooltip.category'},
       'y': {'offset': -2, 'scale': 'yscale', 'signal': 'tooltip.amount'}}},
     'type': 'text'}],
   'padding': 5,
   'scales': [{'domain': {'data': 'table', 'field': 'category'},
     'name': 'xscale',
     'padding': 0.05,
     'range': 'width',
     'round': True,
     'type': 'band'},
    {'domain': {'data': 'table', 'field': 'amount'},
     'name': 'yscale',
     'nice': True,
     'range': 'height'}],
   'signals': [{'name': 'tooltip',
     'on': [{'events': 'rect:mouseover', 'update': 'datum'},
      {'events': 'rect:mouseout', 'update': '{}'}],
     'value': {}}],
   'width': 400},
  'text/plain': '<Vega 3 object>'},
 {})

In [4]:
vg.vega(spec, validate=True)


<altair.vega.v3.display.Vega at 0x109fbfa90>

In [6]:
vg.renderers.enable('json')

In [7]:
vg.vega(spec, validate=True)


<Vega 3 object>