GiovineItalia / Gadfly.jl

Crafty statistical graphics for Julia.
http://gadflyjl.org/stable/
Other
1.9k stars 251 forks source link

Gadfly can't plot Unitful.jl Quantities #1104

Closed non-Jedi closed 5 years ago

non-Jedi commented 6 years ago

e.g.:

julia> using Gadfly; using Unitful;

julia> plot(x=[1;2;3]u"m/s", y=[1;2;3]u"s", Geom.line)
Error showing value of type Gadfly.Plot:
ERROR: DimensionError: s and 1 are not dimensionally compatible.
Stacktrace:
 [1] optimize_ticks_typed(::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}, ::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}, ::Bool, ::Array{Tuple{Float64,Float64},1}, ::Int64, ::Int64, ::Int64, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool) at /home/adam/.julia/v0.6/Gadfly/src/ticks.jl:72
 [2] (::Gadfly.#kw##optimize_ticks)(::Array{Any,1}, ::Gadfly.#optimize_ticks, ::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}, ::Unitful.Quantity{Int64,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)},Unitful.FreeUnits{(Unitful.Unit{:Second,Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}(0, 1//1),),Unitful.Dimensions{(Unitful.Dimension{:Time}(1//1),)}}}) at ./<missing>:0
 [3] apply_statistic(::Gadfly.Stat.TickStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at /home/adam/.julia/v0.6/Gadfly/src/statistics.jl:854
 [4] apply_statistics(::Array{Gadfly.StatisticElement,1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at /home/adam/.julia/v0.6/Gadfly/src/statistics.jl:40
 [5] render_prepare(::Gadfly.Plot) at /home/adam/.julia/v0.6/Gadfly/src/Gadfly.jl:707
 [6] render(::Gadfly.Plot) at /home/adam/.julia/v0.6/Gadfly/src/Gadfly.jl:760
 [7] display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::MIME{Symbol("text/html")}, ::Gadfly.Plot) at /home/adam/.julia/v0.6/Gadfly/src/Gadfly.jl:1074
 [8] display(::Base.REPL.REPLDisplay{Base.REPL.LineEditREPL}, ::Gadfly.Plot) at /home/adam/.julia/v0.6/Gadfly/src/Gadfly.jl:1019
 [9] display(::Gadfly.Plot) at ./multimedia.jl:194
 [10] eval(::Module, ::Any) at ./boot.jl:235
 [11] print_response(::Base.Terminals.TTYTerminal, ::Any, ::Void, ::Bool, ::Bool, ::Void) at ./REPL.jl:144
 [12] print_response(::Base.REPL.LineEditREPL, ::Any, ::Void, ::Bool, ::Bool) at ./REPL.jl:129
 [13] (::Base.REPL.#do_respond#16{Bool,Base.REPL.##26#36{Base.REPL.LineEditREPL,Base.REPL.REPLHistoryProvider},Base.REPL.LineEditREPL,Base.LineEdit.Prompt})(::Base.LineEdit.MIState, ::Base.AbstractIOBuffer{Array{UInt8,1}}, ::Bool) at ./REPL.jl:646

The problem I believe is here: https://github.com/GiovineItalia/Gadfly.jl/blob/aa6a1ad45e701a5af5a7761721dd617af8ee5c42/src/ticks.jl#L72

Instead of calling convert(T, one(T)), oneunit(T) should be called. May be other problems, but this is the first one.

bjarthur commented 6 years ago

would be great to fix all of Gadfly's problems when used with Unitful. you are the first i know that have tried the two together. please let us know of other problems as they arise. fixing them all in a single PR would be best.