Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Plot from Plot.jl to GTK window Julia

Tags:

gtk

julia

glade

I have built a GUI using Glade, GTK and Julia. I want to add a plot in my GUI (in my window / layout) but cannot find a way to add a plot as a widget. How can I implement a plot in my GUI using Plots.jl?

Adding the following does not make anything appear in my GUI layout

x = 1:10; y = rand(10, 3) #
plot(x, y)
like image 554
aight101 Avatar asked Sep 11 '25 22:09

aight101


1 Answers

You can have Plots write the image as a MIME in-memory image and then in the Canvas draw update have Gtk load that image:

using Cairo
using Gtk
using Plots

const io = PipeBuffer()

histio(n) = show(io, MIME("image/png"),  histogram(randn(n)))

function plotincanvas(h = 900, w = 800)
    win = GtkWindow("Normal Histogram Widget", h, w) |> (vbox = GtkBox(:v) |> (slide = GtkScale(false, 1:500)))
    Gtk.G_.value(slide, 250.0)
    can = GtkCanvas()
    push!(vbox, can)
    set_gtk_property!(vbox, :expand, can, true)
    @guarded draw(can) do widget
        ctx = getgc(can)
        n = Int(Gtk.GAccessor.value(slide))
        histio(n)
        img = read_from_png(io)
        set_source_surface(ctx, img, 0, 0)
        paint(ctx)
    end
    id = signal_connect((w) -> draw(can), slide, "value-changed")
    showall(win)
    show(can)
end

plotincanvas()

I am sure that there is also a way to transfer the image data directly, without show(), if you knew the underlying data formats. This method avoids any need for explicit conversions.

like image 159
Bill Avatar answered Sep 13 '25 23:09

Bill