Plotting a histogram in Julia

plotting

#1

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 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())


#2

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.


#3

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.


#4

You have to go to the package documentation here, 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.


#5

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?


#6

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 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.


#7

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?


#8

Try

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

or maybe

display(p)

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


#9

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

julia> display(h)

Still shows nothing.


#10

http://gr-framework.org/julia.html#installation


#11

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())


#12

You clearly have a problem with your Python backend, this is not related to Julia. Maybe try Gadfly? 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.


#13

Please try:

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

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

using Plots
histogram(randn(10000))

For any reason, your installation is/was incomplete.


#14

Here is what I get:

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

leads to (Juno):
image


#15

You can also try VegaLite.jl:

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.


#16

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?


#17

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.


#18

Try following the steps here Plotting a histogram in Julia


#19

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?


#20

savefig("myawesomefigure.png")