Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Plot embedded in Canvas #429

Closed
Kelvyn88 opened this issue Jul 30, 2019 · 11 comments
Closed

Plot embedded in Canvas #429

Kelvyn88 opened this issue Jul 30, 2019 · 11 comments

Comments

@Kelvyn88
Copy link

Hi everybody!

I want to know how to draw a plot created with PyPlot or GR in Canvas. I´m working in a chemical simulation project and I need to show the plot with the results next to others elements or Gtk (buttons, menus, checkboxes)

Any suggestion would be greatly considered.

Kelvyn

@lobingera
Copy link
Contributor

GR.jl has an example https://github.com/jheinen/GR.jl/blob/master/examples/gtk_ex.jl
Gadfly.jl or Winston.jl can render to Gtk/Cairo.
Even matplotlib (in python form) can use GTK3/Cairo for rendering (https://matplotlib.org/gallery/user_interfaces/pylab_with_gtk_sgskip.html?highlight=gtk)

@Kelvyn88
Copy link
Author

I tried the GR example in Atom, I get errors. When I compile the same code in terminal it just run well.

If I change the plot type to just plot instead of hexbin, I got errors as well.

Let me try a little more. I'll share all the information and error messages.

Thanks

@Kelvyn88
Copy link
Author

I just change the code in gtk_ex.jl and run it on the Julia repl and everything was perfect, but in Atom I got:

Starting Julia...
_
_ _ ()_ | Documentation: https://docs.julialang.org
() | () () |
_ _ | | __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ ` | |
| | |
| | | | (
| | | Version 1.1.1 (2019-05-16)
/ |_'|||_'_| | Official https://julialang.org/ release
|__/ |

FATAL ERROR: Gtk state corrupted by error thrown in a callback:
ERROR: SystemError: opening file "C:\Users\Kelvyn\AppData\Local\Temp\jl_3966.tmp.svg": No such file or directory
Stacktrace:

julia> , ::Function, ::String, ::Bool) at .\error.jl:134
[2] systemerror at .\error.jl:134 [inlined]
[3] #open#309(::Bool, ::Nothing, ::Nothing, ::Nothing, ::Nothing, ::Function, ::String) at .\iostream.jl:289
[4] #open at .\none:0 [inlined]
[5] open(::String, ::String) at .\iostream.jl:345
[6] _readfile(::String) at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\GR.jl:3222
[7] show() at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\GR.jl:3270
[8] plot_data(::Bool) at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\jlgr.jl:1366
[9] plot_data at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\jlgr.jl:1063 [inlined]
[10] #plot#34(::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::Array{Float64,1}, ::Vararg{Array{Float64,1},N} where N) at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\jlgr.jl:1563
[11] plot at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\jlgr.jl:1555 [inlined]
[12] #plot#2 at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\GR.jl:3150 [inlined]
[13] plot at C:\Users\Kelvyn.julia\packages\GR\TMylY\src\GR.jl:3150 [inlined]
[14] plot(::Cairo.CairoContext, ::Int32, ::Int32) at C:\Users\Kelvyn\Dropbox\TecNM-Celaya\04_Research\Obj 1\SimBioReactor.jl\GrtestGtk.jl:21
[15] draw(::GtkCanvas) at C:\Users\Kelvyn\Dropbox\TecNM-Celaya\04_Research\Obj 1\SimBioReactor.jl\GrtestGtk.jl:33
[16] draw(::GtkCanvas, ::Bool) at C:\Users\Kelvyn.julia\packages\Gtk\6bRRU\src\cairo.jl:90
[17] notify_resize(::Ptr{GObject}, ::Ptr{Gtk.GdkRectangle}, ::GtkCanvas) at C:\Users\Kelvyn.julia\packages\Gtk\6bRRU\src\cairo.jl:67
[18] (::getfield(Gtk, Symbol("##237#238")))() at C:\Users\Kelvyn.julia\packages\Gtk\6bRRU\src\events.jl:2
[19] g_sigatom(::Any) at C:\Users\Kelvyn.julia\packages\Gtk\6bRRU\src\GLib\signals.jl:167
[20] gtk_main() at C:\Users\Kelvyn.julia\packages\Gtk\6bRRU\src\events.jl:1

@jonathanBieler
Copy link
Collaborator

jonathanBieler commented Jul 31, 2019

It crashes because it fails to load a file, last time I used Juno I also had paths problems (I couldn't include a file even though it existed).

To prevent Gtk crashing completely you can add a @guarded in front of you plot function (see http://juliagraphics.github.io/Gtk.jl/latest/manual/canvas.html).

You can also check Immerse.jl, it has interactive figures with a figure manager. It uses Gadfly though, so it's a bit slow to warm up.

https://github.com/JuliaGraphics/Immerse.jl

@Kelvyn88
Copy link
Author

Hey everybody. Solution is here;

When using Atom, disable the plot pane in the JuliaClient settings. Thats all.

Kelvyn

@Kelvyn88
Copy link
Author

@jonathanBieler Can I push! the plot created with immerse (with its functionality - figure manager) into a Gtk Canvas??

Thanks.

@jonathanBieler
Copy link
Collaborator

Immerse has a Figure type that already contains a Canvas, you can just push it into a container. That's how I do it, I overwrite the figure method and push the Figure into a Notebook : https://github.com/jonathanBieler/GtkIDE.jl/blob/master/src/PlotWindow.jl#L133

@Kelvyn88
Copy link
Author

Hi @jonathanBieler,

This code runs without errors, but my windows does not show the plot:

using Immerse, Distributions, Gtk.ShortNames

# Environmental variable to allow Windows decorations
ENV["GTK_CSD"] = 0

mainWin = Window()
# Properties for mainWin
set_gtk_property!(mainWin, :title, "Test")
set_gtk_property!(mainWin, :window_position, 3)
set_gtk_property!(mainWin, :height_request, 400)
set_gtk_property!(mainWin, :width_request, 400)
set_gtk_property!(mainWin, :accept_focus, true)

p = Immerse.plot(x=[1,2,3,4,5,6], y=[1,2,3,4,5,6])

g = Grid()
push!(mainWin,g)
g[1,1] = Figure(p)

Gtk.showall(mainWin)

@Kelvyn88
Copy link
Author

Anotación 2020-01-12 125929

@jonathanBieler
Copy link
Collaborator

@Kelvyn88 Here's a working example, like I said you need to overwrite the figure method so you can push the figure into your container instead of in a new window :

using Immerse, Gtk.ShortNames

mainWin = Window("")

global const grid = Grid()
grid[1,1] = Label("Nice plot")
push!(mainWin, grid)
Gtk.showall(mainWin)

function Immerse.figure(;name::AbstractString="Figure $(Immerse.nextfig(Immerse._display))",
                 width::Integer=400,    
                 height::Integer=400)

    i = Immerse.nextfig(Immerse._display)
    f = Immerse.Figure()
    signal_connect(Immerse.on_figure_destroy, f, "destroy", Nothing, (), false, (i, Immerse._display))

    grid[1,2] = f
    Gtk.showall(grid)

    Immerse.initialize_toolbar_callbacks(f)
    Immerse.addfig(Immerse._display, i, f)
    i
end

p = Immerse.plot(x=rand(10), y=rand(10))

@Kelvyn88
Copy link
Author

I got it. Thanks!!

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

No branches or pull requests

3 participants