# How to plot histogram based on bin width and bin height?

**URL:** <https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661>\
**Category:** General Usage\
**Tags:** plotting\
**Created:** [November 17, 2018, 10:28pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661 "2018-11-17T22:28:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 17, 2018, 10:28pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/1 "2018-11-17T22:28:40Z")

</div>

e.g:  
given:  
Bins: [0,1), [1,2)  
Height: [3,5]

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 18, 2018, 12:22am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/2 "2018-11-18T00:22:30Z")

</div>

What plotting packages have you tried using?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 18, 2018, 1:47am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/3 "2018-11-18T01:47:47Z")

</div>

Plots.jl and Gadfly.jl

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 18, 2018, 1:59am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/4 "2018-11-18T01:59:41Z")

</div>

In [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) it would look like this:

```julia
using DataFrames, VegaLite

df = DataFrame(bin_start=[0., 1.], bin_end=[1,2.], count=[3,5])

df |>
  @vlplot(:bar, x={:bin_start, bin=:binned, axis={tickStep=1}}, x2=:bin_end, y=:count)

```

That right now returns:  
 ![example](https://global.discourse-cdn.com/julialang/original/3X/1/b/1b502059c4748ab33122b9ca6006bafdb5688d01.png)

Unfortunately the x axis labels are not looking great… I think that is a bug in vega-lite 2.x (the underlying JavaScript library that produces the actual plot). If I run the same spec through vega-lite 3.0.0-rc8 I get something that looks right:  
 ![visualization%20(3)](https://global.discourse-cdn.com/julialang/original/3X/7/9/790809e3ffe8358712150c7423b5cfe5c4592634.png)

So I guess the story is that once vega-lite 3.0.0 is released, I will update [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl) to use it, and then it should look good out of the box. For now, one thing you can do is to plot the thing then click the button to open the spec in the “Vega Editor”. That will open up the online vega-lite editor, and that is based on vega-lite 3.0.0-rc8, and things look good there. You can then download the plot from there. Certainly not good, but the best option right now until the new version is released.

Oh, and the docs for pre-binned histograms is [here](https://vega.github.io/vega-lite/docs/bin.html#binned).

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 18, 2018, 2:59am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/5 "2018-11-18T02:59:02Z")

</div>

Thx for the reply, currently trying to use some native Julia libs.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 18, 2018, 3:31am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/6 "2018-11-18T03:31:01Z")

</div>

Isn’t Plots.jl using a non native Julia backend in the same way that VegaLite.jl does? I think the only pure Julia story is Gadfly.jl.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [November 18, 2018, 3:33am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/7 "2018-11-18T03:33:04Z")

</div>

Good point, it’s deceiving since Plots.jl sounds like a native name.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 18, 2018, 3:40am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/8 "2018-11-18T03:40:13Z")

</div>

Actually, I think Winston.jl might also be pure Julia? But not sure whether it is still maintained.

In general, I think you’ll find more options if you include Julia wrappers of non-Julia packages. Some of them work really well.

---

<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:** [November 18, 2018, 4:16am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/9 "2018-11-18T04:16:03Z")

</div>

Gadfly has `Geom.bar` (and `Geom.histogram`). See the gallery examples here: [http://gadflyjl.org/stable/gallery/geometries.html](http://gadflyjl.org/stable/gallery/geometries.html)  
or more info in the library:  
[http://gadflyjl.org/stable/lib/geometries.html](http://gadflyjl.org/stable/lib/geometries.html).

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [November 18, 2018, 11:47am UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/10 "2018-11-18T11:47:42Z")

</div>

> [@davidanthoff](#):
>
> Isn’t Plots.jl using a non native Julia backend in the same way that VegaLite.jl does? I think the only pure Julia story is Gadfly.jl.

AFAIU Makie is using OpenGL at the end of the pipeline to physically “draw” primitives but the logic converting a custom julia type (or a combination of those) to a set of primitives to be drawn is in Julia. This is Plots logic taken to the extreme (Plots has a bit of a hybrid approach and sometimes out-sources “non-primitive” drawings to the backend). The trade-off in my mind is that “one-to-one” wrappers to a mature library in another language (and there are many excellent examples, PlotlyJS, PyPlot, VegaLite…) can refer user to the library documentation and are generally more “time-tested” (especially to plot objects that can be easily translated across languages, say vectors or tables of strings or numbers) whereas the Plots / Makie “recipe” approach shines when plotting custom Julia types.

---

<div class="post-metadata">

**Author:** ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)\
**Post date:** [November 18, 2018, 4:42pm UTC](https://discourse.julialang.org/t/how-to-plot-histogram-based-on-bin-width-and-bin-height/17661/11 "2018-11-18T16:42:02Z")

</div>

@piever, good point!

I guess the question is why one wants a pure julia solution. If it is mainly to avoid dependencies that might go wrong, then probably Gadfly.jl and Winston.jl are the best (and maybe only?) option. If it is because one wants all the logic in pure julia, but less about dependency management, then Plots.jl and Makie.jl probably also fit the bill.

I think we still don’t have a good handle on the binary dependency story in the plotting area… I think as long as Cairo.jl and Rsvg.jl depend on things like winrpm and homebrew, we won’t really see a super stable situation that “just works”. It has gotten much better, but I’m still getting a lot of errors reports that VegaLite.jl doesn’t install properly, and they all turn out to be Cairo.jl deployment issues. Whoever gets the whole Gtk.jl/Cairo.jl/Rsvg.jl stack moved over to BinaryBuilder.jl will be my hero forever!!
