SHARE
TWEET

Untitled

a guest Aug 19th, 2019 80 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {"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.7.3"},"widgets":{"application/vnd.jupyter.widget-state+json":{"version_major":2,"version_minor":0,"state":{"bf15d9de44c64b54ab7d4a02aa109098":{"model_name":"LayoutModel","model_module":"@jupyter-widgets/base","model_module_version":"*","state":{"_model_module_version":"*","_view_module_version":"*"}},"6db08c7d310048888a8ea652347c41a2":{"model_name":"PlotModel","model_module":"beakerx","model_module_version":"*","state":{"layout":"IPY_MODEL_bf15d9de44c64b54ab7d4a02aa109098","model":{"NUMBER_OF_POINTS_TO_DISPLAY":"numberOfPointsToDisplay","ROWS_LIMIT_ITEMS":"rowsLimitItems","TOO_MANY_ROWS":"tooManyRows","TOTAL_NUMBER_OF_POINTS":"totalNumberOfPoints","chart_title":"Title","constant_bands":[],"constant_lines":[],"domain_axis_label":"Horizontal","graphics_list":[],"init_height":200,"init_width":500,"legend_layout":"VERTICAL","legend_position":{"position":"TOP_RIGHT"},"log_x":false,"log_y":false,"omit_checkboxes":false,"rangeAxes":[{"auto_range":true,"auto_range_includes_zero":false,"label":"","log_base":10,"lower_bound":0,"lower_margin":0.05,"type":"YAxis","upper_bound":0,"upper_margin":0.05,"use_log":false}],"texts":[],"type":"Plot","use_tool_tip":true,"x_auto_range":true,"x_log_base":10,"x_lower_bound":0,"x_lower_margin":0.05,"x_upper_bound":0,"x_upper_margin":0.05,"y_label":"Vertical","numberOfPoints":null,"outputPointsLimit":1000000,"outputPointsPreviewNumber":10000,"tips":{}}}}}}}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Python API to BeakerX Interactive Plotting\n\nYou can access Beaker's native interactive plotting library from Python.\n\n## Plot with simple properties\n\nPython plots has syntax very similar to Groovy plots. Property names are the same.","metadata":{}},{"cell_type":"code","source":"from beakerx import *\nimport pandas as pd\n\ntableRows = pd.read_csv('../resources/data/interest-rates.csv')","metadata":{"trusted":true},"execution_count":1,"outputs":[]},{"cell_type":"code","source":"Plot(title=\"Title\",\n     xLabel=\"Horizontal\",\n     yLabel=\"Vertical\",\n     initWidth=500,\n     initHeight=200)","metadata":{"trusted":true},"execution_count":2,"outputs":[{"output_type":"display_data","data":{"application/vnd.jupyter.widget-view+json":{"version_major":2,"version_minor":0,"model_id":"6db08c7d310048888a8ea652347c41a2"}},"metadata":{}}]},{"cell_type":"markdown","source":"## Plot items\n\n### Lines, Bars, Points and Right yAxis","metadata":{}},{"cell_type":"code","source":"x = [1, 4, 6, 8, 10]\ny = [3, 6, 4, 5, 9]\n\npp = 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\npp.add(YAxis(label=\"Right yAxis\"))\npp.add(Bars(displayName=\"Bar\", \n       x=[1,3,5,7,10], \n       y=[100, 120,90,100,80], \n       width=1))\npp.add(Line(displayName=\"Line\", \n            x=x, \n            y=y, \n            width=6, \n            yAxis=\"Right yAxis\"))\npp.add(Points(x=x, \n              y=y, \n              size=10, \n              shape=ShapeType.DIAMOND,\n              yAxis=\"Right yAxis\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Setting line properties\")\nys = [0, 1, 6, 5, 2, 8]\nys2 = [0, 2, 7, 6, 3, 8]\nplot.add(Line(y= ys, width= 10, color= Color.red))\nplot.add(Line(y= ys, width= 3, color= Color.yellow))\nplot.add(Line(y= ys, width= 4, color= Color(33, 87, 141), style= StrokeType.DASH, interpolation= 0))\nplot.add(Line(y= ys2, width= 2, color= Color(212, 57, 59), style= StrokeType.DOT))\nplot.add(Line(y= [5, 0], x= [0, 5], style= StrokeType.LONGDASH))\nplot.add(Line(y= [4, 0], x= [0, 5], style= StrokeType.DASHDOT))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Changing Point Size, Color, Shape\")\ny1 = [6, 7, 12, 11, 8, 14]\ny2 = [4, 5, 10, 9, 6, 12]\ny3 = [2, 3, 8, 7, 4, 10]\ny4 = [0, 1, 6, 5, 2, 8]\nplot.add(Points(y= y1))\nplot.add(Points(y= y2, shape= ShapeType.CIRCLE))\nplot.add(Points(y= y3, size= 8.0, shape= ShapeType.DIAMOND))\nplot.add(Points(y= y4, size= 12.0, color= Color.orange, outlineColor= Color.red))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Changing point properties with list\")\ncs = [Color.black, Color.red, Color.orange, Color.green, Color.blue, Color.pink]\nss = [6.0, 9.0, 12.0, 15.0, 18.0, 21.0]\nfs = [False, False, False, True, False, False]\nplot.add(Points(y= [5] * 6, size= 12.0, color= cs))\nplot.add(Points(y= [4] * 6, size= 12.0, color= Color.gray, outlineColor= cs))\nplot.add(Points(y= [3] * 6, size= ss, color= Color.red))\nplot.add(Points(y= [2] * 6, size= 12.0, color= Color.black, fill= fs, outlineColor= Color.black))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot()\ny1 = [1.5, 1, 6, 5, 2, 8]\ncs = [Color.black, Color.red, Color.gray, Color.green, Color.blue, Color.pink]\nss = [StrokeType.SOLID, StrokeType.SOLID, StrokeType.DASH, StrokeType.DOT, StrokeType.DASHDOT, StrokeType.LONGDASH]\nplot.add(Stems(y= y1, color= cs, style= ss, width= 5))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Setting the base of Stems\")\nys = [3, 5, 2, 3, 7]\ny2s = [2.5, -1.0, 3.5, 2.0, 3.0]\nplot.add(Stems(y= ys, width= 2, base= y2s))\nplot.add(Points(y= ys))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Bars\")\ncs = [Color(255, 0, 0, 128)] * 5 # transparent bars\ncs[3] = Color.red # set color of a single bar, solid colored bar\nplot.add(Bars(x= [1, 2, 3, 4, 5], y= [3, 5, 2, 3, 7], color= cs, outlineColor= Color.black, width= 0.3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Lines,  Points with Pandas","metadata":{}},{"cell_type":"code","source":"plot = Plot(title= \"Pandas line\")\nplot.add(Line(y= tableRows.y1, width= 2, color= Color(216, 154, 54)))\nplot.add(Line(y= tableRows.y10, width= 2, color= Color.lightGray))\n\nplot","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Pandas Series\")\nplot.add(Line(y= pd.Series([0, 6, 1, 5, 2, 4, 3]), width=2))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Bars\")\ncs = [Color(255, 0, 0, 128)] * 7 # transparent bars\ncs[3] = Color.red # set color of a single bar, solid colored bar\nplot.add(Bars(pd.Series([0, 6, 1, 5, 2, 4, 3]), color= cs, outlineColor= Color.black, width= 0.3))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Areas, Stems and Crosshair","metadata":{}},{"cell_type":"code","source":"ch = Crosshair(color=Color.black, width=2, style=StrokeType.DOT)\nplot = Plot(crosshair=ch)\ny1 = [4, 8, 16, 20, 32]\nbase = [2, 4, 8, 10, 16]\ncs = [Color.black, Color.orange, Color.gray, Color.yellow, Color.pink]\nss = [StrokeType.SOLID, \n      StrokeType.SOLID, \n      StrokeType.DASH, \n      StrokeType.DOT, \n      StrokeType.DASHDOT, \n      StrokeType.LONGDASH]\nplot.add(Area(y=y1, base=base, color=Color(255, 0, 0, 50)))\nplot.add(Stems(y=y1, base=base, color=cs, style=ss, width=5))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot()\ny = [3, 5, 2, 3]\nx0 = [0, 1, 2, 3]\nx1 = [3, 4, 5, 8]\nplot.add(Area(x= x0, y= y))\nplot.add(Area(x= x1, y= y, color= Color(128, 128, 128, 50), interpolation= 0))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = Plot()\np.add(Line(y= [3, 6, 12, 24], displayName= \"Median\"))\np.add(Area(y= [4, 8, 16, 32], base= [2, 4, 8, 16],\n              color= Color(255, 0, 0, 50), displayName= \"Q1 to Q3\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ch = Crosshair(color= Color(255, 128, 5), width= 2, style= StrokeType.DOT)\npp = Plot(crosshair= ch, omitCheckboxes= True,\n                  legendLayout= LegendLayout.HORIZONTAL, legendPosition= LegendPosition(position=LegendPosition.Position.TOP))\nx = [1, 4, 6, 8, 10]\ny = [3, 6, 4, 5, 9]\npp.add(Line(displayName= \"Line\", x= x, y= y, width= 3))\npp.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))\npp.add(Points(x= x, y= y, size= 10))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Constant Lines, Constant Bands","metadata":{}},{"cell_type":"code","source":"p = Plot ()\np.add(Line(y=[-1, 1]))\np.add(ConstantLine(x=0.65, style=StrokeType.DOT, color=Color.blue))\np.add(ConstantLine(y=0.1, style=StrokeType.DASHDOT, color=Color.blue))\np.add(ConstantLine(x=0.3, y=0.4, color=Color.gray, width=5, showLabel=True))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Plot().add(Line(y=[-3, 1, 3, 4, 5])).add(ConstantBand(x=[1, 2], y=[1, 3]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p = Plot() \np.add(Line(x= [-3, 1, 2, 4, 5], y= [4, 2, 6, 1, 5]))\np.add(ConstantBand(x= ['-Infinity', 1], color= Color(128, 128, 128, 50)))\np.add(ConstantBand(x= [1, 2]))\np.add(ConstantBand(x= [4, 'Infinity']))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from decimal import Decimal\npos_inf = Decimal('Infinity')\nneg_inf = Decimal('-Infinity')\nprint (pos_inf)\nprint (neg_inf)\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from beakerx.plot import Text as BeakerxText\nplot = Plot()\nxs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\nys = [8.6, 6.1, 7.4, 2.5, 0.4, 0.0, 0.5, 1.7, 8.4, 1]\ndef 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\nfor 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\nplot.add(Line(x= xs, y= ys))\nplot.add(Points(x= xs, y= ys))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Setting 2nd Axis bounds\")\nys = [0, 2, 4, 6, 15, 10]\nys2 = [-40, 50, 6, 4, 2, 0]\nys3 = [3, 6, 3, 6, 70, 6]\nplot.add(YAxis(label=\"Spread\"))\nplot.add(Line(y= ys))\nplot.add(Line(y= ys2, yAxis=\"Spread\"))\nplot.setXBound([-2, 10])\n#plot.setYBound(1, 5)\nplot.getYAxes()[0].setBound(1,5)\nplot.getYAxes()[1].setBound(3,6)\n\n\nplot","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Setting 2nd Axis bounds\")\nys = [0, 2, 4, 6, 15, 10]\nys2 = [-40, 50, 6, 4, 2, 0]\nys3 = [3, 6, 3, 6, 70, 6]\nplot.add(YAxis(label=\"Spread\"))\nplot.add(Line(y= ys))\nplot.add(Line(y= ys2, yAxis=\"Spread\"))\nplot.setXBound([-2, 10])\nplot.setYBound(1, 5)\n\nplot","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## TimePlot","metadata":{}},{"cell_type":"code","source":"import time\n\nmillis = current_milli_time()\n\nhour = round(1000 * 60 * 60)\nxs = []\nys = []\nfor i in range(11):\n    xs.append(millis + hour * i)\n    ys.append(i)\n\nplot = TimePlot(timeZone=\"America/New_York\")\n# list of milliseconds\nplot.add(Points(x=xs, y=ys, size=10, displayName=\"milliseconds\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = TimePlot()\nplot.add(Line(x=tableRows['time'], y=tableRows['m3']))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### numpy datatime64","metadata":{}},{"cell_type":"code","source":"y = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\ndates = [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')]\nplot = TimePlot()\n\nplot.add(Line(x=dates, y=y))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Timestamp","metadata":{}},{"cell_type":"code","source":"y = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\ndates = 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]')\nplot = TimePlot()\nplot.add(Line(x=dates, y=y))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Datetime and date","metadata":{}},{"cell_type":"code","source":"import datetime\n\ny = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\ndates = [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)]\nplot = TimePlot()\nplot.add(Line(x=dates, y=y))\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datetime\n\ny = pd.Series([7.5, 7.9, 7, 8.7, 8, 8.5])\ndates = [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)]\nplot = TimePlot()\nplot.add(Line(x=dates, y=y))","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## NanoPlot","metadata":{}},{"cell_type":"code","source":"millis = current_milli_time()\nnanos  = millis * 1000 * 1000\nxs = []\nys = []\nfor i in range(11):\n    xs.append(nanos + 7 * i)\n    ys.append(i)\n\nnanoplot = NanoPlot()\nnanoplot.add(Points(x=xs, y=ys))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Stacking","metadata":{}},{"cell_type":"code","source":"y1 = [1,5,3,2,3]\ny2 = [7,2,4,1,3]\np = Plot(title='Plot with XYStacker', initHeight=200)\na1 = Area(y=y1, displayName='y1')\na2 = Area(y=y2, displayName='y2')\nstacker = XYStacker()\np.add(stacker.stack([a1, a2]))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"##  SimpleTime Plot","metadata":{}},{"cell_type":"code","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))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#time column base on DataFrame index \ntableRows.index = tableRows['time']\n\nSimpleTimePlot(tableRows, ['m3'])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rng = pd.date_range('1/1/2011', periods=72, freq='H')\nts = pd.Series(np.random.randn(len(rng)), index=rng)\ndf = pd.DataFrame(ts, columns=['y'])\nSimpleTimePlot(df, ['y'])\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Second Y Axis","metadata":{}},{"cell_type":"markdown","source":"The plot can have two y-axes. Just add a `YAxis` to the plot object, and specify its label.\nThen for data that should be scaled according to this second axis,\nspecify the property `yAxis` with a value that coincides with the label given.\nYou can use `upperMargin` and `lowerMargin` to restrict the range of the data leaving more white, perhaps for the data on the other axis.","metadata":{}},{"cell_type":"code","source":"p = TimePlot(xLabel= \"Time\", yLabel= \"Interest Rates\")\np.add(YAxis(label= \"Spread\", upperMargin= 4))\np.add(Area(x= tableRows.time, y= tableRows.spread, displayName= \"Spread\",\n              yAxis= \"Spread\", color= Color(180, 50, 50, 128)))\np.add(Line(x= tableRows.time, y= tableRows.m3, displayName= \"3 Month\"))\np.add(Line(x= tableRows.time, y= tableRows.y10, displayName= \"10 Year\"))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Combined Plot","metadata":{}},{"cell_type":"code","source":"import math\npoints = 100\nlogBase = 10\nexpys = []\nxs = []\nfor i in range(0, points):\n  xs.append(i / 15.0)\n  expys.append(math.exp(xs[i]))\n\n\ncplot = CombinedPlot(xLabel= \"Linear\")\nlogYPlot = Plot(title= \"Linear x, Log y\", yLabel= \"Log\", logY= True, yLogBase= logBase)\nlogYPlot.add(Line(x= xs, y= expys, displayName= \"f(x) = exp(x)\"))\nlogYPlot.add(Line(x= xs, y= xs, displayName= \"g(x) = x\"))\ncplot.add(logYPlot, 4)\n\nlinearYPlot = Plot(title= \"Linear x, Linear y\", yLabel= \"Linear\")\nlinearYPlot.add(Line(x= xs, y= expys, displayName= \"f(x) = exp(x)\"))\nlinearYPlot.add(Line(x= xs, y= xs, displayName= \"g(x) = x\"))\ncplot.add(linearYPlot,4)\n\ncplot\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot = Plot(title= \"Log x, Log y\", xLabel= \"Log\", yLabel= \"Log\",\n                    logX= True, xLogBase= logBase, logY= True, yLogBase= logBase)\n\nplot.add(Line(x= xs, y= expys, displayName= \"f(x) = exp(x)\"))\nplot.add(Line(x= xs, y= xs, displayName= \"f(x) = x\"))\n\nplot","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top