3d Plot with Plots.jl and GR backend

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.

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.

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?

@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"

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");

Deleted

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:

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
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.

2 Likes

Thanks it works.