Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layering two sound plots causes an unexpected error #1421

Closed
NightMachinery opened this issue Apr 18, 2020 · 6 comments · Fixed by #1448
Closed

Layering two sound plots causes an unexpected error #1421

NightMachinery opened this issue Apr 18, 2020 · 6 comments · Fixed by #1448

Comments

@NightMachinery
Copy link

Here is the code:

using Base.MathConstants
using Statistics, DataFrames, Gadfly, Distributions

function poisson(lan = 15, n = 200)
        p = Float64[e^-lan]
        pc = Float64[p[end]]
        sizehint!(p, n + 10)
        sizehint!(pc, n + 10)
        for i = 1:n-1
                push!(p, (lan / i) * p[end])
                push!(pc, pc[end] + p[end])
        end
        return p, pc
end

n = 200
lans = [5:5:155;]
@time a = [
        DataFrame(lambda = lan, i = [0:n-1;], cdf = poisson(lan, n)[2])
        for lan in lans
]

begin
        set_default_plot_size(30cm, 8cm)
        l1 = layer(vcat(a...), x = :i, y = :cdf, color = :lambda, Geom.line) # , Coord.cartesian(ymin=0, ymax=1)

        l2 = layer(y=[x -> cdf(Poisson(lan), x) for lan in lans], xmin=[0], xmax=[n - 1], Stat.func(num_samples=50), Geom.point)

        p = plot(l1,l2) #hstack(p1, p2)
        display(p)
end

And here is the error:

ERROR: LoadError: MethodError: no method matching -(::String, ::String)
Stacktrace:
 [1] optimize_ticks_typed(::String, ::String, ::Bool, ::Array{Tuple{Float64,Float64},1}, ::Int64, ::Int64, ::Int64, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/ticks.jl:73
 [2] #optimize_ticks#25(::Bool, ::Array{Tuple{Float64,Float64},1}, ::Int64, ::Int64, ::Int64, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::typeof(Gadfly.optimize_ticks), ::String, ::String) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/ticks.jl:61
 [3] (::Gadfly.var"#kw##optimize_ticks")(::NamedTuple{(:strict_span,),Tuple{Bool}}, ::typeof(Gadfly.optimize_ticks), ::String, ::String) at ./none:0
 [4] apply_scale(::Gadfly.Scale.ContinuousColorScale, ::Array{Gadfly.Aesthetics,1}, ::Gadfly.Data, ::Vararg{Gadfly.Data,N} where N) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/scale.jl:644
 [5] apply_statistic(::Gadfly.Stat.FunctionStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/statistics.jl:1429
 [6] apply_statistics(::Array{Gadfly.StatisticElement,1}, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/statistics.jl:33
 [7] render_prepare(::Plot) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/Gadfly.jl:697
 [8] render(::Plot) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/Gadfly.jl:755
 [9] draw at /Users/evar/.julia/packages/Gadfly/cqMtH/src/Gadfly.jl:864 [inlined]
 [10] display(::Gadfly.GadflyDisplay, ::MIME{Symbol("text/html")}, ::Plot) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/Gadfly.jl:1069
 [11] display(::Gadfly.GadflyDisplay, ::String, ::Any) at ./multimedia.jl:214
 [12] display(::Gadfly.GadflyDisplay, ::Plot) at /Users/evar/.julia/packages/Gadfly/cqMtH/src/Gadfly.jl:1026
 [13] display(::Any) at ./multimedia.jl:323
 [14] top-level scope at /Users/evar/Base/_Code/uni/stochastic/jo1/p1.jl:33
 [15] include at ./boot.jl:328 [inlined]
 [16] include_relative(::Module, ::String) at ./loading.jl:1105
 [17] include(::Module, ::String) at ./Base.jl:31
 [18] exec_options(::Base.JLOptions) at ./client.jl:287
 [19] _start() at ./client.jl:460
in expression starting at /Users/evar/Base/_Code/uni/stochastic/jo1/p1.jl:26
@Mattriks
Copy link
Member

The above plot will work by doing:

p = plot(l1, l2, Scale.color_discrete)

If you do plot(l1) and plot(l2) separately, you can see that plot(l1) is given a continuous color scale (:lambda is interpreted as numeric), but in plot(l2) Gadfly forces a discrete color scale. In plot(l1, l2) this causes a clash of color scales. We do plan to support plots with 1 continuous color scale, and 1 discrete color scale (Roadmap #1385).

For the above example, the question that needs investigation is: can a continuous color scale be allowed for plotting functions e.g. plot(l2, Scale.color_continuous)?

@Mattriks Mattriks mentioned this issue May 3, 2020
@Mattriks
Copy link
Member

plot(l1, l2) will also work if you add color=lans to layer l2. And by adding color=[numbers] to the layer, Scale.color_continuous will work. So is anything else needed here?

@NightMachinery
Copy link
Author

@Mattriks I solved my problem after posting the issue, but I think it violates the API expectation that layering plots should just work without additional workarounds. The error messages are unhelpful, as well.

@Mattriks
Copy link
Member

Mattriks commented Jun 1, 2020

What was your solution? I'm unclear what you mean by "the API expectation that layering plots should work without additional workarounds" e..g specifying color as numbers is not a workaround, there is no expectation that color in layer l1 should be the same as in l2? Just trying to clarify what you expect to happen in plot(l1, l2)

@NightMachinery
Copy link
Author

@Mattriks I don’t remember too well, but I think I just made the second layer’s data similar to the first (using a dataframe with similar columns and rows).

I think the naive expectation is that plot(l1,l2) should just overlay the two plots, without further settings. For example, in this case, the colors should automatically be switched to a setting that allows the overlay to happen, instead of producing an error.

Also, the solution that you are proposing is not at all hinted by the API; You know the problem is a clash of color scales, but this is because you know the internals of the system. A user who only knows the external API will not know the problem is a clash of colors. So what I am trying to say is, there needs to be either be an internal clash-breaker, or the clash and a possible avenue of trying to fix it need to be delivered to the end user as part if the error message. Of course, I am aware that this is easier said than done, so just close the issue if it’s hard to do.

@Mattriks
Copy link
Member

Mattriks commented Jun 1, 2020

I think a helpful error message would be nice, so I'll investigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants