!pip install six, pytz, dateutil, Flask, Redis !pip install numpy, scipy, statsmodels, patsy, pandas, networkx !pip install matplotlib, mpld3, seaborn, bokeh, rpy2 #git clone https://github.com/vispy/vispy.git #cd vispy #python setup.py install %pylab inline import numpy as np import pandas as pd matplotlib.rcParams['figure.figsize'] = 15, 5 #set default image size for this interactive session matplotlib.rcParams.update({'font.size': 16, 'font.family': 'serif'}) #update the matplotlib configuration parameters x = linspace(0, 5, 10) y = x ** 2 fig, ax1 = subplots() ax1.plot(x, x**2, lw=2, color="blue", label="test") ax1.set_ylabel(r"area $(m^2)$", fontsize=18, color="blue") for label in ax1.get_yticklabels(): label.set_color("blue") ax2 = ax1.twinx() ax2.plot(x, x**3, lw=2, color="red", label="test") ax2.set_ylabel(r"volume $(m^3)$", fontsize=18, color="red") for label in ax2.get_yticklabels(): label.set_color("red") n = array([0,1,2,3,4,5]) xx = np.linspace(-0.75, 1., 100) fig, axes = subplots(1, 4, figsize=(15,5)) axes[0].scatter(xx, xx + 0.25*randn(len(xx)), label="scatter") axes[1].step(n, n**2, lw=2, label="step") axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5, label="bar") axes[3].fill_between(x, x**2, x**3, color="green", alpha=0.5); axes[0].legend(loc=2); # upper left corner axes[1].legend(loc=2); axes[2].legend(loc=2); from IPython.html.widgets import interact import networkx as nx matplotlib.rcParams['figure.figsize'] = 5, 5 # wrap a few graph generation functions so they have the same signature def random_lobster(n, m, k, p): return nx.random_lobster(n, p, p / m) def powerlaw_cluster(n, m, k, p): return nx.powerlaw_cluster_graph(n, m, p) def erdos_renyi(n, m, k, p): return nx.erdos_renyi_graph(n, p) def newman_watts_strogatz(n, m, k, p): return nx.newman_watts_strogatz_graph(n, k, p) def plot_random_graph(n, m, k, p, generator): g = generator(n, m, k, p) nx.draw(g) plt.show() interact(plot_random_graph, n=(2,30), m=(1,10), k=(1,10), p=(0.0, 1.0, 0.001), generator={'lobster': random_lobster, 'power law': powerlaw_cluster, 'Newman-Watts-Strogatz': newman_watts_strogatz, u'Erdős-Rényi': erdos_renyi, }); from matplotlib.backends.backend_pdf import PdfPages with PdfPages('polarplot.pdf') as pdf: # polar plot using add_axes and polar projection fig = plt.figure(figsize=(5,5)) ax = fig.add_axes([0.0, 0.0, .6, .6], polar=True) t = linspace(0, 2 * pi, 100) ax.plot(t, t, color='blue', lw=3); pdf.savefig(fig) import seaborn as sns from scipy import stats, optimize #Diverging: useful when data has natural, meaningful break-point #like correlation values which are spread around zero sns.palplot(sns.color_palette("coolwarm", 7)) #Qualitative: useful when data is categorical sns.palplot(sns.color_palette("Set2", 10)) sns.palplot(sns.color_palette("cubehelix", 8)) sns.palplot(sns.cubehelix_palette(8, start=2)) x = stats.gamma(3).rvs(5000) y = stats.gamma(5).rvs(5000) with sns.axes_style("white"): sns.jointplot(x, y, kind="hex", color="#4CB391"); #change kind="reg" to show regression line d1 = stats.norm(0, 5).rvs(100) d2 = np.concatenate([stats.gamma(4).rvs(50), -1 * stats.gamma(4).rvs(50)]) data = pd.DataFrame(dict(d1=d1, d2=d2)) data = pd.melt(data.ix[:50], value_name="y", var_name="group") f, (ax_l, ax_r) = plt.subplots(1, 2) sns.violinplot(data.y, data.group, "points", positions=[1, 2], color="RdBu", ax=ax_l) sns.violinplot(data.y, data.group, "stick", positions=[3, 4], color="PRGn", ax=ax_r) plt.tight_layout() sns.reset_orig import mpld3 from mpld3 import plugins, utils fig, ax = plt.subplots(3, 3, figsize=(6, 6)) fig.subplots_adjust(hspace=0.1, wspace=0.1) ax = ax[::-1] X = np.random.normal(size=(3, 100)) for i in range(3): for j in range(3): ax[i, j].xaxis.set_major_formatter(plt.NullFormatter()) ax[i, j].yaxis.set_major_formatter(plt.NullFormatter()) points = ax[i, j].scatter(X[j], X[i]) plugins.connect(fig, plugins.LinkedBrush(points)) mpld3.display() class LinkedView(plugins.PluginBase): """A simple plugin showing how multiple axes can be linked""" JAVASCRIPT = """ mpld3.register_plugin("linkedview", LinkedViewPlugin); LinkedViewPlugin.prototype = Object.create(mpld3.Plugin.prototype); LinkedViewPlugin.prototype.constructor = LinkedViewPlugin; LinkedViewPlugin.prototype.requiredProps = ["idpts", "idline", "data"]; LinkedViewPlugin.prototype.defaultProps = {} function LinkedViewPlugin(fig, props){ mpld3.Plugin.call(this, fig, props); }; LinkedViewPlugin.prototype.draw = function(){ var pts = mpld3.get_element(this.props.idpts); var line = mpld3.get_element(this.props.idline); var data = this.props.data; function mouseover(d, i){ line.data = data[i]; line.elements().transition() .attr("d", line.datafunc(line.data)) .style("stroke", this.style.fill); } pts.elements().on("mouseover", mouseover); }; """ def __init__(self, points, line, linedata): if isinstance(points, matplotlib.lines.Line2D): suffix = "pts" else: suffix = None self.dict_ = {"type": "linkedview", "idpts": utils.get_id(points, suffix), "idline": utils.get_id(line), "data": linedata} fig, ax = plt.subplots(2) # scatter periods and amplitudes np.random.seed(0) P = 0.2 + np.random.random(size=20) A = np.random.random(size=20) x = np.linspace(0, 10, 100) data = np.array([[x, Ai * np.sin(x / Pi)] for (Ai, Pi) in zip(A, P)]) points = ax[1].scatter(P, A, c=P + A, s=200, alpha=0.5) ax[1].set_xlabel('Period') ax[1].set_ylabel('Amplitude') # create the line object lines = ax[0].plot(x, 0 * x, '-w', lw=3, alpha=0.5) ax[0].set_ylim(-1, 1) ax[0].set_title("Hover over points to see lines") # transpose line data and add plugin linedata = data.transpose(0, 2, 1).tolist() plugins.connect(fig, LinkedView(points, lines[0], linedata)) mpld3.display() from __future__ import division from collections import OrderedDict from six.moves import zip from bokeh.plotting import * from bokeh.objects import Range1d, ColumnDataSource, HoverTool from bokeh.sampledata.unemployment1948 import data output_notebook() # Lets plot 4000 circles, you can play around with this if you like N = 4000 # Create a bunch of random points, radii and colors for plotting x = np.random.random(size=N) * 100 y = np.random.random(size=N) * 100 radii = np.random.random(size=N) * 1.5 colors = ["#%02x%02x%02x" % (r, g, 150) for r, g in zip(np.floor(50+2*x), np.floor(30+2*y))] figure() hold() circle(x, y, radius=radii, fill_color=colors, fill_alpha=0.6, line_color=None, title="Colorful Scatter") show() # Read in the data with pandas. Convert the year column to string data['Year'] = [str(x) for x in data['Year']] years = list(data['Year']) months = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"] data = data.set_index('Year') colors = ["#75968f", "#a5bab7", "#c9d9d3", "#e2e2e2", "#dfccce", "#ddb7b1", "#cc7878", "#933b41", "#550b1d"] # need to have values for every pair of year/month names, map the rate to a color month = [] year = [] color = [] rate = [] for y in years: for m in months: month.append(m) year.append(y) monthly_rate = data[m][y] rate.append(monthly_rate) color.append(colors[min(int(monthly_rate)-2, 8)]) # create a `ColumnDataSource` with columns: month, year, color, rate source = ColumnDataSource( data=dict( month=month, year=year, color=color, rate=rate, ) ) figure() # x_range is years, y_range is months (reversed) rect('year', 'month', 0.95, 0.95, source=source, x_range=years, y_range=list(reversed(months)), color='color', line_color=None, tools="resize,hover", title="US Unemployment (1948 - 2013)", plot_width=900, plot_height=400) # remove axis and grid lines, remove major ticks, make tick labels smaller, set x-axis orientation to angled grid().grid_line_color = None axis().axis_line_color = None axis().major_tick_line_color = None axis().major_label_text_font_size = "5pt" axis().major_label_standoff = 0 xaxis().major_label_orientation = np.pi/3 #configure the hover tool to display the month, year and rate hover = [t for t in curplot().tools if isinstance(t, HoverTool)][0] hover.tooltips = OrderedDict([ ('date', '@month @year'), ('rate', '@rate'), ]) show() import rpy2 %load_ext rpy2.ipython ##### A random sample from a few normal distributions ##### #%R install.packages("bcp") %R library(bcp) %R testdata <- c(rnorm(50), rnorm(50, 5, 1), rnorm(50)) %R bcp.0 <- bcp(testdata) %R plot.bcp(bcp.0) %R legacyplot(bcp.0) import time, sys from timeit import default_timer import numpy as np from vispy import app, use, gloo, scene from vispy.util.transforms import perspective, translate, rotate, ortho from vispy.geometry import create_cube from vispy.io import load_data_file from vispy.gloo import (Program, VertexBuffer, IndexBuffer, Texture2D, clear, FrameBuffer, DepthBuffer, set_viewport, set_state) #use('ipynb_static') use('ipynb_vnc') render_vertex = """ attribute vec2 position; attribute vec2 texcoord; varying vec2 v_texcoord; void main() { gl_Position = vec4(position, 0.0, 1.0); v_texcoord = texcoord; } """ render_fragment = """ uniform int pingpong; uniform sampler2D texture; varying vec2 v_texcoord; void main() { float v; v = texture2D(texture, v_texcoord)[pingpong]; gl_FragColor = vec4(1.0-v, 1.0-v, 1.0-v, 1.0); } """ compute_vertex = """ attribute vec2 position; attribute vec2 texcoord; varying vec2 v_texcoord; void main() { gl_Position = vec4(position, 0.0, 1.0); v_texcoord = texcoord; } """ compute_fragment = """ uniform int pingpong; uniform sampler2D texture; uniform float dx; // horizontal distance between texels uniform float dy; // vertical distance between texels varying vec2 v_texcoord; void main(void) { vec2 p = v_texcoord; float old_state, new_state, count; old_state = texture2D(texture, p)[pingpong]; count = texture2D(texture, p + vec2(-dx,-dy))[pingpong] + texture2D(texture, p + vec2( dx,-dy))[pingpong] + texture2D(texture, p + vec2(-dx, dy))[pingpong] + texture2D(texture, p + vec2( dx, dy))[pingpong] + texture2D(texture, p + vec2(-dx, 0.0))[pingpong] + texture2D(texture, p + vec2( dx, 0.0))[pingpong] + texture2D(texture, p + vec2(0.0,-dy))[pingpong] + texture2D(texture, p + vec2(0.0, dy))[pingpong]; new_state = old_state; if( old_state > 0.5 ) { // Any live cell with fewer than two live neighbours dies // as if caused by under-population. if( count < 1.5 ) new_state = 0.0; // Any live cell with two or three live neighbours // lives on to the next generation. // Any live cell with more than three live neighbours dies, // as if by overcrowding. else if( count > 3.5 ) new_state = 0.0; } else { // Any dead cell with exactly three live neighbours becomes // a live cell, as if by reproduction. if( (count > 2.5) && (count < 3.5) ) new_state = 1.0; } if( pingpong == 0) { gl_FragColor[1] = new_state; gl_FragColor[0] = old_state; } else { gl_FragColor[1] = old_state; gl_FragColor[0] = new_state; } } """ class Canvas(app.Canvas): def __init__(self): app.Canvas.__init__(self, title="Conway game of life", size=(512, 512), keys='interactive') self._timer = app.Timer('auto', connect=self.update, start=True) def on_initialize(self, event): # Build programs # -------------- self.comp_size = (512, 512) size = self.comp_size + (4,) Z = np.zeros(size, dtype=np.float32) Z[...] = np.random.randint(0, 2, size) Z[:256, :256, :] = 0 gun = """ ........................O........... ......................O.O........... ............OO......OO............OO ...........O...O....OO............OO OO........O.....O...OO.............. OO........O...O.OO....O.O........... ..........O.....O.......O........... ...........O...O.................... ............OO......................""" x, y = 0, 0 for i in range(len(gun)): if gun[i] == '\n': y += 1 x = 0 elif gun[i] == 'O': Z[y, x] = 1 x += 1 self.pingpong = 1 self.compute = Program(compute_vertex, compute_fragment, 4) self.compute["texture"] = Z self.compute["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.compute["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.compute['dx'] = 1.0 / size[1] self.compute['dy'] = 1.0 / size[0] self.compute['pingpong'] = self.pingpong self.render = Program(render_vertex, render_fragment, 4) self.render["position"] = [(-1, -1), (-1, +1), (+1, -1), (+1, +1)] self.render["texcoord"] = [(0, 0), (0, 1), (1, 0), (1, 1)] self.render["texture"] = self.compute["texture"] self.render['pingpong'] = self.pingpong self.fbo = FrameBuffer(self.compute["texture"], DepthBuffer(self.comp_size)) set_state(depth_test=False, clear_color='black') def on_draw(self, event): with self.fbo: set_viewport(0, 0, *self.comp_size) self.compute["texture"].interpolation = 'nearest' self.compute.draw('triangle_strip') clear() set_viewport(0, 0, *self.size) self.render["texture"].interpolation = 'linear' self.render.draw('triangle_strip') self.pingpong = 1 - self.pingpong self.compute["pingpong"] = self.pingpong self.render["pingpong"] = self.pingpong def on_reshape(self, event): set_viewport(0, 0, *event.size) canvas = Canvas() canvas.show() canvas.close()