Added smooth keyword to palette; Updated docstrings
This commit is contained in:
parent
4d1768e15e
commit
92380bc468
@ -1673,17 +1673,19 @@ end
|
|||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
palette(cmap::ColorScheme; rev=false)
|
palette(cmap::ColorScheme; rev=false, smooth=false)
|
||||||
palette(s::Symbol; rev=false)
|
palette(s::Symbol; rev=false, smooth=false)
|
||||||
|
|
||||||
Convert a `ColorScheme` object into a string containing the gnuplot commands to set up the corresponding palette.
|
Convert a `ColorScheme` object into a string containing the gnuplot commands to set up the corresponding palette.
|
||||||
|
|
||||||
If the argument is a `Symbol` it is interpreted as the name of one of the predefined schemes in [ColorSchemes](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1). If `rev=true` the palette is reversed.
|
If the argument is a `Symbol` it is interpreted as the name of one of the predefined schemes in [ColorSchemes](https://juliagraphics.github.io/ColorSchemes.jl/stable/basics/#Pre-defined-schemes-1).
|
||||||
|
|
||||||
|
If `rev=true` the palette is reversed. If `smooth=true` the palette is interpolated in 256 levels.
|
||||||
"""
|
"""
|
||||||
palette(s::Symbol; rev=false) = palette(colorschemes[s], rev=rev)
|
palette(s::Symbol; kwargs...) = palette(colorschemes[s]; kwargs...)
|
||||||
function palette(cmap::ColorScheme; rev=false)
|
function palette(cmap::ColorScheme; rev=false, smooth=false)
|
||||||
levels = Vector{String}()
|
levels = Vector{String}()
|
||||||
for x in LinRange(0, 1, length(cmap.colors))
|
for x in LinRange(0, 1, (smooth ? 256 : length(cmap.colors)))
|
||||||
if rev
|
if rev
|
||||||
color = get(cmap, 1-x)
|
color = get(cmap, 1-x)
|
||||||
else
|
else
|
||||||
@ -1960,23 +1962,25 @@ struct IsoContourLines
|
|||||||
paths::Vector{Path2d}
|
paths::Vector{Path2d}
|
||||||
data::Dataset
|
data::Dataset
|
||||||
z::Float64
|
z::Float64
|
||||||
function IsoContourLines(paths::Vector{Path2d}, z)
|
prob::Float64
|
||||||
@assert length(z) == 1
|
end
|
||||||
# Prepare Dataset object
|
function IsoContourLines(paths::Vector{Path2d}, z)
|
||||||
data = Vector{String}()
|
@assert length(z) == 1
|
||||||
for i in 1:length(paths)
|
# Prepare Dataset object
|
||||||
append!(data, arrays2datablock(paths[i].x, paths[i].y, z .* fill(1., length(paths[i].x))))
|
data = Vector{String}()
|
||||||
push!(data, "")
|
for i in 1:length(paths)
|
||||||
push!(data, "")
|
append!(data, arrays2datablock(paths[i].x, paths[i].y, z .* fill(1., length(paths[i].x))))
|
||||||
end
|
push!(data, "")
|
||||||
return new(paths, DatasetText(data), z)
|
push!(data, "")
|
||||||
end
|
end
|
||||||
|
return IsoContourLines(paths, DatasetText(data), z, NaN)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
contourlines(x::AbstractVector{Float64}, y::AbstractVector{Float64}, z::AbstractMatrix{Float64}, cntrparam="level auto 10")
|
contourlines(x, y, z, cntrparam="level auto 4")
|
||||||
contourlines(h::Histogram2D, cntrparam="level auto 10")
|
contourlines(x, y, z, fractions)
|
||||||
|
contourlines(h::Histogram2D, ...)
|
||||||
|
|
||||||
Compute paths of contour lines for 2D data, and return a vector of [`IsoContourLines`](@ref) object.
|
Compute paths of contour lines for 2D data, and return a vector of [`IsoContourLines`](@ref) object.
|
||||||
|
|
||||||
@ -1984,16 +1988,19 @@ Compute paths of contour lines for 2D data, and return a vector of [`IsoContourL
|
|||||||
This feature is not available in *dry* mode and will raise an error if used.
|
This feature is not available in *dry* mode and will raise an error if used.
|
||||||
|
|
||||||
# Arguments:
|
# Arguments:
|
||||||
- `x`, `y`: Coordinates;
|
- `x`, `y` (as `AbstractVector{Float64}`): Coordinates;
|
||||||
- `z`: the levels on which iso contour lines are to be calculated
|
- `z::AbstractMatrix{Float64}`: the levels on which iso-contour lines are to be calculated;
|
||||||
- `cntrparam`: settings to compute contour line paths (see gnuplot documentation for `cntrparam`).
|
- `cntrparam::String`: settings to compute contour line paths (see gnuplot documentation for `cntrparam`);
|
||||||
|
- `fractions::Vector{Float64}`: compute contour lines encompassing these fractions of total counts;
|
||||||
|
- `h::Histogram2D`: use histogram bins and counts to compute contour lines.
|
||||||
|
|
||||||
|
|
||||||
# Example
|
# Example
|
||||||
```julia
|
```julia
|
||||||
x = randn(5000);
|
x = randn(10^5);
|
||||||
y = randn(5000);
|
y = randn(10^5);
|
||||||
h = hist(x, y, nbins1=20, nbins2=20);
|
h = hist(x, y, nbins1=20, nbins2=20);
|
||||||
clines = contourlines(h, "levels discrete 15, 30, 45");
|
clines = contourlines(h, "levels discrete 500, 1500, 2500");
|
||||||
|
|
||||||
# Use implicit recipe
|
# Use implicit recipe
|
||||||
@gp clines
|
@gp clines
|
||||||
@ -2003,6 +2010,13 @@ clines = contourlines(h, "levels discrete 15, 30, 45");
|
|||||||
for i in 1:length(clines)
|
for i in 1:length(clines)
|
||||||
@gp :- clines[i].data "w l t '\$(clines[i].z)' lw \$i dt \$i"
|
@gp :- clines[i].data "w l t '\$(clines[i].z)' lw \$i dt \$i"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Calculate probability within 0 < r < σ
|
||||||
|
p(σ) = round(1 - exp(-(σ^2) / 2), sigdigits=3)
|
||||||
|
|
||||||
|
# Draw contour lines at 1, 2 and 3 σ
|
||||||
|
clines = contourlines(h, p.(1:3));
|
||||||
|
@gp palette(:beach, smooth=true, rev=true) "set grid front" "set size ratio -1" h clines
|
||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
contourlines(h::Histogram2D, args...) = contourlines(h.bins1, h.bins2, h.counts, args...)
|
contourlines(h::Histogram2D, args...) = contourlines(h.bins1, h.bins2, h.counts, args...)
|
||||||
@ -2011,24 +2025,28 @@ function contourlines(x::AbstractVector{Float64}, y::AbstractVector{Float64}, z:
|
|||||||
@assert minimum(fraction) > 0
|
@assert minimum(fraction) > 0
|
||||||
@assert maximum(fraction) < 1
|
@assert maximum(fraction) < 1
|
||||||
@assert length(fraction) >= 1
|
@assert length(fraction) >= 1
|
||||||
|
sorted_fraction = sort(fraction, rev=true)
|
||||||
# The following is necessary since `countourlines` return levels
|
|
||||||
# sorted in increasing order, corresponding to decreasing order
|
|
||||||
# top fractions.
|
|
||||||
@assert issorted(fraction, rev=true) "`fraction` must be sorted in decreasing order"
|
|
||||||
|
|
||||||
i = sortperm(z[:], rev=true)
|
i = sortperm(z[:], rev=true)
|
||||||
topfrac = cumsum(z[i]) ./ sum(z)
|
topfrac = cumsum(z[i]) ./ sum(z)
|
||||||
selection = Int[]
|
selection = Int[]
|
||||||
for f in fraction
|
for f in sorted_fraction
|
||||||
push!(selection, minimum(findall(topfrac .>= f)))
|
push!(selection, minimum(findall(topfrac .>= f)))
|
||||||
end
|
end
|
||||||
levels = z[i[selection]]
|
levels = z[i[selection]]
|
||||||
clines = contourlines(x, y, z, "levels discrete " * join(string.(levels), ", "))
|
clines = contourlines(x, y, z, "levels discrete " * join(string.(levels), ", "))
|
||||||
|
@assert issorted(getfield.(clines, :z))
|
||||||
|
|
||||||
|
if length(clines) == length(fraction)
|
||||||
|
out = [IsoContourLines(clines[i].paths, clines[i].data, clines[i].z,
|
||||||
|
sorted_fraction[i]) for i in 1:length(clines)]
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
return clines
|
||||||
end
|
end
|
||||||
|
|
||||||
function contourlines(x::AbstractVector{Float64}, y::AbstractVector{Float64}, z::AbstractMatrix{Float64},
|
function contourlines(x::AbstractVector{Float64}, y::AbstractVector{Float64}, z::AbstractMatrix{Float64},
|
||||||
cntrparam="level auto 10")
|
cntrparam="level auto 4")
|
||||||
lines = gp_write_table("set contour base", "unset surface",
|
lines = gp_write_table("set contour base", "unset surface",
|
||||||
"set cntrparam $cntrparam", x, y, z, is3d=true)
|
"set cntrparam $cntrparam", x, y, z, is3d=true)
|
||||||
level = NaN
|
level = NaN
|
||||||
|
|||||||
@ -29,7 +29,12 @@ recipe(h::Histogram2D) =
|
|||||||
|
|
||||||
Implicit recipes to visualize iso-contour lines.
|
Implicit recipes to visualize iso-contour lines.
|
||||||
"""
|
"""
|
||||||
recipe(c::IsoContourLines) = PlotElement(data=c.data, plot="w l t '$(c.z)'")
|
function recipe(c::IsoContourLines)
|
||||||
|
if isnan(c.prob)
|
||||||
|
return PlotElement(data=c.data, plot="w l t '$(c.z)'")
|
||||||
|
end
|
||||||
|
return PlotElement(data=c.data, plot="w l t '$(round(c.prob * 100, sigdigits=6))%'")
|
||||||
|
end
|
||||||
recipe(v::Vector{IsoContourLines}) = recipe.(v)
|
recipe(v::Vector{IsoContourLines}) = recipe.(v)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user