# Producing plots in real time - "The applicable method may be too new"

**URL:** https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446
**Category:** General Usage
**Tags:** plotting
**Created:** [April 19, 2018, 7:17pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446 "2018-04-19T19:17:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [April 19, 2018, 7:17pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/1 "2018-04-19T19:17:09Z")

</div>

I have existing code that does some complex calculations in “real time”, and I’ve written it so that I can customize its output with user-defined callback functions. This works well for text output or network interfaces, but I cannot for the life of me get it to do anything useful for plotting. I’d like for it to produce a plot that gets extended and displayed as the calculation proceeds forward in time. But without calling gui(), no plot ever gets displayed on the screen. And when I do try to call gui() (or display(plt)), I get the cryptic message

````
ERROR: MethodError: no method matching getindex(::PyPlot.Figure, ::Symbol)
The applicable method may be too new: running in world age 21889, while current world is 21892.```

````

I’ve managed to reproduce this behavior with the following code:

```
using Plots

function myinit()
    pyplot()
    return plot()
end

function mycallback(p, t, x)
    push!(p, [t,], [x,])
    #display(p)
    gui(p)
end

function calculate(t, fn1, fn2)
    foo = fn2()
    for i=1:size(t,1)
        println(t[i], "\t", sin(t[i]))
        fn1(foo, t[i], sin(t[i]))
    end
end

function doall()
    t=0:1:100
    calculate(t, mycallback, myinit)
end

```

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 19, 2018, 8:23pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/2 "2018-04-19T20:23:41Z")

</div>

Put ` pyplot()` outside any function. The way Plots load the backend doesn’t really support loading it in a function like that.

---

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [April 19, 2018, 8:31pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/3 "2018-04-19T20:31:20Z")

</div>

OK. Interesting. That does indeed get rid of the error message. But I get a blank plot.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [April 19, 2018, 8:33pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/4 "2018-04-19T20:33:41Z")

</div>

Can you try the simplest possible plot example?

---

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [April 19, 2018, 8:37pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/5 "2018-04-19T20:37:49Z")

</div>

Sure. This gives me a good plot:

```
t = 0:0.1:pi
x = sin.(t)
plot(t,x)

```

This gives me an empty plot:

```
t = 0:0.1:pi
x = sin.(t)
foo = plot()
push!(foo, t, x)

```

---

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [April 19, 2018, 9:22pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/6 "2018-04-19T21:22:40Z")

</div>

OK, fixed it. Not sure why the above doesn’t work, but the following does:

```
t = 0:0.1:pi
x = sin.(t)
foo = plot([0.], [0.])
push!(foo, t, x)

```

It looks like push!() doesn’t know what to do with a plot that has no existing series - it doesn’t default to establishing a new series. That seems like a bug to me.

---

<div class="post-metadata">

### Author: ![marcelovmaciel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcelovmaciel/32/2573_2.png) [@marcelovmaciel](https://discourse.julialang.org/u/marcelovmaciel)
#### Post date: [April 19, 2018, 10:20pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/7 "2018-04-19T22:20:08Z")

</div>

try

`t = 0:0.1:pi; x = sin.(t); foo = plot() plot!(foo, t, x)`

---

<div class="post-metadata">

### Author: ![GlenHenshaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/glenhenshaw/32/5269_2.png) [@GlenHenshaw](https://discourse.julialang.org/u/GlenHenshaw)
#### Post date: [April 19, 2018, 11:26pm UTC](https://discourse.julialang.org/t/producing-plots-in-real-time-the-applicable-method-may-be-too-new/10446/8 "2018-04-19T23:26:08Z")

</div>

```
plot!(foo, x, t)

```

creates a new series every time it’s called. That isn’t what I want. I want to append newly generated data to the existing series.

Specifically, I’d like to (for instance) read a value from a sensor once a second, and add the new reading to a time series of older values, and have the plot update on the screen after the data is added.
