Error encountered for Geom.density2d with logarithmic scale in Gadfly

Gadfly displayed error when trying to do Geom.density2d with logarithmic axis/scale. Code below. The first two plots are OK, but not the third plot.

using Gadfly

x = [0.025, 0.028, 0.035, 0.036, 0.037, 0.039, 0.039, 0.041, 0.041, 0.041, 0.042, 0.043, 0.045, 0.047, 0.049, 0.051, 0.053, 0.053, 0.055, 0.056, 0.057, 0.057, 0.059, 0.063, 0.063, 0.067, 0.067, 0.069, 0.07, 0.072, 0.073, 0.075, 0.076, 0.076, 0.078, 0.082, 0.083, 0.083, 0.084, 0.085, 0.085, 0.086, 0.09, 0.092, 0.092, 0.093, 0.095, 0.095, 0.096, 0.103, 0.103,
0.104, 0.104, 0.105, 0.105, 0.106, 0.107, 0.107, 0.108, 0.109, 0.11, 0.117, 0.119, 0.119, 0.12, 0.126, 0.133, 0.133, 0.135, 0.135, 0.135, 0.137, 0.138, 0.141, 0.144, 0.144, 0.145, 0.145, 0.146, 0.146, 0.149, 0.151, 0.155, 0.155, 0.159, 0.162, 0.162, 0.163, 0.164, 0.167, 0.17, 0.171, 0.172, 0.179, 0.181, 0.187, 0.187, 0.188, 0.188, 0.201, 0.204, 0.204, 0.205, 0.208, 0.208, 0.213, 0.214, 0.214, 0.218, 0.221, 0.222, 0.23, 0.231, 0.238, 0.239, 0.245, 0.245, 0.25, 0.256, 0.256, 0.258, 0.262, 0.262, 0.263, 0.264, 0.267, 0.269, 0.273, 0.273, 0.276, 0.279, 0.288, 0.292, 0.296, 0.296, 0.297, 0.298, 0.301, 0.322, 0.324, 0.328, 0.332, 0.336, 0.341, 0.347, 0.354, 0.357, 0.365, 0.376]
y = [0.018, 4.0, 0.288, 0.157, 0.037, 0.163, 0.06499999, 1.0, 0.09199999, 0.203, 0.26, 0.028, 0.243, 4.0, 2.0, 0.852, 58.0, 4.0, 0.023, 2.48, 4.0, 3.0, 14.0, 37.3, 0.121, 2.0, 12.0,
4.0, 19.0, 4.0, 0.038, 1.0, 2.0, 0.146, 0.061, 0.127, 2.0, 203.0, 2.0, 2.0, 0.278, 2.0, 0.229, 2.0, 0.3, 4.0, 4.0, 0.146, 289.0001, 4.0, 2.0, 2.0, 0.163, 1.0, 0.266, 4.0, 3.0, 1.0, 9.0, 3.0, 0.171, 1.0, 0.264, 10.0, 0.374, 1.0, 2.0, 2.0, 25.0, 2.0, 0.582, 2.0, 15.0, 2.0, 2.0, 3.0, 2.0, 2.0, 2.0, 1.93, 9.0, 2.0, 19.0, 0.684, 4.0, 2.0, 2.0, 2.0, 19.0, 2.0, 6.0, 2.0, 5.0, 2.0, 4.0, 1351.0, 3.0, 2.0, 4.0, 4.0, 12.0, 9.0, 6.0, 6.0, 6.0, 1291.0, 5.0, 79.00001, 28.0, 7.0, 5.0, 60.0, 16.0, 15.0, 14.0, 49.0, 9.0, 19.0, 26.0, 35.0, 79.00001, 26.0, 4.0, 192.0, 79.00001, 26.0, 35.0, 148.0, 31.0, 46.99999, 79.00001, 456.0, 88.0, 21.0, 149.0, 31.0, 465.0001, 19.0, 81.00001, 48.0, 139.0, 938.9999, 303.0, 214.0, 440.9999, 168.0, 367.0, 196.0, 437.9999]

plot(x=x, y=y, Geom.point, Scale.y_log10) # plot#1
plot(x=x, y=y, Geom.point, Geom.density2d) # plot#2
plot(x=x, y=y, Geom.point, Geom.density2d, Scale.y_log10) # plot#3

The error message is below.

DomainError with -0.014589995486293805:
NaN result for non-NaN input.
nan_dom_err at math.jl:404 [inlined]
log10(::Float64) at math.jl:550
apply_scale_typed!(::Array{Float64,1}, ::Array{Float64,1}, ::Gadfly.Scale.ContinuousScale) at scale.jl:268
apply_scale(::Gadfly.Scale.ContinuousScale, ::Array{Gadfly.Aesthetics,1}, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at scale.jl:221
apply_statistic(::Gadfly.Stat.ContourStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at statistics.jl:1533
apply_statistic(::Gadfly.Stat.Density2DStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at statistics.jl:526
apply_statistics(::Array{Gadfly.StatisticElement,1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at statistics.jl:33
render_prepare(::Plot) at Gadfly.jl:697
render(::Plot) at Gadfly.jl:755
draw at Gadfly.jl:864 [inlined]
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("application/prs.juno.plotpane+html")}, ::Plot) at Gadfly.jl:971
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::String, ::Plot) at multimedia.jl:109
displayinplotpane(::Plot) at showdisplay.jl:51
displayandrender(::Plot) at showdisplay.jl:131
(::Atom.var"#201#205")() at eval.jl:177
#invokelatest#1 at essentials.jl:712 [inlined]
invokelatest at essentials.jl:711 [inlined]
(::Atom.var"#200#204"{String,String})() at eval.jl:176
withpath(::Atom.var"#200#204"{String,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:9
#199 at eval.jl:161 [inlined]
with_logstate(::Atom.var"#199#203"{String,String}, ::Base.CoreLogging.LogState) at logging.jl:398
with_logger at logging.jl:505 [inlined]
#198 at eval.jl:160 [inlined]
hideprompt(::Atom.var"#198#202"{String,String}) at repl.jl:140
macro expansion at dynamic.jl:24 [inlined]
evalall(::String, ::Nothing, ::String) at eval.jl:150
macro expansion at eval.jl:39 [inlined]
(::Atom.var"#172#173")() at task.jl:358

Is this expected or bug? Thanks.

It’s a bug. I started a PR (Stat.contour with Scale transformation by Mattriks · Pull Request #1432 · GiovineItalia/Gadfly.jl · GitHub).

Thanks. I’ve pulled in latest Gadfly.jl#master. It works great.

Then, I tried to change line color, as per code below - plot#1 works, but not plot#2.
Plot#2 follows the documentation (color_continuous) as I would like to change the line color for this layer. However, error encountered.

using Gadfly
using Colors

x = [0.025, 0.028, 0.035, 0.036, 0.037, 0.039, 0.039, 0.041, 0.041, 0.041, 0.042, 0.043, 0.045, 0.047, 0.049, 0.051, 0.053, 0.053, 0.055, 0.056, 0.057, 0.057, 0.059, 0.063, 0.063, 0.067, 0.067, 0.069, 0.07, 0.072, 0.073, 0.075, 0.076, 0.076, 0.078, 0.082, 0.083, 0.083, 0.084, 0.085, 0.085, 0.086, 0.09, 0.092, 0.092, 0.093, 0.095, 0.095, 0.096, 0.103, 0.103,
0.104, 0.104, 0.105, 0.105, 0.106, 0.107, 0.107, 0.108, 0.109, 0.11, 0.117, 0.119, 0.119, 0.12, 0.126, 0.133, 0.133, 0.135, 0.135, 0.135, 0.137, 0.138, 0.141, 0.144, 0.144, 0.145, 0.145, 0.146, 0.146, 0.149, 0.151, 0.155, 0.155, 0.159, 0.162, 0.162, 0.163, 0.164, 0.167, 0.17, 0.171, 0.172, 0.179, 0.181, 0.187, 0.187, 0.188, 0.188, 0.201, 0.204, 0.204, 0.205, 0.208, 0.208, 0.213, 0.214, 0.214, 0.218, 0.221, 0.222, 0.23, 0.231, 0.238, 0.239, 0.245, 0.245, 0.25, 0.256, 0.256, 0.258, 0.262, 0.262, 0.263, 0.264, 0.267, 0.269, 0.273, 0.273, 0.276, 0.279, 0.288, 0.292, 0.296, 0.296, 0.297, 0.298, 0.301, 0.322, 0.324, 0.328, 0.332, 0.336, 0.341, 0.347, 0.354, 0.357, 0.365, 0.376]
y = [0.018, 4.0, 0.288, 0.157, 0.037, 0.163, 0.06499999, 1.0, 0.09199999, 0.203, 0.26, 0.028, 0.243, 4.0, 2.0, 0.852, 58.0, 4.0, 0.023, 2.48, 4.0, 3.0, 14.0, 37.3, 0.121, 2.0, 12.0,
4.0, 19.0, 4.0, 0.038, 1.0, 2.0, 0.146, 0.061, 0.127, 2.0, 203.0, 2.0, 2.0, 0.278, 2.0, 0.229, 2.0, 0.3, 4.0, 4.0, 0.146, 289.0001, 4.0, 2.0, 2.0, 0.163, 1.0, 0.266, 4.0, 3.0, 1.0, 9.0, 3.0, 0.171, 1.0, 0.264, 10.0, 0.374, 1.0, 2.0, 2.0, 25.0, 2.0, 0.582, 2.0, 15.0, 2.0, 2.0, 3.0, 2.0, 2.0, 2.0, 1.93, 9.0, 2.0, 19.0, 0.684, 4.0, 2.0, 2.0, 2.0, 19.0, 2.0, 6.0, 2.0, 5.0, 2.0, 4.0, 1351.0, 3.0, 2.0, 4.0, 4.0, 12.0, 9.0, 6.0, 6.0, 6.0, 1291.0, 5.0, 79.00001, 28.0, 7.0, 5.0, 60.0, 16.0, 15.0, 14.0, 49.0, 9.0, 19.0, 26.0, 35.0, 79.00001, 26.0, 4.0, 192.0, 79.00001, 26.0, 35.0, 148.0, 31.0, 46.99999, 79.00001, 456.0, 88.0, 21.0, 149.0, 31.0, 465.0001, 19.0, 81.00001, 48.0, 139.0, 938.9999, 303.0, 214.0, 440.9999, 168.0, 367.0, 196.0, 437.9999]

palettef = Scale.lab_gradient("purple", "blue", "green", "orange", "red")

# Plot#1
plot(x=x, y=y, Geom.point, order=1,
    layer(x=x, y=y, Geom.density2d(levels=7), order=2, Theme(line_width=0.07mm)),
    Scale.y_log10,
    Scale.color_continuous(colormap=palettef)
)

# Plot#2
plot(x=x, y=y, Geom.point, order=1,
    layer(x=x, y=y, Geom.density2d(levels=7), order=2, Theme(line_width=0.07mm), continuous_color_scale(colormap=palettef)),
    Scale.y_log10,
)

Error mesage.

ERROR: LoadError: UndefVarError: continuous_color_scale not defined
Stacktrace:
 [1] top-level scope at D:\codes\Git\Gasfly_log_density2d.jl:19
in expression starting at D:\codes\Git\Gasfly_log_density2d.jl:19

What is the correct way to change “Geom.density2d” line color within this layer? Thanks.

Including a Scale (e.g. Scale.color_continuous) in a Layer or Theme currently has problems that would require a detailed technical explanation. I assume that plot #1 gives you the expected colors.

Plot#1 will only be OK if no color aesthetics defined in one (or any?) of the layer, e.g. in Geom.point layer. If color aesthetics and Scale.color_continuous(colormap=palettef) are defined together, error encountered. Sample code below. Perhaps, have to wait until Theme(continuous_color_scale(colormap=palettef)) bug fix.

using Gadfly

x = [0.025, 0.028, 0.035, 0.036, 0.037, 0.039, 0.039, 0.041, 0.041, 0.041, 0.042, 0.043, 0.045, 0.047, 0.049, 0.051, 0.053, 0.053, 0.055, 0.056, 0.057, 0.057, 0.059, 0.063, 0.063, 0.067, 0.067, 0.069, 0.07, 0.072, 0.073, 0.075, 0.076, 0.076, 0.078, 0.082, 0.083, 0.083, 0.084, 0.085, 0.085, 0.086, 0.09, 0.092, 0.092, 0.093, 0.095, 0.095, 0.096, 0.103, 0.103,
0.104, 0.104, 0.105, 0.105, 0.106, 0.107, 0.107, 0.108, 0.109, 0.11, 0.117, 0.119, 0.119, 0.12, 0.126, 0.133, 0.133, 0.135, 0.135, 0.135, 0.137, 0.138, 0.141, 0.144, 0.144, 0.145, 0.145, 0.146, 0.146, 0.149, 0.151, 0.155, 0.155, 0.159, 0.162, 0.162, 0.163, 0.164, 0.167, 0.17, 0.171, 0.172, 0.179, 0.181, 0.187, 0.187, 0.188, 0.188, 0.201, 0.204, 0.204, 0.205, 0.208, 0.208, 0.213, 0.214, 0.214, 0.218, 0.221, 0.222, 0.23, 0.231, 0.238, 0.239, 0.245, 0.245, 0.25, 0.256, 0.256, 0.258, 0.262, 0.262, 0.263, 0.264, 0.267, 0.269, 0.273, 0.273, 0.276, 0.279, 0.288, 0.292, 0.296, 0.296, 0.297, 0.298, 0.301, 0.322, 0.324, 0.328, 0.332, 0.336, 0.341, 0.347, 0.354, 0.357, 0.365, 0.376]
y = [0.018, 4.0, 0.288, 0.157, 0.037, 0.163, 0.06499999, 1.0, 0.09199999, 0.203, 0.26, 0.028, 0.243, 4.0, 2.0, 0.852, 58.0, 4.0, 0.023, 2.48, 4.0, 3.0, 14.0, 37.3, 0.121, 2.0, 12.0,
4.0, 19.0, 4.0, 0.038, 1.0, 2.0, 0.146, 0.061, 0.127, 2.0, 203.0, 2.0, 2.0, 0.278, 2.0, 0.229, 2.0, 0.3, 4.0, 4.0, 0.146, 289.0001, 4.0, 2.0, 2.0, 0.163, 1.0, 0.266, 4.0, 3.0, 1.0, 9.0, 3.0, 0.171, 1.0, 0.264, 10.0, 0.374, 1.0, 2.0, 2.0, 25.0, 2.0, 0.582, 2.0, 15.0, 2.0, 2.0, 3.0, 2.0, 2.0, 2.0, 1.93, 9.0, 2.0, 19.0, 0.684, 4.0, 2.0, 2.0, 2.0, 19.0, 2.0, 6.0, 2.0, 5.0, 2.0, 4.0, 1351.0, 3.0, 2.0, 4.0, 4.0, 12.0, 9.0, 6.0, 6.0, 6.0, 1291.0, 5.0, 79.00001, 28.0, 7.0, 5.0, 60.0, 16.0, 15.0, 14.0, 49.0, 9.0, 19.0, 26.0, 35.0, 79.00001, 26.0, 4.0, 192.0, 79.00001, 26.0, 35.0, 148.0, 31.0, 46.99999, 79.00001, 456.0, 88.0, 21.0, 149.0, 31.0, 465.0001, 19.0, 81.00001, 48.0, 139.0, 938.9999, 303.0, 214.0, 440.9999, 168.0, 367.0, 196.0, 437.9999]
c = ["A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
"B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B",
"C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C","C",
"D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D","D",
"E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E","E",
"F","F","F","F","F","F","F","F","F","F","F","F","F","F"]

palettef = Scale.lab_gradient("purple", "blue", "green", "orange", "red")

l1 = layer(x=x, y=y, Geom.density2d(levels=7), order=2)
l2 = layer(x=x, y=y, color=c, Geom.point)

# Plot#1
plot(l1, l2,
    Scale.y_log10,
    Scale.color_continuous(colormap=palettef), # Error encountered,
)

Error message below.

MethodError: no method matching -(::String, ::String)
optimize_ticks_typed(::String, ::String, ::Bool, ::Array{Tuple{Float64,Float64},1}, ::Int64, ::Int64, ::Int64, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool) at ticks.jl:73
optimize_ticks(::String, ::String; extend_ticks::Bool, Q::Array{Tuple{Float64,Float64},1}, k_min::Int64, k_max::Int64, k_ideal::Int64, granularity_weight::Float64, simplicity_weight::Float64, coverage_weight::Float64, niceness_weight::Float64, strict_span::Bool) at ticks.jl:61
(::Gadfly.var"#optimize_ticks##kw")(::NamedTuple{(:strict_span,),Tuple{Bool}}, ::typeof(Gadfly.optimize_ticks), ::String, ::String) at ticks.jl:60
apply_scale(::Gadfly.Scale.ContinuousColorScale, ::Array{Gadfly.Aesthetics,1}, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at scale.jl:658
apply_scales(::IterTools.Distinct{Base.ValueIterator{Dict{Symbol,Gadfly.ScaleElement}},Gadfly.ScaleElement}, ::Array{Gadfly.Aesthetics,1}, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at scale.jl:24
apply_scales(::IterTools.Distinct{Base.ValueIterator{Dict{Symbol,Gadfly.ScaleElement}},Gadfly.ScaleElement}, ::Gadfly.Data, ::Gadfly.Data) at scale.jl:34
render_prepare(::Plot) at Gadfly.jl:662
render(::Plot) at Gadfly.jl:755
draw at Gadfly.jl:864 [inlined]
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::MIME{Symbol("application/prs.juno.plotpane+html")}, ::Plot) at Gadfly.jl:971
show(::IOContext{Base.GenericIOBuffer{Array{UInt8,1}}}, ::String, ::Plot) at multimedia.jl:109
displayinplotpane(::Plot) at showdisplay.jl:51
displayandrender(::Plot) at showdisplay.jl:131
(::Atom.var"#201#205")() at eval.jl:177
#invokelatest#1 at essentials.jl:712 [inlined]
invokelatest at essentials.jl:711 [inlined]
(::Atom.var"#200#204"{String,String})() at eval.jl:176
withpath(::Atom.var"#200#204"{String,String}, ::String) at utils.jl:30
withpath(::Function, ::String) at eval.jl:9
#199 at eval.jl:161 [inlined]
with_logstate(::Atom.var"#199#203"{String,String}, ::Base.CoreLogging.LogState) at logging.jl:398
with_logger at logging.jl:505 [inlined]
#198 at eval.jl:160 [inlined]
hideprompt(::Atom.var"#198#202"{String,String}) at repl.jl:140
macro expansion at dynamic.jl:24 [inlined]
evalall(::String, ::Nothing, ::String) at eval.jl:150
macro expansion at eval.jl:39 [inlined]
(::Atom.var"#172#173")() at task.jl:358

The issue here is that you have 1 discrete color scale (c is discrete) and 1 continuous color scale (supplied). The Gadfly team agree this is an important feature, and it’s on the current Roadmap #1385 (last dot point). Your plot above is similar to issue #1098, see there for possible workarounds.

Please add my vote for this feature. Based on the sample code in issue #1098, the workaround that I can think of would be to calculate unique color group, prepare the palette, then themes that particular layer with the palette.

I started PR #1436, which adds a second color scale.

1 Like