diff --git a/src/backends/pgfplotsx.jl b/src/backends/pgfplotsx.jl index a523cc48..54f8b341 100644 --- a/src/backends/pgfplotsx.jl +++ b/src/backends/pgfplotsx.jl @@ -1,4 +1,5 @@ using Contour: Contour +using StatsBase: Histogram, fit # PGFPlotsX.print_tex(io::IO, data::ColorGradient) = write(io, pgfx_colormap(data)) Base.@kwdef mutable struct PGFPlotsXPlot is_created::Bool = false @@ -33,7 +34,6 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) ) ) end - pushed_colormap = false for sp in plt.subplots bb = bbox(sp) cstr = plot_color(sp[:background_color_legend]) @@ -65,36 +65,16 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) pgfx_axis!(axis_opt, sp, letter) end end - # Search series for any gradient. In case one series uses a gradient set - # the colorbar and colomap. - # The reasoning behind doing this on the axis level is that pgfplots - # colorbar seems to only works on axis level and needs the proper colormap for - # correctly displaying it. - # It's also possible to assign the colormap to the series itself but - # then the colormap needs to be added twice, once for the axis and once for the - # series. - # As it is likely that all series within the same axis use the same - # colormap this should not cause any problem. - for series in series_list(sp) - for col in (:markercolor, :fillcolor, :linecolor) - if typeof(series.plotattributes[col]) == ColorGradient - if !pushed_colormap - PGFPlotsX.push_preamble!(pgfx_plot.the_plot, """\\pgfplotsset{ - colormap={plots}{$(pgfx_colormap(series.plotattributes[col]))}, - }""") - pushed_colormap = true - push!(axis_opt, - "colorbar" => nothing, - "colormap name" => "plots", - ) - end - - # goto is needed to break out of col and series for - @goto colorbar_end - end - end + if hascolorbar(sp) + PGFPlotsX.push_preamble!(pgfx_plot.the_plot, """\\pgfplotsset{ + colormap={plots$(sp.attr[:subplot_index])}{$(pgfx_colormap(series.plotattributes[col]))}, + }""") + pushed_colormap = true + push!(axis_opt, + "colorbar" => nothing, + "colormap name" => "plots$(sp.attr[:subplot_index])", + ) end - @label colorbar_end push!(axis_opt, "colorbar style" => PGFPlotsX.Options( "title" => sp[:colorbar_title], @@ -181,6 +161,8 @@ function (pgfx_plot::PGFPlotsXPlot)(plt::Plot{PGFPlotsXBackend}) PGFPlotsX.Table(Contour.contours(args..., opt[:levels])) ) push!(axis, surface_plot) + elseif st == :histogram2d + hist_ else # treat segments segments = iter_segments(series) diff --git a/test/test_pgfplotsx.jl b/test/test_pgfplotsx.jl index e0a2eacd..3d96360d 100644 --- a/test/test_pgfplotsx.jl +++ b/test/test_pgfplotsx.jl @@ -90,4 +90,16 @@ end plot(p1, p2) # TODO: filled contours end # testset + @testset "Varying colors" begin + t = range(0, stop=1, length=100) + θ = (6π) .* t + x = t .* cos.(θ) + y = t .* sin.(θ) + p1 = plot(x, y, line_z=t, linewidth=3, legend=false) + p2 = scatter(x, y, marker_z=((x, y)->begin + x + y + end), color=:bluesreds, legend=false) + plot(p1, p2) + # TODO: handle gradients as color + end # testset end # testset