mpld3
¶See the mpld3 github for more about mpl3d. See the seaborn docs for more about seaborn.
Last updated December 30, 2013
import numpy as np
from numpy.random import randn
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from mpld3 import display_d3
np.random.seed(0)
def sinplot(flip=1):
x = np.linspace(0, 14, 100)
for i in range(1, 7):
plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)
x1 = randn(80)
x2 = randn(80)
x3 = x1 * x2
y1 = .5 + 2 * x1 - x2 + 2.5 * x3 + 3 * randn(80)
y2 = .5 + 2 * x1 - x2 + 2.5 * randn(80)
y3 = y2 + randn(80)
g = np.random.choice(list("ABC"), 80)
y_logistic = 1 / (1 + np.exp(-y1))
y_flip = [np.random.binomial(1, p) for p in y_logistic]
df = pd.DataFrame(dict(x1=x1, x2=x2, x3=x3, y1=y1, y2=y2, y3=y3, y_flip=y_flip, g=g))
d = randn(100, 30)
sinplot()
sinplot()
display_d3()
sns.set(style="whitegrid")
sinplot()
sinplot()
display_d3()
sns.set(style="nogrid")
sinplot()
sinplot()
display_d3()
sns.set()
sns.lmplot("x1", "y2", df)
fig = plt.gcf()
display_d3(fig)
sns.lmplot("x1", "y2", df, col="g", size=3.5)
fig = plt.gcf()
display_d3(fig)
sns.lmplot("x1", "y2", df, color="g")
fig = plt.gcf()
display_d3(fig)
/Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.py:378: UserWarning: legend is not implemented: it will be ignored
sns.regplot("x1", "y1", df)
fig = plt.gcf()
display_d3(fig)
sns.coefplot("y1 ~ x1 * x2 + g", df)
fig = plt.gcf()
display_d3(fig)
sns.interactplot("x1", "x2", "y1", df)
fig = plt.gcf()
display_d3(fig)
/Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.py:375: UserWarning: artists not implemented. Elements will be ignored
sns.corrplot(df)
fig = plt.gcf()
display_d3(fig)
/Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.py:1022: UserWarning: Not all QuadMesh features are yet implemented
sns.boxplot(df.x1, df.g)
fig = plt.gcf()
display_d3(fig)
sns.violinplot(df.x1, df.g)
fig = plt.gcf()
display_d3(fig)
sns.distplot(df.x1)
fig = plt.gcf()
display_d3(fig)
sns.kdeplot(df.x1, shade=True)
fig = plt.gcf()
display_d3(fig)
sns.kdeplot(df[["x1", "x2"]])
fig = plt.gcf()
display_d3(fig)
sns.kdeplot(df[["x1", "x2"]], shade=True)
fig = plt.gcf()
display_d3(fig)
def random_walk(n, start=0, p_inc=.2):
return start + np.cumsum(np.random.uniform(size=n) < p_inc)
starts = np.random.choice(range(4), 10)
probs = [.1, .3, .5]
walks = np.dstack([[random_walk(15, s, p) for s in starts] for p in probs])
sns.tsplot(walks)
fig = plt.gcf()
display_d3(fig)
sns.tsplot(walks, err_style="ci_bars")
fig = plt.gcf()
display_d3(fig)
def sine_wave(n_x, obs_err_sd=1.5, tp_err_sd=.3):
x = np.linspace(0, (n_x - 1) / 2, n_x)
y = np.sin(x) + np.random.normal(0, obs_err_sd) + np.random.normal(0, tp_err_sd, n_x)
return y
sines = np.array([sine_wave(31) for _ in range(20)])
sns.tsplot(sines, err_style="boot_traces", n_boot=500)
fig = plt.gcf()
display_d3(fig)
sns.tsplot(sines, err_style="boot_kde", n_boot=500)
fig = plt.gcf()
display_d3(fig)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-47-6723e41ae15d> in <module>() ----> 1 display_d3(fig) /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/display.pyc in display_d3(fig, closefig, d3_url) 50 if closefig: 51 plt.close(fig) ---> 52 return HTML(fig_to_d3(fig, d3_url)) 53 54 /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/display.pyc in fig_to_d3(fig, d3_url) 21 the IPython HTML rich display of the figure. 22 """ ---> 23 return D3Figure(fig).html(d3_url) 24 25 /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.pyc in html(self, d3_url, with_d3_import, with_style) 129 result += self.style() 130 --> 131 axes = '\n'.join(ax.html() for ax in self.axes) 132 fig = self.FIGURE_TEMPLATE.format(figid=self.figid, 133 figwidth=self.fig.get_figwidth(), /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.pyc in <genexpr>((ax,)) 129 result += self.style() 130 --> 131 axes = '\n'.join(ax.html() for ax in self.axes) 132 fig = self.FIGURE_TEMPLATE.format(figid=self.figid, 133 figwidth=self.fig.get_figwidth(), /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.pyc in html(self) 392 393 def html(self): --> 394 elements = '\n'.join(c.html() for c in self.children) 395 zooms = '\n'.join(c.zoom() for c in self.children) 396 /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.pyc in <genexpr>((c,)) 392 393 def html(self): --> 394 elements = '\n'.join(c.html() for c in self.children) 395 zooms = '\n'.join(c.zoom() for c in self.children) 396 /Users/mwaskom/anaconda/lib/python2.7/site-packages/mpld3-0.0.1-py2.7.egg/mpld3/_objects.pyc in html(self) 1154 self.x, self.y = 0, 0 1155 data = self.image.get_array().data -> 1156 self.height, self.width = data.shape 1157 1158 binary_buffer = io.BytesIO() ValueError: too many values to unpack