# How do I plot?

**URL:** <https://discourse.julialang.org/t/how-do-i-plot/30783>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 6, 2019, 9:13am UTC](https://discourse.julialang.org/t/how-do-i-plot/30783 "2019-11-06T09:13:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emir\_Sahin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/emir_sahin/32/11168_2.png) [@Emir\_Sahin](https://discourse.julialang.org/u/Emir_Sahin)\
**Post date:** [November 6, 2019, 9:13am UTC](https://discourse.julialang.org/t/how-do-i-plot/30783/1 "2019-11-06T09:13:47Z")

</div>

xfirst=rand(-5:5)

for n=1:100

```
a=rand(-0.1:0.1)

xnew=xfirst+(xfirst*a)

if mainfonk(xnew)>mainfonk(xfirst)

    xfirst=xnew

    println(mainfonk(xfirst))

end

```

end

Hi. I am working hill climbing in julia. I am trying to find how can I create graph this code. But I didnt find. I want to create a graph by printing the resulting S values on the y axis and the number of iterations it finds on the x axis.Can you help me?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [November 6, 2019, 10:05am UTC](https://discourse.julialang.org/t/how-do-i-plot/30783/2 "2019-11-06T10:05:43Z")

</div>

> [@Emir\_Sahin](#):
>
> how can I create graph

Hi there! The word used is likely “plot” rather than “graph”. So if you search your tutorials or manuals for “plot” you should find some clues. There’s a [JuliaPlots](http://docs.juliaplots.org/latest/) organzsation, which should be a good place to start learning.

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [November 6, 2019, 10:43am UTC](https://discourse.julialang.org/t/how-do-i-plot/30783/3 "2019-11-06T10:43:34Z")

</div>

Besides the plotting, there are some issues with your code. For example, `rand(-0.1:0.1)` always gives you the same result, and `xnew=xfirst+(xfirst*a)` never makes progress when `xfirst` is zero.

You could rewrite it like this (requires Plots and Distributions package):

```julia
import Distributions: Uniform

# one iteration
function step(xfirst,mainfct)
    a=rand(Uniform(-0.1,0.1))
    xnew=xfirst+a
    if mainfct(xnew)>mainfct(xfirst)
        return xnew
    end
    return xfirst
end

function runSteps(xfirst,mainfct,n=100)
    xarr = [xfirst]
    for i=1:n
        xfirst = step(xfirst,mainfct)
        push!(xarr, xfirst)
    end
    return xarr
end

mainfct(x) = (x+1.0)^2 # your input function

xfirst = 0.0 # starting value
xarr = runSteps(xfirst, mainfct, 100) # run 100 steps
yarr = mainfct.(xarr) # get function values

using Plots
plot(0:length(yarr)-1, yarr, xlabel="iterations", ylabel="function value at iteration",label="")

```

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 6, 2019, 5:45pm UTC](https://discourse.julialang.org/t/how-do-i-plot/30783/4 "2019-11-06T17:45:32Z")

</div>

There are a number of actively maintained packages around for plotting. They differ in the kind of interface they provide, the features etc. I believe the most widely used ones right now are probably [Gadfly.jl](https://github.com/GiovineItalia/Gadfly.jl), [Makie.jl](https://github.com/JuliaPlots/Makie.jl), [PlotlyJS.jl](https://github.com/sglyon/PlotlyJS.jl), [Plots.jl](https://github.com/JuliaPlots/Plots.jl) and [VegaLite.jl](https://github.com/queryverse/VegaLite.jl) (in alphabetical order). I might well have missed some 🙂
