{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python API to BeakerX Interactive Plotting\n", "\n", "You can access Beaker's native interactive plotting library from Python.\n", "\n", "## Plot with simple properties\n", "\n", "Python plots has syntax very similar to Groovy plots. Property names are the same." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from beakerx import *\n", "import pandas as pd\n", "\n", "tableRows = pd.read_csv('../resources/data/interest-rates.csv')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "Plot(title=\"Title\",\n", " xLabel=\"Horizontal\",\n", " yLabel=\"Vertical\",\n", " initWidth=500,\n", " initHeight=200)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot items\n", "\n", "### Lines, Bars, Points and Right yAxis" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Setting line properties\")\n", "ys = [0, 1, 6, 5, 2, 8]\n", "ys2 = [0, 2, 7, 6, 3, 8]\n", "plot.add(Line(y= ys, width= 10, color= Color.red))\n", "plot.add(Line(y= ys, width= 3, color= Color.yellow))\n", "plot.add(Line(y= ys, width= 4, color= Color(33, 87, 141), style= StrokeType.DASH, interpolation= 0))\n", "plot.add(Line(y= ys2, width= 2, color= Color(212, 57, 59), style= StrokeType.DOT))\n", "plot.add(Line(y= [5, 0], x= [0, 5], style= StrokeType.LONGDASH))\n", "plot.add(Line(y= [4, 0], x= [0, 5], style= StrokeType.DASHDOT))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Changing Point Size, Color, Shape\")\n", "y1 = [6, 7, 12, 11, 8, 14]\n", "y2 = [4, 5, 10, 9, 6, 12]\n", "y3 = [2, 3, 8, 7, 4, 10]\n", "y4 = [0, 1, 6, 5, 2, 8]\n", "plot.add(Points(y= y1))\n", "plot.add(Points(y= y2, shape= ShapeType.CIRCLE))\n", "plot.add(Points(y= y3, size= 8.0, shape= ShapeType.DIAMOND))\n", "plot.add(Points(y= y4, size= 12.0, color= Color.orange, outlineColor= Color.red))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Changing point properties with list\")\n", "cs = [Color.black, Color.red, Color.orange, Color.green, Color.blue, Color.pink]\n", "ss = [6.0, 9.0, 12.0, 15.0, 18.0, 21.0]\n", "fs = [False, False, False, True, False, False]\n", "plot.add(Points(y= [5] * 6, size= 12.0, color= cs))\n", "plot.add(Points(y= [4] * 6, size= 12.0, color= Color.gray, outlineColor= cs))\n", "plot.add(Points(y= [3] * 6, size= ss, color= Color.red))\n", "plot.add(Points(y= [2] * 6, size= 12.0, color= Color.black, fill= fs, outlineColor= Color.black))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot()\n", "y1 = [1.5, 1, 6, 5, 2, 8]\n", "cs = [Color.black, Color.red, Color.gray, Color.green, Color.blue, Color.pink]\n", "ss = [StrokeType.SOLID, StrokeType.SOLID, StrokeType.DASH, StrokeType.DOT, StrokeType.DASHDOT, StrokeType.LONGDASH]\n", "plot.add(Stems(y= y1, color= cs, style= ss, width= 5))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Setting the base of Stems\")\n", "ys = [3, 5, 2, 3, 7]\n", "y2s = [2.5, -1.0, 3.5, 2.0, 3.0]\n", "plot.add(Stems(y= ys, width= 2, base= y2s))\n", "plot.add(Points(y= ys))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Bars\")\n", "cs = [Color(255, 0, 0, 128)] * 5 # transparent bars\n", "cs[3] = Color.red # set color of a single bar, solid colored bar\n", "plot.add(Bars(x= [1, 2, 3, 4, 5], y= [3, 5, 2, 3, 7], color= cs, outlineColor= Color.black, width= 0.3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Lines, Points with Pandas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Pandas line\")\n", "plot.add(Line(y= tableRows.y1, width= 2, color= Color(216, 154, 54)))\n", "plot.add(Line(y= tableRows.y10, width= 2, color= Color.lightGray))\n", "\n", "plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Pandas Series\")\n", "plot.add(Line(y= pd.Series([0, 6, 1, 5, 2, 4, 3]), width=2))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Bars\")\n", "cs = [Color(255, 0, 0, 128)] * 7 # transparent bars\n", "cs[3] = Color.red # set color of a single bar, solid colored bar\n", "plot.add(Bars(pd.Series([0, 6, 1, 5, 2, 4, 3]), color= cs, outlineColor= Color.black, width= 0.3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Areas, Stems and Crosshair" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ch = Crosshair(color=Color.black, 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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot()\n", "y = [3, 5, 2, 3]\n", "x0 = [0, 1, 2, 3]\n", "x1 = [3, 4, 5, 8]\n", "plot.add(Area(x= x0, y= y))\n", "plot.add(Area(x= x1, y= y, color= Color(128, 128, 128, 50), interpolation= 0))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = Plot()\n", "p.add(Line(y= [3, 6, 12, 24], displayName= \"Median\"))\n", "p.add(Area(y= [4, 8, 16, 32], base= [2, 4, 8, 16],\n", " color= Color(255, 0, 0, 50), displayName= \"Q1 to Q3\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ch = Crosshair(color= Color(255, 128, 5), width= 2, style= StrokeType.DOT)\n", "pp = Plot(crosshair= ch, omitCheckboxes= True,\n", " legendLayout= LegendLayout.HORIZONTAL, legendPosition= LegendPosition(position=LegendPosition.Position.TOP))\n", "x = [1, 4, 6, 8, 10]\n", "y = [3, 6, 4, 5, 9]\n", "pp.add(Line(displayName= \"Line\", x= x, y= y, width= 3))\n", "pp.add(Bars(displayName= \"Bar\", x= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], y= [2, 2, 4, 4, 2, 2, 0, 2, 2, 4], width= 0.5))\n", "pp.add(Points(x= x, y= y, size= 10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Constant Lines, Constant Bands" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": {}, "outputs": [], "source": [ "Plot().add(Line(y=[-3, 1, 3, 4, 5])).add(ConstantBand(x=[1, 2], y=[1, 3]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = Plot() \n", "p.add(Line(x= [-3, 1, 2, 4, 5], y= [4, 2, 6, 1, 5]))\n", "p.add(ConstantBand(x= ['-Infinity', 1], color= Color(128, 128, 128, 50)))\n", "p.add(ConstantBand(x= [1, 2]))\n", "p.add(ConstantBand(x= [4, 'Infinity']))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from decimal import Decimal\n", "pos_inf = Decimal('Infinity')\n", "neg_inf = Decimal('-Infinity')\n", "print (pos_inf)\n", "print (neg_inf)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from beakerx.plot import Text as BeakerxText\n", "plot = Plot()\n", "xs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", "ys = [8.6, 6.1, 7.4, 2.5, 0.4, 0.0, 0.5, 1.7, 8.4, 1]\n", "def label(i):\n", " if ys[i] > ys[i+1] and ys[i] > ys[i-1]:\n", " return \"max\"\n", " if ys[i] < ys[i+1] and ys[i] < ys[i-1]:\n", " return \"min\"\n", " if ys[i] > ys[i-1]:\n", " return \"rising\"\n", " if ys[i] < ys[i-1]:\n", " return \"falling\"\n", " return \"\"\n", "\n", "for i in xs:\n", " i = i - 1\n", " if i > 0 and i < len(xs)-1:\n", " plot.add(BeakerxText(x= xs[i], y= ys[i], text= label(i), pointerAngle= -i/3.0))\n", "\n", "plot.add(Line(x= xs, y= ys))\n", "plot.add(Points(x= xs, y= ys))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Setting 2nd Axis bounds\")\n", "ys = [0, 2, 4, 6, 15, 10]\n", "ys2 = [-40, 50, 6, 4, 2, 0]\n", "ys3 = [3, 6, 3, 6, 70, 6]\n", "plot.add(YAxis(label=\"Spread\"))\n", "plot.add(Line(y= ys))\n", "plot.add(Line(y= ys2, yAxis=\"Spread\"))\n", "plot.setXBound([-2, 10])\n", "#plot.setYBound(1, 5)\n", "plot.getYAxes()[0].setBound(1,5)\n", "plot.getYAxes()[1].setBound(3,6)\n", "\n", "\n", "plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Setting 2nd Axis bounds\")\n", "ys = [0, 2, 4, 6, 15, 10]\n", "ys2 = [-40, 50, 6, 4, 2, 0]\n", "ys3 = [3, 6, 3, 6, 70, 6]\n", "plot.add(YAxis(label=\"Spread\"))\n", "plot.add(Line(y= ys))\n", "plot.add(Line(y= ys2, yAxis=\"Spread\"))\n", "plot.setXBound([-2, 10])\n", "plot.setYBound(1, 5)\n", "\n", "plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## TimePlot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = TimePlot()\n", "plot.add(Line(x=tableRows['time'], y=tableRows['m3']))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### numpy datatime64" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\n", "dates = [np.datetime64('2015-02-01'), \n", " np.datetime64('2015-02-02'), \n", " np.datetime64('2015-02-03'),\n", " np.datetime64('2015-02-04'),\n", " np.datetime64('2015-02-05'),\n", " np.datetime64('2015-02-06')]\n", "plot = TimePlot()\n", "\n", "plot.add(Line(x=dates, y=y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Timestamp" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\n", "dates = pd.Series(['2015-02-01',\n", " '2015-02-02',\n", " '2015-02-03',\n", " '2015-02-04',\n", " '2015-02-05',\n", " '2015-02-06']\n", " , dtype='datetime64[ns]')\n", "plot = TimePlot()\n", "plot.add(Line(x=dates, y=y))\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Datetime and date" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import datetime\n", "\n", "y = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\n", "dates = [datetime.date(2015, 2, 1),\n", " datetime.date(2015, 2, 2),\n", " datetime.date(2015, 2, 3),\n", " datetime.date(2015, 2, 4),\n", " datetime.date(2015, 2, 5),\n", " datetime.date(2015, 2, 6)]\n", "plot = TimePlot()\n", "plot.add(Line(x=dates, y=y))\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "import datetime\n", "\n", "y = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\n", "dates = [datetime.datetime(2015, 2, 1),\n", " datetime.datetime(2015, 2, 2),\n", " datetime.datetime(2015, 2, 3),\n", " datetime.datetime(2015, 2, 4),\n", " datetime.datetime(2015, 2, 5),\n", " datetime.datetime(2015, 2, 6)]\n", "plot = TimePlot()\n", "plot.add(Line(x=dates, y=y))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## NanoPlot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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", "nanoplot = NanoPlot()\n", "nanoplot.add(Points(x=xs, y=ys))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Stacking" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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": {}, "outputs": [], "source": [ "SimpleTimePlot(tableRows, [\"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=True, # no lines (true by default)\n", " displayPoints=False) # show points (false by default))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "#time column base on DataFrame index \n", "tableRows.index = tableRows['time']\n", "\n", "SimpleTimePlot(tableRows, ['m3'])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rng = pd.date_range('1/1/2011', periods=72, freq='H')\n", "ts = pd.Series(np.random.randn(len(rng)), index=rng)\n", "df = pd.DataFrame(ts, columns=['y'])\n", "SimpleTimePlot(df, ['y'])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Second Y Axis" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The plot can have two y-axes. Just add a `YAxis` to the plot object, and specify its label.\n", "Then for data that should be scaled according to this second axis,\n", "specify the property `yAxis` with a value that coincides with the label given.\n", "You can use `upperMargin` and `lowerMargin` to restrict the range of the data leaving more white, perhaps for the data on the other axis." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "p = TimePlot(xLabel= \"Time\", yLabel= \"Interest Rates\")\n", "p.add(YAxis(label= \"Spread\", upperMargin= 4))\n", "p.add(Area(x= tableRows.time, y= tableRows.spread, displayName= \"Spread\",\n", " yAxis= \"Spread\", color= Color(180, 50, 50, 128)))\n", "p.add(Line(x= tableRows.time, y= tableRows.m3, displayName= \"3 Month\"))\n", "p.add(Line(x= tableRows.time, y= tableRows.y10, displayName= \"10 Year\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Combined Plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import math\n", "points = 100\n", "logBase = 10\n", "expys = []\n", "xs = []\n", "for i in range(0, points):\n", " xs.append(i / 15.0)\n", " expys.append(math.exp(xs[i]))\n", "\n", "\n", "cplot = CombinedPlot(xLabel= \"Linear\")\n", "logYPlot = Plot(title= \"Linear x, Log y\", yLabel= \"Log\", logY= True, yLogBase= logBase)\n", "logYPlot.add(Line(x= xs, y= expys, displayName= \"f(x) = exp(x)\"))\n", "logYPlot.add(Line(x= xs, y= xs, displayName= \"g(x) = x\"))\n", "cplot.add(logYPlot, 4)\n", "\n", "linearYPlot = Plot(title= \"Linear x, Linear y\", yLabel= \"Linear\")\n", "linearYPlot.add(Line(x= xs, y= expys, displayName= \"f(x) = exp(x)\"))\n", "linearYPlot.add(Line(x= xs, y= xs, displayName= \"g(x) = x\"))\n", "cplot.add(linearYPlot,4)\n", "\n", "cplot\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot = Plot(title= \"Log x, Log y\", xLabel= \"Log\", yLabel= \"Log\",\n", " logX= True, xLogBase= logBase, logY= True, yLogBase= logBase)\n", "\n", "plot.add(Line(x= xs, y= expys, displayName= \"f(x) = exp(x)\"))\n", "plot.add(Line(x= xs, y= xs, displayName= \"f(x) = x\"))\n", "\n", "plot" ] } ], "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.6.2" } }, "nbformat": 4, "nbformat_minor": 1 }