# Geom.contour in Gadfly.jl - How to use it?

**URL:** https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123
**Category:** Visualization
**Tags:** gadfly
**Created:** [April 27, 2021, 7:25pm UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123 "2021-04-27T19:25:26Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)
#### Post date: [April 27, 2021, 7:25pm UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123/1 "2021-04-27T19:25:27Z")

</div>

I am trying to draw iso-density contours on a plot. Yet, it seems I am drawing lines all over the place – as opposed to contours representing different iso-densities (plot should be top-view of a mountain). I tried point and function representations, but my plot is wonky either way. I am missing something fundamental, but am not sure what it is? Any help is appreciated. Thanks!

Here is code to reproduce one of my wonky plots:

```julia
using Distributions, Gadfly, Format, DataFrames

## specify random variables
X = Beta(2,2)
unscaledBeta = Beta(2,8)
Y = LocationScale(0, 100000, Beta(2,8))

## calculate log pdf
function negLogDensFun(x::Real,y::Real)
    -logpdf(X,x) - logpdf(Y,y) ## logpdf built-in to Distributions
end

## test it
negLogDensFun(0.5,25000)

## sample directly and show on grid
numDraws = 100
plotDF = DataFrame(winningsMultiplier = rand(X,numDraws),
                    maxWinnings = rand(Y,numDraws))
plotDF.negLogDens = negLogDensFun.(plotDF.winningsMultiplier,
                                  plotDF.maxWinnings)

# make plot
plot(plotDF, x = :winningsMultiplier,
             y = :maxWinnings,
        layer(Geom.point),
        layer(z=(x,y) -> negLogDensFun(x,y),
              Geom.contour(levels = 3)),
        Coord.cartesian(xmin =0, xmax = 1,
                        ymin = 0, ymax = 10^5),
        Scale.y_continuous(labels = x -> format(x, commas = true)))

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/2/a28725a32239710177a7823947a017fa5c552b65.png)

Wonky plot (above), but I was hoping for contour lines akin to the ones shown in the gallery (below):

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac856a916c3a1dc6f533e350cb9c020338b69497.png)

My plot would have just one mountain top though.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 27, 2021, 11:14pm UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123/2 "2021-04-27T23:14:52Z")

</div>

Have you checked [this post](https://discourse.julialang.org/t/way-to-make-contour-plot-from-3-vectors/20118/4)?

---

<div class="post-metadata">

### Author: ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)
#### Post date: [April 28, 2021, 12:11am UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123/3 "2021-04-28T00:11:03Z")

</div>

The above example should work (because `z` is a function), but the issue is the `Inf` values on the edges of the domain (which I assume cause a problem for [Contour.jl](https://github.com/JuliaGeometry/Contour.jl)).

```julia
plot(z=(x,y) -> negLogDensFun(x,y), Geom.contour(),
         xmin=[0.001], xmax=[0.999], ymin=[0.001], ymax=[10^4.9], 
    Theme(line_width=3pt)
    )

```

![Contour](https://global.discourse-cdn.com/julialang/original/3X/5/a/5a83e9c709b69843731e2e494d908781796eb070.png)

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 28, 2021, 11:23am UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123/4 "2021-04-28T11:23:38Z")

</div>

When a function is provided how many points Gadfly computes in order to produce the contours?

Another way using a matrix may allow some extra user control on the plot:

```julia
x = plotDF.winningsMultiplier
y = plotDF.maxWinnings
x0, y0 = sort(x), sort(y)
z0 = [negLogDensFun(x,y) for x in x0, y in y0]
Gadfly.plot( layer(z=z0, x=x0, y=y0, Geom.contour(levels=5)),
           layer(x=x, y=y) )

```

 ![Gadfly_logDenFun](https://global.discourse-cdn.com/julialang/original/3X/6/9/69297f10b4bf31a6d22d1b86e25487601bd7e853.png)

---

<div class="post-metadata">

### Author: ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)
#### Post date: [April 28, 2021, 2:03pm UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123/5 "2021-04-28T14:03:47Z")

</div>

> [@Mattriks](#):
>
> ```julia
> plot(z=(x,y) -> negLogDensFun(x,y), Geom.contour(),
> xmin=[0.001], xmax=[0.999], ymin=[0.001], ymax=[10^4.9], 
> Theme(line_width=3pt)
> )
> 
> ```

Thanks for this tip. I was missing the sort step when I tried going the matrix route.

---

<div class="post-metadata">

### Author: ![Adam\_Fleischhacker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adam_fleischhacker/32/24468_2.png) [@Adam\_Fleischhacker](https://discourse.julialang.org/u/Adam_Fleischhacker)
#### Post date: [April 28, 2021, 2:06pm UTC](https://discourse.julialang.org/t/geom-contour-in-gadfly-jl-how-to-use-it/60123/6 "2021-04-28T14:06:39Z")

</div>

This is fabulous. Thank you for debugging this - I was out of ideas and very much appreciate the support 🙂
