# 3d Plot with Plots.jl and GR backend

**URL:** <https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 6, 2019, 10:40am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903 "2019-05-06T10:40:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [May 6, 2019, 10:40am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903/1 "2019-05-06T10:40:40Z")

</div>

I am trying to build a 3d plot using the following code but the plot does not get displayed except the blank grid. This code is taken from a book that uses plotly.

```julia
using Plots, StatsBase, Random, Distributions
Random.seed!(0)

actualAlpha, actualLambda = 2,3
gammaDist = Gamma(actualAlpha,1/actualLambda)
n = 10^3
sample = rand(gammaDist, n)
alphaGrid = 1.5:0.01:2.5;
lambdaGrid = 2:0.01:3.5;
likelihood = [prod([pdf.(Gamma(a,1/l),v) for v in sample]) for a in alphaGrid, l in lambdaGrid];
p1=plot(lambdaGrid, alphaGrid, likelihood,linealpha=0.3, size=(600,600), fc=:heat,st=:surface)

```

the plot should appear like this.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/5/45786336fcbb89465869d6ae3da6ad171434862b.png)

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 6, 2019, 10:50am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903/2 "2019-05-06T10:50:34Z")

</div>

First of all, you’ve got two different plotting calls. The top code is not used in the bottom code. The top code works fine and shows three superimposed histograms, so you can probably delete that from the question.

For the second thing, it’s neither a problem with Plots.jl, GR or 3D (as in your title). It’s simply that your `likelihood` variable contains only 0s. Are you sure that is taken directly from your book?

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [May 6, 2019, 11:12am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903/3 "2019-05-06T11:12:04Z")

</div>

@mkborregaard.  
Thanks. Yes I mistakenly included the first few lines of code. The actual code is after random seed call.  
I can confirm that the code is directly taken from the book. Here is the code from the book " Statistics with Julia"

```julia
using Random, Distributions, PyPlot
Random.seed!(0)
actualAlpha, actualLambda = 2,3
gammaDist = Gamma(actualAlpha,1/actualLambda)
n = 10ˆ3
sample = rand(gammaDist, n)
alphaGrid = 1.5:0.01:2.5
lambdaGrid = 2:0.01:3.5
likelihood = [prod([pdf(Gamma(a,1/l),v) for v in sample])
for a in alphaGrid, l in lambdaGrid]
plot_surface(lambdaGrid, alphaGrid, likelihood, rstride=1,
cstride=1,linewidth=0, antialiased=false, cmap="coolwarm");

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 6, 2019, 11:27am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903/4 "2019-05-06T11:27:32Z")

</div>

Deleted

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [May 6, 2019, 11:36am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903/5 "2019-05-06T11:36:12Z")

</div>

Ah, yes, it’s simply that the numbers are too small to be displayed. Plots uses Showoff to generate the axes ticks, and that converts everything to Float32 internally. Because that doesn’t hold such small numbers, the axis settings are off, and you can’t get the plot to appear.  
Multiplying the result by `1e205` works:

```julia
likelihood = [1e205* prod([pdf(Gamma(a,1/l),v) for v in sample]) for a in alphaGrid, l in lambdaGrid]
surface(likelihood, c = :coolwarm)

```

gives  
 ![14](https://global.discourse-cdn.com/julialang/original/3X/1/7/176282dfe05300e37524fe4f1a523897d8a1a947.png)  
As you can see, GR’s surface plots use a small resolution internally so it isn’t as pretty as the PyPlot one - I think @jheinen is working on improving that.

---

<div class="post-metadata">

**Author:** ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)\
**Post date:** [May 6, 2019, 11:39am UTC](https://discourse.julialang.org/t/3d-plot-with-plots-jl-and-gr-backend/23903/6 "2019-05-06T11:39:49Z")

</div>

Thanks it works.
