|
| 1 | +import sequtils |
| 2 | +import plotly |
| 3 | +import chroma |
| 4 | +from std / algorithm import reversed |
| 5 | + |
| 6 | +# simple example showcasing error bands (by hand) |
| 7 | + |
| 8 | +let |
| 9 | + d = Trace[float](mode: PlotMode.LinesMarkers, `type`: PlotType.Scatter) |
| 10 | + size = @[16.float] |
| 11 | +d.marker = Marker[float](size: size) |
| 12 | +d.xs = @[1'f64, 2, 3, 4, 5] |
| 13 | +d.ys = @[1'f64, 2, 1, 9, 5] |
| 14 | + |
| 15 | +# Create a Trace for the error band |
| 16 | +let |
| 17 | + dBand = Trace[float](mode: PlotMode.Lines, `type`: PlotType.Scatter, |
| 18 | + opacity: 0.75, # opacity 75% to be prettier |
| 19 | + fill: ToSelf, # `ToSelf` means the filling is done to its own data |
| 20 | + hideLine: true) # line width 0 disables the outline |
| 21 | +# Create X data that is first increasing and then decreasing |
| 22 | +dBand.xs = concat(d.xs, d.xs.reversed) |
| 23 | +# Assign the actual ribbon band. Currently needs to be a seq |
| 24 | +dBand.marker = Marker[float](color: @[color(0.6, 0.6, 0.6)]) |
| 25 | + |
| 26 | +# define some errors we will use (symmetric) |
| 27 | +let yErr = d.ys.mapIt(0.25) |
| 28 | +# now create the first upper band range |
| 29 | +var yErrs = newSeqOfCap[float](d.ys.len * 2) # first upper, then lower |
| 30 | +for i in 0 ..< d.ys.len: # upper errors |
| 31 | + yErrs.add(d.ys[i] + yErr[i]) |
| 32 | +# and now the lower |
| 33 | +for i in countdown(d.ys.high, 0): # lower errors |
| 34 | + yErrs.add(d.ys[i] - yErr[i]) |
| 35 | +dBand.ys = yErrs |
| 36 | + |
| 37 | +let |
| 38 | + layout = Layout(title: "testing", width: 1200, height: 400, |
| 39 | + xaxis: Axis(title: "my x-axis"), |
| 40 | + yaxis: Axis(title: "y-axis too"), autosize: false) |
| 41 | + p = Plot[float](layout: layout, traces: @[d, dBand]) # assign both traces |
| 42 | +echo p.save() |
| 43 | +p.show() |
0 commit comments