Как наложить участки в Julia с прозрачностью (используя Plots) - PullRequest
0 голосов
/ 08 сентября 2018

Я решаю проблему, описанную здесь . Требуется наложение одного сюжета на другой. Этот ответ описывает способ наложения графиков с использованием пакета Plots, но ни один из параметров с alpha в названии не добавляет прозрачности графику (код ниже).

Как добавить прозрачность к закрашенному контурному графику в julia v1.0.0 с помощью пакета Plots?

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 голосов
/ 08 сентября 2018

Оказывается, что seriesalpha была нужной мне настройкой. Однако он не работает при использовании gr бэкэнда.

...