Search code examples
rjupyter-notebookcurvenormal-distribution

Error in R jupyter "plot.new has not been called yet."


I am trying to run this code in R on Jupyter Notebook.

The code is to plot a curve using the output of normal distribution.

curve(dnorm(x, mean = mean(iris$Petal.Width), sd = sd(iris$Petal.Width)), add = TRUE)

The value of x is:

0 1 2 3 4 5 6 7 8 9 10 50

I am getting this error

Error in plot.xy(xy.coords(x, y), type = type, ...): plot.new has not been called yet.

I tried looking for similar posts such as this one. But the solution provided was not helpful in my case.

can anyone help me with this error or point me in the right direction? thank you.


Solution

  • It's not the problem with jupyter, for curve, you need to provide the function, and also plot something before you do curve with add=TRUE. try something like this below:

    f = function(x){
    dnorm(x,mean=mean(iris$Petal.Width),sd=sd(iris$Petal.Width))
    }
    values = 1:10
    plot(values,f(values),col="blue")
    curve(f,values,add=TRUE)
    

    enter image description here