-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add subplot macro for basic subplots #34
Conversation
Basically `PlotJson` is just a `Plot[T]` object, where the fields of it have been converted to `JsonNodes`. This allows for much easier handling of: - multiple traces of different data types - extension of `PlotJson` by manually adding json fields, which are not yet supported by nim plotly and still being able to hand the resulting `PlotJson` to the `show` or `saveImage` procs
# given in relative coordinates of the plot [0, 1] canvas | ||
Domain* = tuple | ||
left, bottom, width, height: float | ||
DomainAlt* = tuple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you document DomainAlt? I'm not sure what it is/does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DomainAlt
simply refers to the alternate representation of a domain, which uses right
and top
instead of width
and height
. I simply introduced these as two named types to clear up the proc signatures.
https://github.com/brentp/nim-plotly/blob/6a897fa35ad1291da1c43140bd81655feed1563f/src/plotly/plotly_subplots.nim#L4-L15
then is used to convert DomainAlt
to a normal Domain
. I should maybe add a comment to the DomainAlt
line though (and now that I look at the code for convertDomain
, use Domain
in the type check there. Will do both tomorrow.
this is really neat! Looks good to me after documenting DomainAlt. |
Will add a line about |
cool! if you specify subplots without explicit domains, does plotly give you a reasonable default (multi) plot? |
To be honest, I have no idea as I haven't tried it. But I do know that an alternative to explicit domain setting is the |
If no grid layout nor layout via domains is given, automatically calculates a suitable grid, which favors columns over rows.
Ok, all done now. Add the ability to set a grid aside from manually setting domains (though mixing isn't supported. No clue if plotly even itself supports that). grid:
rows: 2
columns: 1 where either is optional. If only one is set, the other is assumed to be 1. |
thanks! I actually have been needing this. |
This adds basic subplot support via the new
subplots
macro.A basic implementation for that was quite easy, but then I realized that for real subplots it might be justified to have
Plot[T]
objects as a basis with differentT
. Therefore I introduced aPlotJson
object, which is basically thePlot[T]
object albeit with the two fields already converted toJsonNodes
. This way we can merge the twoPlot[T]
of potentially different data types together.The main part of the creation of subplots is then the (currently pretty static and hacky)
combine
proc. Since the signature ofcombine
isn't very nice and in my opinion the user doesn't need to know what's needed to create a subplot, I started to write a macro. In the end it's a lot of lines for a single output line of the macro, but well. It's there now. :PUsage looks like this (assuming we have a
plt1: Plot[T]
,plt2: Plot[U]
,layout: Layout
):So it consists of a layout object to be used as the base layout for the whole plot and then several
plot
blocks. Eachplot
block then just takes aPlot[T]
as the first line and then a description of the plot location. Either as a nameless tuple of the type:(left, bottom, width, height)
in relative coordinates of the canvas in
[0, 1]
. Or one can specifically set each of the fields, one per line. That way one may also useright
,top
instead ofwidth
,height
(for good measure instead of:
,=
is also supported, and so are just first letters for each location, i.e.l
,b
, ...).Useful sideffect of
PlotJson
As a side effect, since
PlotJson
is now a valid type forshow
andsaveImage
one can thus easily extend plotly "on the fly" if a feature isn't available. So for instance if we have someand we wish to use a feature that's not in nim-plotly: for example to set the length of the ticks. In plotly this is done via
ticklen
(https://plot.ly/javascript/reference/#layout-yaxis-ticklen). Now we can just do:and all should work.
In theory one can also just copy paste some plotly.js code and create a
JsonNode
via the%*
macro and with little change one can even create a plot that way. If this nim-lang/Nim#10037 PR would be merged that would become almost trivial.Not very useful in general, but for quick prototyping of unavailable features might come in handy.