basic contour

This commit is contained in:
Simon Christ 2019-11-21 14:37:39 +01:00
parent 48208998e2
commit 1e23b8d475
2 changed files with 46 additions and 29 deletions

View File

@ -1,4 +1,5 @@
# PGFPlotsX.print_tex(io::IO, data::Symbol) = PGFPlotsX.print_tex(io, string(data)) using Contour: Contour
# PGFPlotsX.print_tex(io::IO, data::ColorGradient) = write(io, pgfx_colormap(data))
Base.@kwdef mutable struct PGFPlotsXPlot Base.@kwdef mutable struct PGFPlotsXPlot
is_created::Bool = false is_created::Bool = false
was_shown::Bool = false was_shown::Bool = false
@ -132,7 +133,7 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
) )
# function args # function args
args = if st == :contour args = if st == :contour
opt[:z].surf, opt[:x], opt[:y] opt[:x], opt[:y], opt[:z].surf'
elseif is3d(st) elseif is3d(st)
opt[:x], opt[:y], opt[:z] opt[:x], opt[:y], opt[:z]
elseif st == :straightline elseif st == :straightline
@ -158,35 +159,44 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend})
if haskey(_pgfx_series_extrastyle, st) if haskey(_pgfx_series_extrastyle, st)
push!(series_opt, _pgfx_series_extrastyle[st] => nothing) push!(series_opt, _pgfx_series_extrastyle[st] => nothing)
end end
# treat segments if st == :contour
segments = iter_segments(series) surface_opt = PGFPlotsX.Options(
segment_opt = PGFPlotsX.Options() "contour prepared" => nothing
for (i, rng) in enumerate(segments)
seg_args = (arg[rng] for arg in args)
segment_opt = merge( segment_opt, pgfx_linestyle(opt, i) )
segment_opt = merge( segment_opt, pgfx_marker(opt, i) )
if st == :shape || series[:fillrange] !== nothing
segment_opt = merge( segment_opt, pgfx_fillstyle(opt, i) )
end
segment_plot = series_func(
merge(series_opt, segment_opt),
PGFPlotsX.Coordinates(seg_args...),
series[:fillrange] !== nothing ? "\\closedcycle" : "{}"
) )
push!(axis, segment_plot) surface_plot = series_func(
# add to legend? # merge(series_opt, surface_opt),
if i == 1 && opt[:label] != "" && sp[:legend] != :none && should_add_to_legend(series) surface_opt,
push!( axis, PGFPlotsX.LegendEntry( opt[:label] ) PGFPlotsX.Table(Contour.contours(args...))
)
push!(axis, surface_plot)
else
# treat segments
segments = iter_segments(series)
segment_opt = PGFPlotsX.Options()
for (i, rng) in enumerate(segments)
seg_args = (arg[rng] for arg in args)
segment_opt = merge( segment_opt, pgfx_linestyle(opt, i) )
segment_opt = merge( segment_opt, pgfx_marker(opt, i) )
if st == :shape || series[:fillrange] !== nothing
segment_opt = merge( segment_opt, pgfx_fillstyle(opt, i) )
end
segment_plot = series_func(
merge(series_opt, segment_opt),
PGFPlotsX.Coordinates(seg_args...),
series[:fillrange] !== nothing ? "\\closedcycle" : "{}"
) )
push!(axis, segment_plot)
# add to legend?
if i == 1 && opt[:label] != "" && sp[:legend] != :none && should_add_to_legend(series)
push!( axis, PGFPlotsX.LegendEntry( opt[:label] )
)
end
end
# add series annotations
anns = series[:series_annotations]
for (xi,yi,str,fnt) in EachAnn(anns, series[:x], series[:y])
pgfx_add_annotation!(axis, xi, yi, PlotText(str, fnt), pgfx_thickness_scaling(series))
end end
end
# TODO: different seriestypes, histogramms, contours, etc.
# TODO: colorbars
# TODO: gradients
# add series annotations
anns = series[:series_annotations]
for (xi,yi,str,fnt) in EachAnn(anns, series[:x], series[:y])
pgfx_add_annotation!(series_plot, xi, yi, PlotText(str, fnt), pgfx_thickness_scaling(series))
end end
end end
if ispolar(sp) if ispolar(sp)

View File

@ -65,6 +65,13 @@ end
r = abs.(0.1 * randn(100) + sin.(3Θ)) r = abs.(0.1 * randn(100) + sin.(3Θ))
plot(Θ, r, proj=:polar, m=2) plot(Θ, r, proj=:polar, m=2)
end # testset end # testset
@testset "Drawing shapes" begin
verts = [(-1.0, 1.0), (-1.28, 0.6), (-0.2, -1.4), (0.2, -1.4), (1.28, 0.6), (1.0, 1.0), (-1.0, 1.0), (-0.2, -0.6), (0.0, -0.2), (-0.4, 0.6), (1.28, 0.6), (0.2, -1.4), (-0.2, -1.4), (0.6, 0.2), (-0.2, 0.2), (0.0, -0.2), (0.2, 0.2), (-0.2, -0.6)]
x = 0.1:0.2:0.9
y = 0.7 * rand(5) .+ 0.15
plot(x, y, line=(3, :dash, :lightblue), marker=(Shape(verts), 30, RGBA(0, 0, 0, 0.2)), bg=:pink, fg=:darkblue, xlim=(0, 1), ylim=(0, 1), leg=false)
# TODO: draw those polygons
end # testset
@testset "Histogram 2D" begin @testset "Histogram 2D" begin
histogram2d(randn(10000), randn(10000), nbins=20) histogram2d(randn(10000), randn(10000), nbins=20)
# TODO: totally broken, errors also for pgfplots # TODO: totally broken, errors also for pgfplots
@ -78,8 +85,8 @@ end
X = repeat(reshape(x, 1, :), length(y), 1) X = repeat(reshape(x, 1, :), length(y), 1)
Y = repeat(y, 1, length(x)) Y = repeat(y, 1, length(x))
Z = map(f, X, Y) Z = map(f, X, Y)
p1 = contour(x, y, f, fill=true)
p2 = contour(x, y, Z) p2 = contour(x, y, Z)
p1 = contour(x, y, f, fill=true)
plot(p1, p2) plot(p1, p2)
# TODO: totally broken, also errors for pgfplots # TODO: totally broken, also errors for pgfplots
end # testset end # testset