# First-class histogram object and plotting in Plots.jl

**URL:** <https://discourse.julialang.org/t/first-class-histogram-object-and-plotting-in-plots-jl/7674>\
**Category:** Visualization\
**Tags:** question\
**Created:** [December 10, 2017, 4:35pm UTC](https://discourse.julialang.org/t/first-class-histogram-object-and-plotting-in-plots-jl/7674 "2017-12-10T16:35:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 10, 2017, 4:35pm UTC](https://discourse.julialang.org/t/first-class-histogram-object-and-plotting-in-plots-jl/7674/1 "2017-12-10T16:35:56Z")

</div>

I have a dataset with a large number of points, and would like to plot histograms which are costly to calculate. I would prefer if I could save the calculated histogram using `JLD2`, then load it in a separate script for plotting. Eg (MWE, this is of course trivially fast)

```julia
using JLD2
x = randn(100)
f(x) = x^2
# is there a library which has a representation for a histogram,
# independently of its plot ...?
h = histogram(f.(x))
@save "data.jld2" h

```

then

```julia
using Plots; gr()
using JLD2
@load "data.jld2"
plot(h) # ... that I could then plot?

```

---

<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:** [December 10, 2017, 4:47pm UTC](https://discourse.julialang.org/t/first-class-histogram-object-and-plotting-in-plots-jl/7674/2 "2017-12-10T16:47:36Z")

</div>

Simply:

```julia
julia> using StatsBase

julia> h = fit(Histogram, f.(x))
StatsBase.Histogram{Int64,1,Tuple{StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}}}
edges:
  0.0:2.0:14.0
weights: [837, 115, 36, 11, 0, 0, 1]
closed: left
isdensity: false

julia> plot(h)

```

should work (there is a recipe to plot Histogram objects created with StatBase). I know it sounds silly but with Plots it’s kind of impossible to know all the recipes that are implemented. As a rule of thumb I generally try something intuitive and end up either very happy with the result or opening a bug report 🙂

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [December 11, 2017, 7:33am UTC](https://discourse.julialang.org/t/first-class-histogram-object-and-plotting-in-plots-jl/7674/3 "2017-12-11T07:33:22Z")

</div>

As a more general solution, you can use the HDF5 backend:

```julia
hdf5() 
p = histogram(randn(100000))
Plots.hdf5plot_write(p, "myhistogram.hdf5")
#then
gr()
pread = Plots.hdf5plot_read("myhistogram.hdf5")

```

Oh, and

> [@piever](#):
>
> As a rule of thumb I generally try something intuitive and end up either very happy with the result or opening a bug report

To be fair, you generally end up opening a PR 🙂
