# Plotting histograms with GADFLY

**URL:** <https://discourse.julialang.org/t/plotting-histograms-with-gadfly/93467>\
**Category:** General Usage\
**Tags:** question, plotting, gadfly\
**Created:** [January 24, 2023, 4:16pm UTC](https://discourse.julialang.org/t/plotting-histograms-with-gadfly/93467 "2023-01-24T16:16:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)\
**Post date:** [January 24, 2023, 4:16pm UTC](https://discourse.julialang.org/t/plotting-histograms-with-gadfly/93467/1 "2023-01-24T16:16:35Z")

</div>

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

```julia
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))

```

 ![Screenshot 2023-01-24 at 18.15.48](https://global.discourse-cdn.com/julialang/original/3X/4/1/4184fe2565298e6a5157c1255040fcbfb3cfd227.png)

---

<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:** [January 24, 2023, 11:16pm UTC](https://discourse.julialang.org/t/plotting-histograms-with-gadfly/93467/2 "2023-01-24T23:16:01Z")

</div>

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.

```julia
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)

```

![issue1422](https://global.discourse-cdn.com/julialang/original/3X/3/d/3dc152ee8b52361f9fc4e8b8046d7682ce7580bd.png)

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](https://github.com/GiovineItalia/Gadfly.jl/issues/1422). I have had some thoughts about separating `color` and `group`, but needs testing.

---

<div class="post-metadata">

**Author:** ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)\
**Post date:** [January 24, 2023, 11:33pm UTC](https://discourse.julialang.org/t/plotting-histograms-with-gadfly/93467/3 "2023-01-24T23:33:41Z")

</div>

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

---

<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:** [January 24, 2023, 11:49pm UTC](https://discourse.julialang.org/t/plotting-histograms-with-gadfly/93467/4 "2023-01-24T23:49:27Z")

</div>

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

```julia
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),
    Guide.title("Group=:Dist")
)

```

![issue1489](https://global.discourse-cdn.com/julialang/original/3X/f/9/f9ade48a8c5d9fc00e6b4ac13955916f0d55595b.png)

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