Plotting a histogram in Julia

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?

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.

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?

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.

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

julia> display(h)

Still shows nothing.

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

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

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.

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.

1 Like

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

1 Like

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.

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?

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.

Try following the steps here Plotting a histogram in Julia

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?

1 Like

savefig("myawesomefigure.png")

The problem was

julia> pyplot()

which tells Plots to use the Matplotlib backend, even if GR is correctly installed.

On trendlines, the answer is simple: yes. But that work needs to happen in vega-lite itself, not the julia wrapper. I know the dev team there has that on their roadmap, so hopefully some day it will just magically appear :slight_smile:

The question on alternative APIs is a bit more difficult. My thinking right now is that I don’t think more canned plotting APIs belong in VegaLite.jl itself, I think that package should probably stick to the grammar of graphics API style. BUT, I think it would be awesome to have another package that sits on top of VegaLite.jl that might have APIs like histogram(data). The Vega.jl package actually had that sort of API, but I think that package is not active/working right now (not 100% sure, though). Another option would probably be to create a vega/vega-lite backend for Plots.jl, right?

That’s an option, but in my way making it a Plots backend would take away a lot of what’s nice with VegaLite, which is the GOG interface. Well, maybe VegaLite JSON could be automatically translated to Plots recipes, at which point the functionality would go both ways - an interesting project!

Well, I certainly wouldn’t want to take the GOG interface away! But I think we could also provide a more Plots.jl like interface. And I just don’t know whether that would be better achieved via a Plots.jl backend or a separate, standalone API. Or something Makie.jl related? I have to admit I’m not on top of the current roadmap for that ecosystem :slight_smile: