Plotting histograms with GADFLY

The “density = true” function is not working on my histogram, how do I fix this?

prob1 = result3.u
u2 = repeat(reshape(S₀, :, 1), 1, 200)
y = predict_neuralsde(prob1, u2)[1,:,:]'
y1 = y[end,:]
m = S₀.*exp.(r.*1)
y_sort = y[:, sortperm(y[end,:])];
m = m[1]

plt = Gadfly.plot(y = y1, color=y1,Guide.xlabel("Frequency"),Guide.ylabel(nothing),Theme(key_label_font_size=6pt,major_label_font_size=7pt,key_position = :none),Guide.manual_color_key("", ["Neural SDE Terminal Stock Price","Mean"],["#FFA500","#000000"],pos=[0.6w,-0.45h],shape=[Gadfly.Shape.hline,Gadfly.Shape.hline]),yintercept=[m], Geom.hline(color=["black"],style=[:dash]),Geom.histogram(density=:true , orientation=:horizontal, bincount=20),Guide.yticks(ticks = [50 75 100 125 150 175]), Scale.ContinuousColorScale(p -> get(ColorSchemes.:YlOrBr_6, p)), Guide.xlabel(nothing))

The issue here is that color is used for grouping. In the following right plot, I essentially have 100 1-cell histograms. Because color groups, that is the way it is supposed to work.

y1 = rand(Normal(0, 5), 100)
p1 = plot(x=y1, Geom.histogram(density=false, bincount=20, orientation=:vertical),
p2 = plot(x=y1, Geom.histogram(density=false, bincount=20, orientation=:vertical),
    color=y1, Scale.ContinuousColorScale(p -> get(ColorSchemes.:YlOrBr_6, p))
hstack(p1, p2)


This causes an issue though when density=true, because then each histogram is divided by (sum(bincounts)*binwidth), and those 100 histograms each have 1 bincount.
The issue has been raised in How can I change the colors of bars of a bar plot? (No grouping) · Issue #1422 · GiovineItalia/Gadfly.jl · GitHub. I have had some thoughts about separating color and group, but needs testing.

1 Like

Thanks, so essentially there is currently no way to plot a histogram with density = true , using a color scale?

No not yet. Following Geom.ribbon with group aesthetic by Mattriks · Pull Request #1605 · GiovineItalia/Gadfly.jl · GitHub, I have a new PR (in prep) which will allow this:

D = DataFrame(Dist=["Prior", "Posterior"],  
    Density=[Normal(-0.22, 0.02), Normal(-0.29, 0.015)])

xcoord = Coord.cartesian(xmin=-0.4, xmax=-0.1)
p3 = plot(D, y=:Density, group=:Dist, xcoord,
    layer(Stat.unidistribution([[0.0001, 0.1], [0.1, 0.9], [0.9, 0.9999]]), Geom.ribbon, alpha=[0.8]),
    Scale.color_discrete_manual("orange", "yellow", "coral"), Theme(lowlight_color=identity),


So it might be possible to do something similar with Geom.histogram.

1 Like