Vincent Test

This notebook shows a simple example of using the vincent renderer to convert a simple matplotlib plot to vega via the vincent package.

This converter is still very incomplete, and will be improved with time.


In [1]:
import matplotlib.pyplot as plt
import numpy as np

In [2]:
%matplotlib inline

In [3]:
import vincent
vincent.core.initialize_notebook()



In [4]:
from IPython.display import display, HTML
from vincent.visualization import _vega_t
import random

def display_natural(obj):
    """
    Display a vincent object in IPython
    without forcing it to 100% of the page width
    """
    # the following is adapted from vincent.Visualization._repr_html_
    id = random.randint(0, 2 ** 16)
    html = '<div id="vis%d"></div>' % id
    html += '<script>\n'
    html += _vega_t % (obj.to_json(pretty_print=False), id) + '\n'
    html += '</script>\n'
    return HTML(html)

In [5]:
from mplexporter.renderers import fig_to_vincent

In [6]:
x = np.linspace(0, 10, 100)
y = np.sin(x)

fig, ax = plt.subplots()
ax.plot(x, y)

display_natural(fig_to_vincent(fig))


Out[6]:

In [7]:
np.random.seed(0)
x = np.random.normal(size=50)
y = np.random.normal(size=50)

fig, ax = plt.subplots()
ax.plot(x, y, 'or')

display_natural(fig_to_vincent(fig))


Out[7]:

In [ ]: