{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Animations of marks and axes can be enabled by setting 'animation_duration' property of the figure" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Line Animations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import bqplot.pyplot as plt\n", "from bqplot import LinearScale, Axis, Lines, Bars, Figure" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "axes_options = {'x': {'label': 'x'}, 'y': {'label': 'y'}}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.arange(100)\n", "y = np.cumsum(np.random.randn(2, 100), axis=1) #two random walks\n", "\n", "fig = plt.figure(animation_duration=1000)\n", "lines = plt.plot(x=x, y=y, colors=['red', 'green'], axes_options=axes_options)\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# update data of the line mark\n", "lines.y = np.cumsum(np.random.randn(2, 100), axis=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Scatter Animations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(animation_duration=1000)\n", "x, y = np.random.rand(2, 20)\n", "scatt = plt.scatter(x, y, colors=['blue'], axes_options=axes_options)\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#data updates\n", "scatt.x = np.random.rand(20) * 10\n", "scatt.y = np.random.rand(20)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Pie Animations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "data = np.random.rand(6)\n", "\n", "fig = plt.figure(animation_duration=1000)\n", "pie = plt.pie(data, radius=180, sort=False, display_labels='outside', display_values=True,\n", " values_format='.0%', labels=list('ABCDEFGHIJ'))\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pie.sizes = np.random.rand(8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "pie.sort = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#make pie a donut\n", "with pie.hold_sync():\n", " pie.radius = 180\n", " pie.inner_radius = 120" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bar animations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "n = 10\n", "x = list('ABCDEFGHIJ')\n", "y1, y2 = np.random.rand(2, n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig = plt.figure(animation_duration=1000)\n", "bar = plt.bar(x, [y1, y2], padding=0.2, type='grouped')\n", "fig" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y1, y2 = np.random.rand(2, n)\n", "bar.y = [y1, y2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple Mark Animations (using the object model)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "xs = LinearScale()\n", "ys1 = LinearScale()\n", "ys2 = LinearScale()\n", "\n", "x = np.arange(20)\n", "y = np.cumsum(np.random.randn(20))\n", "y1 = np.random.rand(20)\n", "\n", "line = Lines(x=x, y=y, scales={'x': xs, 'y': ys1}, colors=['magenta'], marker='square')\n", "bar = Bars(x=x, y=y1, scales={'x': xs, 'y': ys2}, colorpadding=0.2, colors=['steelblue'])\n", "\n", "xax = Axis(scale=xs, label='x', grid_lines='solid')\n", "yax1 = Axis(scale=ys1, orientation='vertical', tick_format='0.1f', label='y', grid_lines='solid')\n", "yax2 = Axis(scale=ys2, orientation='vertical', side='right', tick_format='0.0%', label='y1', grid_lines='none')\n", "\n", "Figure(marks=[bar, line], axes=[xax, yax1, yax2], animation_duration=1000)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# update mark data\n", "line.y = np.cumsum(np.random.randn(20))\n", "bar.y = np.random.rand(20)" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }