Skip to content
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

PlotlyJS doesn't work with Interact #31

Closed
ranjanan opened this issue Mar 31, 2016 · 11 comments
Closed

PlotlyJS doesn't work with Interact #31

ranjanan opened this issue Mar 31, 2016 · 11 comments

Comments

@ranjanan
Copy link

PlotlyJS doesn't work with the @manipulate macro.

using Interact
@manipulate for i = 1:10
    PlotlyJS.plot()
end

gives an error:

Not implemented (yet). Use Electron frontend to save figures
@ranjanan
Copy link
Author

cc: @shashi @rohitvarkey

@rohitvarkey
Copy link

On a hunch, I tried forcing the MIME to "text/html", which resulted in the plot appearing, but Interact doesn't seem to update the plot.

using Interact
@manipulate for i = 1:10
    p = gen_plot(i) #some function that returns a plot.
    display(MIME"text/html"(), p)
end

@shashi
Copy link

shashi commented Mar 31, 2016

display has side-effects, so it doesn't update.

@shashi
Copy link

shashi commented Mar 31, 2016

With this hack

immutable PlotWrap
    x::Any
end

Base.writemime(io::IO, m::MIME"text/html", p::PlotWrap) = writemime(io, m, p.x)

This works

@manipulate for n=1:10
    PlotWrap(plot(scatter(x=1:n, y=rand(n))))
end

Looks like the PNG writemime is being chosen in the case @ranjanan posted. I wonder why, Interact should pick html by default.

Either way, I don't think PlotlyJS should error out if writemime is called for PNG

@ranjanan
Copy link
Author

Thanks for the hack, @shashi

@sglyon
Copy link
Member

sglyon commented Mar 31, 2016

@shashi doesn't display always have side effects (it does some sort of IO to display an object)? What side effects are causing the problems here?

Also, because plotly and this package have built in support for manipulating a plot that has already been displayed (see all the methods here), I believe it might be worthwhile to write a custom PlotlyJS.jl + Reactive.jl bridge. Any thoughts on where to start with that?

@sglyon
Copy link
Member

sglyon commented Mar 31, 2016

@shashi Agreed that PlotlyJS shouldn't error out when trying to save a PNG from a notebook. Supporting png/pdf/jpeg/eps output from the notebook only requires that I can execute a one-line call to javascript and have the notebook return the string output back to Julia.

I simply haven't had time to hook up this 2 way communication in the notebook yet, but if someone already knows how it can work, I'd love any pointers on where to start.

@sglyon
Copy link
Member

sglyon commented Mar 31, 2016

Thanks to @shashi I've figured out how to make this work without the need for the hack.

This works now:

@manipulate for i = 1:10
    plot(scatter(x=1:i, y=randn(i)))
end

But what's even cooler is that the more efficient plotly.js javascript routines for updating just portions of a plot also work:

p = plot(scatter(x=1:10, y=randn(10), mode="markers"),
         Layout(xaxis_range=[0, 12], yaxis_range=(-3, 3)))
display(p)
@manipulate for marker_size=10:5:75
    restyle!(p, marker_size=marker_size)
end

If you're going to be mutating plots with @manipulate I'd recommend looking into using the plotly.js API methods like restyle!, relayout! and friends

@sglyon sglyon closed this as completed Mar 31, 2016
@shashi
Copy link

shashi commented Mar 31, 2016

💯

@ranjanan
Copy link
Author

ranjanan commented Apr 1, 2016

@spencerlyon2 In your example

p = plot(scatter(x=1:10, y=randn(10), mode="markers"),
         Layout(xaxis_range=[0, 12], yaxis_range=(-3, 3)))
display(p)
@manipulate for marker_size=10:5:75
    restyle!(p, marker_size=marker_size)
end

How do I change the sizes of the first 5 markers and not the others?

@sglyon
Copy link
Member

sglyon commented Apr 1, 2016

Hmm that one is a bit tricker. I can think of a inelegant way to do it: marker_size=vcat(fill(marker_size, 5), fill(10, length(p.plot.data[1][:x])-5)), but there should be a better solution to that.

The semantics of restyle! follows the plotly.js version. I'd recommend looking at their examples here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants