如何在Julia中以透明方式覆盖图(使用图)

时间:2018-09-08 10:45:57

标签: plot julia

我正在解决here中描述的问题。它需要将一个图与另一个图重叠。 This answer描述了使用Plots程序包覆盖图的方法,但是options中名称中没有带有alpha的{​​{3}}都不会增加图的透明度(下面的代码)。

如何使用Plots软件包为julia v1.0.0中的填充轮廓图添加透明度?

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

1 个答案:

答案 0 :(得分:0)

原来,seriesalpha是我需要的设置。但是,当使用gr后端时,它将不起作用。