SciNim / nim-plotly

plotly wrapper for nim-lang
https://scinim.github.io/nim-plotly/
MIT License
179 stars 15 forks source link

[WIP] subplots API #43

Open timotheecour opened 5 years ago

timotheecour commented 5 years ago

[will write more details here]

the obvious next step is provide a nice API to generate subplots, as in matlab / matplotlib etc Ideally:

note

Vindaar commented 5 years ago

I wanted to wait until you filled in the details, but no matter.

First of all you're aware of the subplots macro from #34 here: https://github.com/brentp/nim-plotly/blob/master/src/plotly/plotly_subplots.nim?

What you describe is in possible already. Yes, not via creating a grid and filling it one after another. Only via a macro for now (but for the general use case it's really simple to use!).

What you describe however is basically done by the combine proc here: https://github.com/brentp/nim-plotly/blob/master/src/plotly/plotly_subplots.nim#L62 The reason why this proc is not public is the fact that it has to work with PlotJson instead of the normal Plot[T] type. I don't want the user having to deal with the confusion of why there's another type aside from Plot[T] and the bother of having to convert each Plot[T] via toPlotJson before we can hand them all to combine.

For your createGrid approach it's thinkable to do something like

type
  Grid = object
    plts: seq[PlotJson]
    cols: int
    rows: int
    domains: seq[Domain]

proc `[]=`[T: Plot](grid: var Grid, idx: int, plt: T) =
  grid.plts[idx] = plt.toPlotJson

(didn't test that code) of course to avoid having the user deal with PlotJson by themselves.

more complex use cases should be feasible, eg support use case where user has his own html and asks plotly to fill in a div with a Plot

That seems orthogonal to subplots to me? Although that's surely a nice feature.

timotheecour commented 5 years ago

Yes, not via creating a grid and filling it one after another. Only via a macro for now

thanks; wasn't aware of subplots macro;

let subplt = subplots:
  baseLayout: layout
  plot:
    plt1
    left: 0.0
    bottom: 0.0
    width: 0.45
    height: 1.0
    # alternatively use right, top instead of width, height
    # single letters also supported, e.g. l == left
  plot:
    plt2
    # or just write a concise tuple, here the
    (0.55, 0.0, 0.45, 1.0)

however that's not flexible enough for many workflows where's it's easier to fill them in 1 by 1 programmatically instead of declaratively (eg if arbitrary nrows/ncols or even if these are known at RT rather than CT)

The reason why this proc is not public is the fact that it has to work with PlotJson instead of the normal Plot[T] type. I don't want the user having to deal with the confusion of why there's another type aside from Plot[T] and the bother of having to convert each Plot[T] via toPlotJson before we can hand them all to combine

i don't think it's such a big deal to expose PlotJson and require toPlotJson in the appropriate places; we get CT errors anyways so it's trivial to fix; I'd rather have the flexibility (even if there's a big fat warning in the proc doc comment regarding that) rather than being limited;

that being said, there may be a way to do everything via Plot[T]; but i need to think more