{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python3 Plots" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can access Beaker's native interactive plotting library from Python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot with simple properties" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python3 plots has syntax very similar to Groovy plots. Property names are the same." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c1f2c4089cac4bd89f7cdebc7140b477" } }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from beakerx import *\n", "\n", "Plot(title=\"Title\",\n", " xLabel=\"Horizontal\",\n", " yLabel=\"Vertical\",\n", " initWidth=500,\n", " initHeight=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lines, Bars, Points and Right yAxis" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "ename": "AttributeError", "evalue": "'Plot' object has no attribute 'add'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 9\u001b[0m omitCheckboxes=True)\n\u001b[1;32m 10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mpp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0madd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mYAxis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlabel\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Right yAxis\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m pp.add(Bars(displayName=\"Bar\", \n\u001b[1;32m 13\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m7\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAttributeError\u001b[0m: 'Plot' object has no attribute 'add'" ] } ], "source": [ "x = [1, 4, 6, 8, 10]\n", "y = [3, 6, 4, 5, 9]\n", "\n", "pp = Plot(title='Bars, Lines, Points and 2nd yAxis', \n", " xLabel=\"xLabel\", \n", " yLabel=\"yLabel\", \n", " legendLayout=LegendLayout.HORIZONTAL,\n", " legendPosition=LegendPosition(position=LegendPosition.Position.RIGHT),\n", " omitCheckboxes=True)\n", "\n", "pp.add(YAxis(label=\"Right yAxis\"))\n", "pp.add(Bars(displayName=\"Bar\", \n", " x=[1,3,5,7,10], \n", " y=[100, 120,90,100,80], \n", " width=1))\n", "pp.add(Line(displayName=\"Line\", \n", " x=x, \n", " y=y, \n", " width=6, \n", " yAxis=\"Right yAxis\"))\n", "pp.add(Points(x=x, \n", " y=y, \n", " size=10, \n", " shape=ShapeType.DIAMOND,\n", " yAxis=\"Right yAxis\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Areas, Stems and Crosshair" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "ch = Crosshair(color=beaker.prefs['theme']['plotColors'][0], width=2, style=StrokeType.DOT)\n", "plot = Plot(crosshair=ch)\n", "y1 = [4, 8, 16, 20, 32]\n", "base = [2, 4, 8, 10, 16]\n", "cs = [Color.black, Color.orange, Color.gray, Color.yellow, Color.pink]\n", "ss = [StrokeType.SOLID, \n", " StrokeType.SOLID, \n", " StrokeType.DASH, \n", " StrokeType.DOT, \n", " StrokeType.DASHDOT, \n", " StrokeType.LONGDASH]\n", "plot.add(Area(y=y1, base=base, color=Color(255, 0, 0, 50)))\n", "plot.add(Stems(y=y1, base=base, color=cs, style=ss, width=5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Constant Lines, Constant Bands" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "p = Plot ()\n", "p.add(Line(y=[-1, 1]))\n", "p.add(ConstantLine(x=0.65, style=StrokeType.DOT, color=Color.blue))\n", "p.add(ConstantLine(y=0.1, style=StrokeType.DASHDOT, color=Color.blue))\n", "p.add(ConstantLine(x=0.3, y=0.4, color=Color.gray, width=5, showLabel=True))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Plot().add(Line(y=[-3, 1, 3, 4, 5])).add(ConstantBand(x=[1, 2], y=[1, 3]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TimePlot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import time\n", "\n", "millis=current_milli_time()\n", "\n", "hour = round(1000 * 60 * 60);\n", "xs = [];\n", "ys = [];\n", "for i in range(11):\n", " xs.append(millis + hour * i);\n", " ys.append(i);\n", "\n", "plot = TimePlot(timeZone=\"America/New_York\")\n", "# list of milliseconds\n", "plot.add(Points(x=xs, y=ys, size=10, displayName=\"milliseconds\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NanoPlot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "millis = current_milli_time()\n", "nanos = millis * 1000 * 1000\n", "xs = []\n", "ys = []\n", "for i in range(11):\n", " xs.append(nanos + 7 * i)\n", " ys.append(i);\n", "\n", "np = NanoPlot()\n", "np.add(Points(x=xs, y=ys))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stacking" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "y1 = [1,5,3,2,3]\n", "y2 = [7,2,4,1,3]\n", "p = Plot(title='Plot with XYStacker', initHeight=200)\n", "a1 = Area(y=y1, displayName='y1')\n", "a2 = Area(y=y2, displayName='y2')\n", "stacker = XYStacker()\n", "p.add(stacker.stack([a1, a2]))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## SimpleTime Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "SimpleTimePlot(beaker.rates, [\"y1\", \"y10\"])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "SimpleTimePlot(beaker.rates, [\"y1\", \"y10\"], # column names\n", " timeColumn=\"time\", # time is default value for a timeColumn\n", " yLabel=\"Price\", \n", " displayNames=[\"1 Year\", \"10 Year\"],\n", " colors = [[216, 154, 54], Color.lightGray],\n", " displayLines=False, # no lines (true by default)\n", " displayPoints=True) # show points (false by default))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combined Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# The CombinedPlot allows for stacked plots with linked X axis.\n", "c = CombinedPlot(title=\"US Treasuries\", initWidth=1000)\n", "\n", "p1 = TimePlot(yLabel=\"Short Term\")\n", "p1.add(Line(x=beaker.time, y=beaker.m3, displayName=\"3 month\"))\n", "\n", "p2 = TimePlot(yLabel=\"Long Term\")\n", "p2.add(Line(x=beaker.time, y=beaker.y10, displayName=\"10 Year\"))\n", "\n", "c.add(p1, 1).add(p2, 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Demo data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "rates = beaker.rates\n", "'ok'" ] } ], "metadata": { "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.5.3" } }, "nbformat": 4, "nbformat_minor": 2 }