# Bincount of histogram

**URL:** <https://discourse.julialang.org/t/bincount-of-histogram/35991>\
**Category:** General Usage\
**Tags:** question, package, plotting\
**Created:** [March 14, 2020, 11:40pm UTC](https://discourse.julialang.org/t/bincount-of-histogram/35991 "2020-03-14T23:40:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)\
**Post date:** [March 14, 2020, 11:40pm UTC](https://discourse.julialang.org/t/bincount-of-histogram/35991/1 "2020-03-14T23:40:01Z")

</div>

I’d like to adjust bin count. After using `Gadfly`, the plot() became undefined.

```julia
using Distributions
    function a()
        jump=rand(TDist(1),10000)#12>10
        for j in 1:length(jump)
         while abs(jump[j])<1 #jump[j]==0
             jump[j]=4*rand(TDist(1),1)[1] 
         end
        end
            return jump
    end
    jump=a()
    jump2=rand(TDist(1),10000)#nature t
    jump4=rand(Normal(0,1),10000)#approximate Gaussian distribution

using Plots
  histogram(jump,alpha=0.3,xlims=(-25,25),label="limited t",normalize=:probability,bins=10000)#,bins=10000 doesn't work
  histogram!(jump2,alpha=0.8,label="nature t",normalize=:probability)
  histogram!(jump4,alpha=0.5,label="Gaussian",normalize=:probability)

```

The bin width vary a lot, I’d like to make them dense and uniform, so found bincount in Gadfly. This start another disaster (In previous post people sove the undefined plot() problem by delete the package file and reinstall all…Are there any other solution available?

```julia
# using Gadfly #Caution: after using it, plot() became undefined
# plot(jump,bincount=10000,Geom.histogram)

using Plots
gr() #didn't make plot() recover
plot(jump) 

(v1.2) pkg> add Plots #after this, still plot() not defined

(v1.2) pkg> build Plots #plot() not defined

```

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 14, 2020, 11:49pm UTC](https://discourse.julialang.org/t/bincount-of-histogram/35991/2 "2020-03-14T23:49:52Z")

</div>

I’m not sure I understand what the question is but I usually use [StatsPlots.jl](https://github.com/JuliaPlots/StatsPlots.jl) for histograms. There’s a `bins` keyword argument you can pass to `histogram` that will accept an integer or you can use one of the following auto-binning algorithms: `:sturges, :sqrt, :rice, :scott or :fd`. Here are some examples:

```julia
using Distributions
using StatsPlots

X = rand(Normal(), 100)
histogram(X, bins=5)

```

![1](https://global.discourse-cdn.com/julialang/original/3X/6/1/6139dc99d50951eec8ec01070f1ba764e9fe712d.png)

```julia
histogram(X, bins=10)

```

![2](https://global.discourse-cdn.com/julialang/original/3X/1/4/149ac07c13fe532e61fd3b9103f32c9a438c371a.png)

```julia
histogram(X, bins=:sturges)

```

![1](https://global.discourse-cdn.com/julialang/original/3X/6/1/6139dc99d50951eec8ec01070f1ba764e9fe712d.png)

Regarding the package not working after having installed Gadfly, I notice that you are working from the default environment `(v1.2) pkg>` so I would suggest setting up separate environments for each of your projects. For now, you might try to do `pkg> rm Gadfly` and then `pkg> build Plots` to remove Gadfly and then rebuild Plots to see if that helps.

---

<div class="post-metadata">

**Author:** ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)\
**Post date:** [March 15, 2020, 12:06am UTC](https://discourse.julialang.org/t/bincount-of-histogram/35991/3 "2020-03-15T00:06:21Z")

</div>

`pkg> rm Gadfly` and then `pkg> build Plots`  
are executed but still

```julia
julia> histogram(jump)
ERROR: UndefVarError: histogram not defined
Stacktrace:
 [1] top-level scope at none:0

julia> plot(jump)
ERROR: UndefVarError: plot not defined
Stacktrace:
 [1] top-level scope at none:0

```

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [March 15, 2020, 12:12am UTC](https://discourse.julialang.org/t/bincount-of-histogram/35991/4 "2020-03-15T00:12:00Z")

</div>

I believe `histogram` is only exported by StatsPlots.jl so that wouldn’t work if you haven’t added StatsPlots. I think I know what the problem is. If Gadfly exports a `plot` function and you are loading both packages, you will need to specify which plot function you are calling like this `Plots.plot(jump)` or `Gadfly.plot(jump)`. Try that.

---

<div class="post-metadata">

**Author:** ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)\
**Post date:** [March 15, 2020, 12:36am UTC](https://discourse.julialang.org/t/bincount-of-histogram/35991/5 "2020-03-15T00:36:22Z")

</div>

You’re right. Thanks a lot. You made my day!!!
