# \[ANN\] ImPlot.jl -- FAST real-time plotting for CImGui.jl

**URL:** <https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087>\
**Category:** Package Announcements\
**Created:** [July 14, 2020, 11:23pm UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087 "2020-07-14T23:23:49Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)\
**Post date:** [February 13, 2021, 10:26am UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/21 "2021-02-13T10:26:40Z")

</div>

This is beautiful. So simple, so clean. Thank you! \<3

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [March 12, 2021, 11:42pm UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/22 "2021-03-12T23:42:59Z")

</div>

How should I change marker style, like in scatter plot from this [c++ demo](https://traineq.org/implot_demo/src/implot_demo.html):

```julia
        if (ImPlot::BeginPlot("Scatter Plot", NULL, NULL)) {
            ImPlot::PlotScatter("Data 1", xs1, ys1, 100);
            ImPlot::PushStyleVar(ImPlotStyleVar_FillAlpha, 0.25f);
            ImPlot::SetNextMarkerStyle(ImPlotMarker_Square, 6, ImVec4(0,1,0,0.5f), IMPLOT_AUTO, ImVec4(0,1,0,1));
            ImPlot::PlotScatter("Data 2", xs2, ys2, 50);
            ImPlot::PopStyleVar();
            ImPlot::EndPlot();
        }

```

Looks like some `ImPlotStyleVar_...` and `SetNext...` methods are missing?

---

<div class="post-metadata">

**Author:** ![Thomas\_Lei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_lei/32/20372_2.png) [@Thomas\_Lei](https://discourse.julialang.org/u/Thomas_Lei)\
**Post date:** [April 8, 2021, 3:08pm UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/23 "2021-04-08T15:08:59Z")

</div>

Hi all,

First of all thanks for the amazing work on ImPlot.

I’m trying to use use CImGui/ImPlot to display/process data I receive from a UDP socket. Baslically, my `infinite_loop` function (which I renamed `listen!`) looks like this :

```julia
function listen!(cb::Listener)
    cb.stopped = false
    @async while true
        cb.stopped && break
        data = process(copy(recv(cb.socket)))
        push!(cb, data)
        yield()
    end
end

```

I’ve setup everything following [this example](https://github.com/Gnimuc/CImGui.jl/discussions/39) with `@async` / `yield()` in the `renderloop` and everything goes smoothly with low UDP framerate (about 5 UDP frames per seconds).

However I’m loosing UDP frames when the rate at which data is sent to the UDP socket is increased. In the real life application I need to receive and display 3200 bytes every 64 µs. I guess that the Julia scheduler does not yield to the` listen!` function often enough for the data to be `recv` and processed in time.

Things get better when I `yield()` after every instruction in the rendering loop but I’m still loosing a lot of data.

I also tried to get things working with `Threads.@spawn` as in [this example](https://gist.github.com/sefffal/c422395cbdf5fea13986b99b2f2e85bd) but without any sucess.

Any hints ?  
Thanks !

---

<div class="post-metadata">

**Author:** ![sefffal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sefffal/32/23640_2.png) [@sefffal](https://discourse.julialang.org/u/sefffal)\
**Post date:** [April 8, 2021, 8:32pm UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/24 "2021-04-08T20:32:27Z")

</div>

What if you had the render thread always plot the latest data from an array, while another thread updates that array whenever data is available?

I use that pattern to show updates at 60fps while the data itself updates at 200hz.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [April 9, 2021, 5:12am UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/25 "2021-04-09T05:12:04Z")

</div>

What’s the definition of `Listener`? Have you tried using `Channel` with `spawn = true`?

> [@Thomas\_Lei](#):
>
> I need to receive and display 3200 bytes every 64 µs.

Are you sure the data need to be **displayed** at this high framerate? That’s about 15000fps. Common monitors have a refresh rate of up to 240Hz.

If what you meant is displaying at a common fps but processing the data every 64us, then I believe the `Channel` is enough for this use case.

---

<div class="post-metadata">

**Author:** ![Thomas\_Lei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_lei/32/20372_2.png) [@Thomas\_Lei](https://discourse.julialang.org/u/Thomas_Lei)\
**Post date:** [April 9, 2021, 6:05am UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/26 "2021-04-09T06:05:11Z")

</div>

> [@Gnimuc](#):
>
> Are you sure the data need to be **displayed** at this high framerate? That’s about 15000fps.

Data is coming in at about 15 kHz but indeed processing and display is performed at common fps rates (every 20 to 100 ms).

> [@Gnimuc](#):
>
> What’s the definition of `Listener`? Have you tried using `Channel` with `spawn = true`?

It basically consists in :

- a UDP socket,
- a 2D circular buffer for data, and a 1D circular buffer for storing UDP frames indices (updated every 64 µs)
- 2 arrays which store the current data to be displayed (updated every ~20 ms)

```julia
mutable struct Listener <: AbstractArray{Cfloat, 2}

    # UDP data
    IP_adress :: IPv4
    port :: Int 
    socket :: UDPSocket

    # Buffer 
    capacity :: Int
    nb_channels :: Int
    first :: Int
    length :: Int
    buffer :: Array{Cfloat, 2}
    indices :: Vector{Int64}

    # Current data
    current_data :: Array{Cfloat, 2}
    current_indices :: Vector{Int64}
    counter :: Int64

    # State
    stopped :: Bool
end

```

> [@Gnimuc](#):
>
> If what you meant is displaying at a common fps but processing the data every 64us, then I believe the `Channel` is enough for this use case.

> [@sefffal](#):
>
> What if you had the render thread always plot the latest data from an array, while another thread updates that array whenever data is available?

Indeed, that’s what I’m trying to do with `@async` tasks and `yield()`, but I guess `Channel` or `Threads@spawn` better suit my needs. Thank you to both of you for the tips; I’ll give it a try and keep you posted.

---

<div class="post-metadata">

**Author:** ![Thomas\_Lei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thomas_lei/32/20372_2.png) [@Thomas\_Lei](https://discourse.julialang.org/u/Thomas_Lei)\
**Post date:** [April 13, 2021, 7:24am UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/27 "2021-04-13T07:24:39Z")

</div>

Got it working with `Threads.@spawn` and `lock()/unlock()` in both rendering/listening loops.  
Thanks @sefffal and @Gnimuc !

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [April 13, 2021, 7:44am UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/28 "2021-04-13T07:44:19Z")

</div>

Would you mind publishing a simplified example in a gist? I believe this will benefit others in the future.

---

<div class="post-metadata">

**Author:** ![sairus7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sairus7/32/10816_2.png) [@sairus7](https://discourse.julialang.org/u/sairus7)\
**Post date:** [May 26, 2021, 9:21pm UTC](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087/29 "2021-05-26T21:21:51Z")

</div>

All examples from cpp demo are now reimplemented in Julia!

@wsphillips can you update first message with this notification, and maybe change gif image to something more interesting from demo examples? 🙂

[Previous page](https://discourse.julialang.org/t/ann-implot-jl-fast-real-time-plotting-for-cimgui-jl/43087.md?page=1)
