From e3a166f04cc22d8a7a1d5338f5beda9d1590fd31 Mon Sep 17 00:00:00 2001 From: Simon Christ Date: Thu, 21 Nov 2019 14:37:39 +0100 Subject: [PATCH] basic contour --- src/backends/pgfplotsx.jl | 66 ++++++++++++++++++++++----------------- test/test_pgfplotsx.jl | 9 +++++- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index 07da6b69..bdb7b7f9 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -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 is_created::Bool = false was_shown::Bool = false @@ -132,7 +133,7 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) ) # function args args = if st == :contour - opt[:z].surf, opt[:x], opt[:y] + opt[:x], opt[:y], opt[:z].surf' elseif is3d(st) opt[:x], opt[:y], opt[:z] elseif st == :straightline @@ -158,35 +159,44 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) if haskey(_pgfx_series_extrastyle, st) push!(series_opt, _pgfx_series_extrastyle[st] => nothing) end - # 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" : "{}" + if st == :contour + surface_opt = PGFPlotsX.Options( + "contour prepared" => nothing ) - 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] ) + surface_plot = series_func( + # merge(series_opt, surface_opt), + surface_opt, + 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 - # 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 if ispolar(sp) diff --git a/test/test_pgfplotsx.jl b/test/test_pgfplotsx.jl index 68002222..de775c32 100644 --- a/test/test_pgfplotsx.jl +++ b/test/test_pgfplotsx.jl @@ -65,6 +65,13 @@ end r = abs.(0.1 * randn(100) + sin.(3Θ)) plot(Θ, r, proj=:polar, m=2) 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 histogram2d(randn(10000), randn(10000), nbins=20) # TODO: totally broken, errors also for pgfplots @@ -78,8 +85,8 @@ end X = repeat(reshape(x, 1, :), length(y), 1) Y = repeat(y, 1, length(x)) Z = map(f, X, Y) - p1 = contour(x, y, f, fill=true) p2 = contour(x, y, Z) + p1 = contour(x, y, f, fill=true) plot(p1, p2) # TODO: totally broken, also errors for pgfplots end # testset