0

I'm solving a problem described here. It requires overlaying one plot with the other. This answer describes the way to overlay plots using Plots package but none of the options with alpha in their name adds transparency to the plot (code below).

How can I add transparency to the filled contour plot in julia v1.0.0 using Plots package?

import Plots

struct Sectors{T}
    values::Vector{T}
    n::Int
    Sectors(values::Vector{T}) where T = new{T}(values, length(values))
end

mutable struct Radius{T}
    value::T
    radius::Union{T, Nothing}
    next::Union{Radius{T}, Nothing}
    sectors::Union{Sectors{T}, Nothing}
    function Radius(radius::T,
                    value::T;
                    next::Union{Radius{T}, Nothing} = nothing,
                    sectors::Union{Sectors{T}, Nothing}=nothing) where T
        new{T}(value, radius, nothing, nothing)
    end
end

function create(radiuses::Vector{T},
                radius_vals::Vector{T},
                sector_vals::Vector{Union{Nothing, Vector{T}}})::Radius{T} where T
    r = Radius(radiuses[1], radius_vals[1])
    if sector_vals[1] !== nothing
        r.sectors = Sectors(sector_vals[1])
    end
    k = r
    for i in 2:length(radiuses)
        n = Radius(radiuses[i], radius_vals[i])
        k.next = n
        k = n
        if sector_vals[i] !== nothing
            n.sectors = Sectors(sector_vals[i])
        end
    end
    r
end

function filter(r::T, α::T, rads::Radius{T})::T where T<:Number
    while true
        if (abs(r) <= rads.radius)
            if (rads.sectors !== nothing)
                return rads.sectors.values[convert(Int,
                                                   fld(α + π/2 + π/rads.sectors.n/2,
                                                       π/(rads.sectors.n-1)) + 1)]
            else
                return rads.value
            end
        else
            if (rads.next !== nothing)
                rads = rads.next
            else
                return 0
            end
        end
    end
end

function construct_board(xy::AbstractMatrix{T})::AbstractMatrix{T} where T<:Number
    rads = create(T[14.2, 31.8, 184.1 , 203.2, 304.8, 323.8, 425.5],
                  T[1, 0, 1, 0, 1, 0, 1],
                  [nothing,
                   nothing,
                   T[0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3],
                   T[0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7],
                   T[0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3],
                   T[0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7, 0.3, 0.7],
                   nothing])
    target = zeros(size(xy, 1), size(xy, 1))
    for i in eachindex(xy[:, 1])
        for j in eachindex(xy[:, 2])
            x = xy[i, 1]
            y = xy[j, 2]
            α = atan(y/x)
            r = sqrt(x^2 + y^2)
            target[i, j] = filter(r, α, rads)
        end
    end
    target
end

function filter2(x::T, y::T, σx::T, σy::T)::T where T<:Number
    return 1/(2π*σx*σy)*exp(-x^2/2σx^2-y^2/2σy^2)
end

function construct_hits(xy::AbstractMatrix{T})::AbstractMatrix{T} where T<:Number
    target = zeros(size(xy, 1), size(xy, 1))
    for i in eachindex(xy[:, 1])
        for j in eachindex(xy[:, 2])
            x = xy[i, 1]
            y = xy[j, 2]
            σx = 54.
            σy = 90.
            target[i, j] = filter2(x, y, σx, σy)
        end
    end
    m, i = findmax(target)
    target ./ m
end


function main()
    Plots.plotly() #  same results with gr backend
    a = construct_board(cat(-500.:1:500, -500.:1:500, dims=2))
    plot_a = Plots.contourf(a)
    b = construct_hits(cat(-500.:1:500, -500.:1:500, dims=2))
    plot_b = Plots.contourf!(b,
                             seriesalpha = 0.1 )
    p = Plots.plot(plot_a, plot_b)
    display(p)
end

main()
Sergey
  • 1,166
  • 14
  • 27

1 Answers1

0

Turns out, that seriesalpha was the setting I needed. However it does not work when gr backend is used.

Sergey
  • 1,166
  • 14
  • 27