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

Plots integration #13

Open
PhilipVinc opened this issue Apr 12, 2021 · 5 comments
Open

Plots integration #13

PhilipVinc opened this issue Apr 12, 2021 · 5 comments
Assignees

Comments

@PhilipVinc
Copy link

PhilipVinc commented Apr 12, 2021

With some painful reverse engineering, I realised that we can use Plots.jl to integrate with StripplePlotly with the following:

using Mesurements
using Plots

function to_plotly_base(pl)
  pl = Plots.plotlybase_syncplot(pl)
  delete!(pl.layout.fields, :height)
  delete!(pl.layout.fields, :width)
  pl.layout.fields[:margin] = Dict(:l=>50, :b=>50, :r=>50, :t=>60)
  return pl
end

# generate data with errors
y = Measurements.measurement(rand(10), rand(10)*0.1)

# use recipes from plots.jl
plt = plot(1:10, y)

# this object now is a PlotlyBase.Plot and can be used with Stripple
plt = to_plotly_base(plt)

# update the model
model.plot_data[] = plt.data
model.plot_layout[] = plt.layout

It would be nice if this could be done automatically.

(The margins are the default from PlotlyBase, because the default from Plots jl get cropped when using them with stripple)

@essenciary
Copy link
Member

Thanks for sharing this, definitely worth digging into it!

@touchft
Copy link

touchft commented Apr 12, 2021

PlotlyJS.jl are wrap of PlotlyBase.jl. The good is that the data and layout fields from PlotlyBase behave same as they do in plot of StipplePlotly.

BTW: I cannot find Plots.plotlybase_syncplot .

julia> using Plots

julia> Plots.plotlybase_syncplot
ERROR: UndefVarError: plotlybase_syncplot not defined

@PhilipVinc
Copy link
Author

It's in here.
I guess it's being lazy loaded so you might have to run Plots.plotly() first ?

@PhilipVinc
Copy link
Author

PhilipVinc commented Apr 12, 2021

Confirmed.
You must have PlotlyBase in your project too, and run Plots.plotly().

@AbhimanyuAryan AbhimanyuAryan self-assigned this Feb 15, 2022
@hhaensel
Copy link
Member

hhaensel commented Oct 10, 2024

Just want to add a hint that there is pop! to delete a field from a layout. In this case your implementation is probably the fastest you can do, but it may break if the internals of the package change, e.g. when one should decide to move away from the fields structure. So your function could read:

function to_plotly_base(pl)
  if isdefined(Plots, :plotlybase_syncplot)
    current_backend = Plots.backend()
    Plots.plotly() === current_backend || Plots.backend(current_backend)
  end

  pl = Plots.plotlybase_syncplot(pl)
  pop!(pl.layout, :height)
  pop!(pl.layout, :width)
  pl.layout.fields[:margin] = Dict(:l=>50, :b=>50, :r=>50, :t=>60)
  return pl
end

Anyhow, I proposed a PR to also add a delete!()method to PlotlyBase.

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

5 participants