Python3 Plots

You can access Beaker's native interactive plotting library from Python.

Plot with simple properties

Python3 plots has syntax very similar to Groovy plots. Property names are the same.


In [3]:
from beaker import *

Plot(title="Title",
     xLabel="Horizontal",
     yLabel="Vertical",
     initWidth=500,
     initHeight=200)


Plot items

Lines, Bars, Points and Right yAxis


In [4]:
x = [1, 4, 6, 8, 10]
y = [3, 6, 4, 5, 9]

pp = Plot(title='Bars, Lines, Points and 2nd yAxis', 
          xLabel="xLabel", 
          yLabel="yLabel", 
          legendLayout=LegendLayout.HORIZONTAL,
          legendPosition=LegendPosition(position=LegendPosition.Position.RIGHT),
          omitCheckboxes=True)

pp.add(YAxis(label="Right yAxis"))
pp.add(Bars(displayName="Bar", 
       x=[1,3,5,7,10], 
       y=[100, 120,90,100,80], 
       width=1))
pp.add(Line(displayName="Line", 
            x=x, 
            y=y, 
            width=6, 
            yAxis="Right yAxis"))
pp.add(Points(x=x, 
              y=y, 
              size=10, 
              shape=ShapeType.DIAMOND,
              yAxis="Right yAxis"))


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-4-d195137c12dd> in <module>()
      9           omitCheckboxes=True)
     10 
---> 11 pp.add(YAxis(label="Right yAxis"))
     12 pp.add(Bars(displayName="Bar", 
     13        x=[1,3,5,7,10],

AttributeError: 'Plot' object has no attribute 'add'

Areas, Stems and Crosshair


In [ ]:
ch = Crosshair(color=beaker.prefs['theme']['plotColors'][0], width=2, style=StrokeType.DOT)
plot = Plot(crosshair=ch)
y1 = [4, 8, 16, 20, 32]
base = [2, 4, 8, 10, 16]
cs = [Color.black, Color.orange, Color.gray, Color.yellow, Color.pink]
ss = [StrokeType.SOLID, 
      StrokeType.SOLID, 
      StrokeType.DASH, 
      StrokeType.DOT, 
      StrokeType.DASHDOT, 
      StrokeType.LONGDASH]
plot.add(Area(y=y1, base=base, color=Color(255, 0, 0, 50)))
plot.add(Stems(y=y1, base=base, color=cs, style=ss, width=5))

Constant Lines, Constant Bands


In [ ]:
p = Plot ()
p.add(Line(y=[-1, 1]))
p.add(ConstantLine(x=0.65, style=StrokeType.DOT, color=Color.blue))
p.add(ConstantLine(y=0.1, style=StrokeType.DASHDOT, color=Color.blue))
p.add(ConstantLine(x=0.3, y=0.4, color=Color.gray, width=5, showLabel=True))

In [ ]:
Plot().add(Line(y=[-3, 1, 3, 4, 5])).add(ConstantBand(x=[1, 2], y=[1, 3]))

TimePlot


In [ ]:
import time

millis=current_milli_time()

hour = round(1000 * 60 * 60);
xs = [];
ys = [];
for i in range(11):
    xs.append(millis + hour * i);
    ys.append(i);

plot = TimePlot(timeZone="America/New_York")
# list of milliseconds
plot.add(Points(x=xs, y=ys, size=10, displayName="milliseconds"))

NanoPlot


In [ ]:
millis = current_milli_time()
nanos  = millis * 1000 * 1000
xs = []
ys = []
for i in range(11):
    xs.append(nanos + 7 * i)
    ys.append(i);

np = NanoPlot()
np.add(Points(x=xs, y=ys))

Stacking


In [ ]:
y1 = [1,5,3,2,3]
y2 = [7,2,4,1,3]
p = Plot(title='Plot with XYStacker', initHeight=200)
a1 = Area(y=y1, displayName='y1')
a2 = Area(y=y2, displayName='y2')
stacker = XYStacker()
p.add(stacker.stack([a1, a2]))

SimpleTime Plot


In [ ]:
SimpleTimePlot(beaker.rates, ["y1", "y10"])

In [ ]:
SimpleTimePlot(beaker.rates, ["y1", "y10"], # column names
               timeColumn="time", # time is default value for a timeColumn
               yLabel="Price", 
               displayNames=["1 Year", "10 Year"],
               colors = [[216, 154, 54], Color.lightGray],
               displayLines=False, # no lines (true by default)
               displayPoints=True) # show points (false by default))

Combined Plot


In [ ]:
# The CombinedPlot allows for stacked plots with linked X axis.
c = CombinedPlot(title="US Treasuries", initWidth=1000)

p1 = TimePlot(yLabel="Short Term")
p1.add(Line(x=beaker.time, y=beaker.m3, displayName="3 month"))

p2 = TimePlot(yLabel="Long Term")
p2.add(Line(x=beaker.time, y=beaker.y10, displayName="10 Year"))

c.add(p1, 1).add(p2, 1)

Demo data


In [ ]:
rates = beaker.rates
'ok'