# Plotting a histogram in Julia

**URL:** <https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361>\
**Category:** New to Julia\
**Tags:** plotting\
**Created:** [February 1, 2019, 4:56pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361 "2019-02-01T16:56:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 4:56pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/1 "2019-02-01T16:56:38Z")

</div>

I have an array of ages and I want to plot a histogram. The Julia docs have nothing about histograms so following the histogram example [here](https://lectures.quantecon.org/jl/julia_by_example.html) I tried calling the histogram function, but got the error below.

How do I plot a histogram in Julia? I’m using the shell, but could do this in IJulia.

julia\> ages  
943-element Array{Any,1}:  
24  
53  
23

julia\> histogram(ages)  
/home/dean/.local/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.  
% get\_backend())

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 1, 2019, 5:12pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/2 "2019-02-01T17:12:01Z")

</div>

What packages are you using? I assume, based on your error message, that you’re using PyPlot and there’s some error related to that. The Plots package uses GR as its default backend and it works well for histograms. I also like to use the Gadfly package. There’s a lot of possible tools to do that in Julia, but you need to give us more information about what yo0u’re doing to help you debug it.

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 5:28pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/3 "2019-02-01T17:28:28Z")

</div>

Here’s what I’m using (Distributions might not be necessary):

julia\> using Plots  
julia\> pyplot()  
import Pkg; Pkg.add(“Distributions”)  
julia\> using Distributions

Subsequent calls to histogram(ages) now returns without error, but no plot is displayed.

Is there documentation somewhere that covers histogram()? I searched the Julia docs and it showed no results.

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 1, 2019, 5:31pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/4 "2019-02-01T17:31:22Z")

</div>

You have to go to the package documentation [here](http://docs.juliaplots.org/latest/examples/pyplot/), where you can see specific examples for PyPlot. I suggest you try other backends too, as mentioned before, GR is the default and works very well.

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 5:40pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/5 "2019-02-01T17:40:52Z")

</div>

Opened a new Julia shell:  
julia\> using Plots

julia\> histogram(randn(1000), bins=:scott, weights=repeat(1:5, outer=200))  
ERROR: error compiling \_plot!: error compiling \_display: could not load library “libGR.so”  
libGR.so: cannot open shared object file: No such file or directory  
Stacktrace:  
[1] #plot#132(::Base.Iterators.Pairs{Symbol,Any,Tuple{Symbol,Symbol,Symbol},NamedTuple{(:bins, :weights, :seriestype),Tuple{Symbol,Array{Int64,1},Symbol}}}, ::Function, ::Array{Float64,1}) at /home/dean/.julia/packages/Plots/cxnAH/src/plot.jl:57  
[2] #plot at ./none:0 [inlined]  
[3] #histogram#359 at /home/dean/.julia/packages/RecipesBase/Uz5AO/src/RecipesBase.jl:368 [inlined]  
[4] (::getfield(Plots, Symbol(“#kw##histogram”)))(::NamedTuple{(:bins, :weights),Tuple{Symbol,Array{Int64,1}}}, ::typeof(histogram), ::Array{Float64,1}) at ./none:0  
[5] top-level scope at none:0

So something is wrong with the GR backend on my Julia instance (1.0.3 Ubuntu 16.04). Is this a bug in Julia?

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 1, 2019, 5:50pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/6 "2019-02-01T17:50:17Z")

</div>

Try ] add GR

Your problem seems related to the tools outside of Julia, so you need to make sure you have GR up and running in your computer. These problems can be frustrating, I know, and I’ve had my share of those, but usually you have to make sure you have the package ([GR.jl](https://github.com/jheinen/GR.jl) in this case), and sometimes you have to do ] build GR to make sure everything works. Another useful tool is to use ] test GR, if all tests pass, your package should run.

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 5:59pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/7 "2019-02-01T17:59:03Z")

</div>

Installed 1.1.0. Opened new Julia shell:  
julia\> import Pkg; Pkg.add(“Plots”)  
julia\> Pkg.add(“PyPlot”)  
julia\> pyplot()  
[Info: Precompiling PyPlot [d330b81b-6aea-500a-939a-2ce795aea3ee]  
┌ Warning: No working GUI backend found for matplotlib  
└ @ PyPlot ~/.julia/packages/PyPlot/mQXSC/src/init.jl:160  
Plots.PyPlotBackend()

julia\> histogram(randn(1000), bins=:scott, weights=repeat(1:5, outer=200))  
/home/dean/.local/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.  
% get\_backend())

julia\> Pkg.add(“GR”)  
Resolving package versions…  
Updating `~/.julia/environments/v1.1/Project.toml`  
[28b8d3ca] + GR v0.37.0  
Updating `~/.julia/environments/v1.1/Manifest.toml`  
[no changes]

julia\> histogram(randn(1000), bins=:scott, weights=repeat(1:5, outer=200))

No output. histogram() just returns.

Do I need to add a GUI backend?

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 1, 2019, 6:01pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/8 "2019-02-01T18:01:19Z")

</div>

Try

```julia
p = histogram(randn(1000), bins=:scott, weights=repeat(1:5, outer=200))
p

```

or maybe

```julia
display(p)

```

You don’t need the GUI to display plots and in my Ubuntu machine a new window opens with the plot.

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 6:03pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/9 "2019-02-01T18:03:18Z")

</div>

julia\> h = histogram(randn(1000), bins=:scott, weights=repeat(1:5, outer=200))

julia\> display(h)

Still shows nothing.

---

<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:** [February 1, 2019, 6:10pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/10 "2019-02-01T18:10:42Z")

</div>

[http://gr-framework.org/julia.html#installation](http://gr-framework.org/julia.html#installation)

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 6:16pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/11 "2019-02-01T18:16:09Z")

</div>

julia\> import Pkg; Pkg.add(“GR”)  
Updating registry at `~/.julia/registries/General`  
Updating git-repo `https://github.com/JuliaRegistries/General.git`  
Resolving package versions…  
Updating `~/.julia/environments/v1.1/Project.toml`  
[no changes]  
Updating `~/.julia/environments/v1.1/Manifest.toml`  
[no changes]

julia\> using Plots

julia\> pyplot()  
┌ Warning: No working GUI backend found for matplotlib  
└ @ PyPlot ~/.julia/packages/PyPlot/mQXSC/src/init.jl:160  
Plots.PyPlotBackend()

julia\> histogram(randn(1000), bins=:scott, weights=repeat(1:5, outer=200))  
/home/dean/.local/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.  
% get\_backend())

---

<div class="post-metadata">

**Author:** ![alejandromerchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alejandromerchan/32/10500_2.png) [@alejandromerchan](https://discourse.julialang.org/u/alejandromerchan)\
**Post date:** [February 1, 2019, 6:25pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/12 "2019-02-01T18:25:09Z")

</div>

You clearly have a problem with your Python backend, this is not related to Julia. Maybe try [Gadfly](http://gadflyjl.org/stable/)? As far I know, Gadfly is native Julia and very similar to ggplot2 in R. It had some issues with Julia 1.0, but I think those have been solved.

---

<div class="post-metadata">

**Author:** ![jheinen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jheinen/32/2787_2.png) [@jheinen](https://discourse.julialang.org/u/jheinen)\
**Post date:** [February 1, 2019, 6:47pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/13 "2019-02-01T18:47:19Z")

</div>

Please try:

```julia
ENV["GRDIR"]=""
] build GR
using GR
histogram(randn(10000))

```

If that works, you can restart Julia and switch to Plots:

```julia
using Plots
histogram(randn(10000))

```

For any reason, your installation is/was incomplete.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [February 1, 2019, 7:43pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/14 "2019-02-01T19:43:23Z")

</div>

Here is what I get:

```plaintext
julia> using Plots; pyplot()
Plots.PyPlotBackend()
julia> ages = 0:120;
julia> population = rand(ages,2000);
julia> histogram(population)

```

leads to (Juno):  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/8/6829c7da7daed3266f3aa61cbc20e010cd780aca.png)

---

<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:** [February 1, 2019, 8:34pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/15 "2019-02-01T20:34:41Z")

</div>

You can also try [VegaLite.jl](https://github.com/fredo-dedup/VegaLite.jl):

```julia
using VegaLite, Query

ages |>
  @map({age=_}) |>
  @vlplot(:bar, x={:age, bin=true}, y="count()")

```

The `@map` call turns the array into a table (VegaLite.jl needs the input data as a table).

A little more verbose than the other options mentioned in the text here because it follows a grammar of graphics API, but on the flip side that can also be quite powerful.

---

<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:** [February 1, 2019, 8:44pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/16 "2019-02-01T20:44:36Z")

</div>

Are more intuitive syntaxes for things like this, and trendlines, etc., in the works for VegaLite, or are there philosophical reasons to not go that way?

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 9:32pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/17 "2019-02-01T21:32:07Z")

</div>

I get:  
julia\> histogram(population)  
/home/dean/.local/lib/python3.5/site-packages/matplotlib/figure.py:445: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.  
% get\_backend())

There’s got to be a setup step that I’m missing.

---

<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:** [February 1, 2019, 9:33pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/18 "2019-02-01T21:33:30Z")

</div>

Try following the steps here [Plotting a histogram in Julia - #13 by jheinen](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/13)

---

<div class="post-metadata">

**Author:** ![DWSchulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dwschulze/32/35968_2.png) [@DWSchulze](https://discourse.julialang.org/u/DWSchulze)\
**Post date:** [February 1, 2019, 9:40pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/19 "2019-02-01T21:40:04Z")

</div>

That worked. Thanks. I guess I needed to build GR from pkg\> mode. This should probably be added to the docs.

Now that the histogram is being displayed what do I have to do to save it to a file?

---

<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:** [February 1, 2019, 10:39pm UTC](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361/20 "2019-02-01T22:39:06Z")

</div>

`savefig("myawesomefigure.png")`

[Next page](https://discourse.julialang.org/t/plotting-a-histogram-in-julia/20361.md?page=2)
