SciNim / nim-plotly

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

Add some sugar to plotly #29

Closed Vindaar closed 5 years ago

Vindaar commented 6 years ago

Once again, based on https://github.com/brentp/nim-plotly/pull/28.

While playing around with NimData I wanted to have some easy way to just plot data. I wrote a few templates to do that until I realized that this should really go here (support for DataFrames is then trivial).

~To be fair, this is not an efficient way to create plots (every . template call will create a mutable copy of the output of the previous!), nor is the created code necessarily nice.~ (rewritten using procs) But it seems to work just fine. Until someone can come up with a cleaner solution, I think this is better than nothing. Well, and that this is incomplete is pretty obvious I guess.

It allows us to write e.g.

scatterPlot(x, y)
  .mode(PlotMode.LinesMarkers)
  .markersize(15)
  .show()

which would create reasonable title and labels created from the variable names.

I'd consider this code pretty experimental, but maybe playing around with it a little will uncover the shortcomings better than staring at it. :)

Edit: just a note about why all base plot templates aren't procs: this allows us to get the variable name in the calling scope of the template to provide good defaults for labels etc.

Edit2: Ohhh, I just realized that since Plot is a ref type I can modify the return value of a proc returning Plot[T] (so no need for var Plot[T] return type). No need for everything being a template.

Vindaar commented 6 years ago

Ok, quite a bit nicer now. The base plot templates remain as templates to use the variable names for titles, labels etc.

Added procs to change width / height of layout and fixed a bug (I reused x and y in the heatmap template).

Edit: also every proc, which works on a Trace of the plot, takes an optional idx parameter, to access a specific trace in the plot.

brentp commented 5 years ago

this is nice! thanks!