# Understanding basic Plots behavior

I’m working my way through basic Julia exercises in a course. I have no problem with most of it but I haven’t yet grasped the odd way plots seem to work. Here’s a basic fn for calculating the steady state values of a logistic map:

``````function steady_state_values(r)
b = 0.25
for i in 1:400
b = r * b * (1-b)
end
attractor_values = zeros(150)
for i in 1:150
b = r * b * (1-b)
attractor_values[i] = b
end
return attractor_values
end
``````

I want to vary the parameter r (along the x axis) and plot the corresponding steady state values along y. I’ve tried variations of this:

``````using Plots
first = true
for r = 2.9 : 0.001 : 4
x = fill(r, length(y))
if first
plot(x=x, y=y)
first = false
else
plot!(x=x, y=y)
end
end
``````

Multiple calls to plot don’t seem to work–they result in no output-- so I plot the first array then call plot! subsequently. Even this doesn’t work. Can someone explain the logic? I come from matplotlib where you call figure() to begin, then any number of plotting operations, then show().
Plots.jl doesn’t seem to work that way. I’ve gone through a Plots.jl notebook but it didn’t clarify.

Thanks,
-Tom

`plot(...)` creates and returns a plot object. `plot!(plt,...)` modifies the plot object `plt` and returns it. If `plt` is not specified, the last plot touched is used.

In Juno and Jupyter, a plot is shown when it’s the output of the cell/line that was just run. It’s just like with any return value, except instead of showing a textual representation, it’s graphical. Your code ended in a `for` loop, which has the value `nothing`, so nothing is shown or printed.
To see the plot, you can do

``````using Plots
plt = plot()
for r = ...
...
plot!(x, y)  # or plot!(plt,x,y)
end
plt
``````

The last line is to display the plot `plt`.

3 Likes

Are you doing this in Jupyter or a `.jl` file? It can matter when `for` loops are involved… my suggestion is to stay in Jupyter for this sort of stuff unless your loops in a function.

I was doing this directly in Jupyter.

Thanks! That’s it exactly.

(In a Python notebook the plotting is a side effect, and the value doesn’t matter. Hence my confusion.)