From 7c91d10c79782a7c145f473439fcd8d6fb63ad0e Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 16 Oct 2015 00:21:37 -0400 Subject: [PATCH] several fixes after gadfly reorg --- docs/example_generation.jl | 4 +- src/Plots.jl | 2 + src/backends/gadfly.jl | 76 +++++++++++++++++++++++++------------- src/backends/immerse.jl | 2 +- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 4daf719a..f8302912 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -99,8 +99,8 @@ const examples = PlotExample[ "Currently only text annotations are supported. Pass in a tuple or vector-of-tuples: (x,y,text). `annotate!(ann)` is shorthand for `plot!(; annotation=ann)`", [ :(y = rand(10)), - :(plot(y, ann=(3,y[3],"this is #3"))), - :(annotate!([(5,y[5],"this is #5"),(9,y[10],"this is #10")])) + :(plot(y, ann=(3,y[3],text("this is #3",:left)))), + :(annotate!([(5,y[5],text("this is #5",16,:red,:center)),(10,y[10],text("this is #10",:right,20,:Helvitica))])) ]), ] diff --git a/src/Plots.jl b/src/Plots.jl index 2427745b..680c63e9 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -141,6 +141,7 @@ xticks!{T<:Real,S<:@compat(AbstractString)}( yticks!{T<:Real,S<:@compat(AbstractString)}( ticks::AVec{T}, labels::AVec{S}; kw...) = plot!(; yticks = (ticks,labels), kw...) annotate!(anns...; kw...) = plot!(; annotation = anns, kw...) +annotate!{T<:Tuple}(anns::AVec{T}; kw...) = plot!(; annotation = anns, kw...) xflip!(flip::Bool = true; kw...) = plot!(; xflip = flip, kw...) yflip!(flip::Bool = true; kw...) = plot!(; yflip = flip, kw...) xaxis!(args...; kw...) = plot!(; xaxis = args, kw...) @@ -160,6 +161,7 @@ xticks!{T<:Real,S<:@compat(AbstractString)}(plt::Plot, yticks!{T<:Real,S<:@compat(AbstractString)}(plt::Plot, ticks::AVec{T}, labels::AVec{S}; kw...) = plot!(plt; yticks = (ticks,labels), kw...) annotate!(plt::Plot, anns...; kw...) = plot!(plt; annotation = anns, kw...) +annotate!{T<:Tuple}(plt::Plot, anns::AVec{T}; kw...) = plot!(plt; annotation = anns, kw...) xflip!(plt::Plot, flip::Bool = true; kw...) = plot!(plt; xflip = flip, kw...) yflip!(plt::Plot, flip::Bool = true; kw...) = plot!(plt; yflip = flip, kw...) xaxis!(plt::Plot, args...; kw...) = plot!(plt; xaxis = args, kw...) diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index 07051f68..d2426d14 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -83,10 +83,6 @@ function createGadflyPlotObject(d::Dict) Gadfly.Guide.ylabel(d[:ylabel]), Gadfly.Guide.title(d[:title])] - # add the legend? - if d[:legend] - unshift!(gplt.guides, Gadfly.Guide.manual_color_key("", @compat(AbstractString)[], Color[])) - end # hide the legend if get(d, :legend, true) @@ -98,7 +94,7 @@ function createGadflyPlotObject(d::Dict) fg = getColor(d[:foreground_color]) gplt.theme = Gadfly.Theme(; background_color = getColor(d[:background_color]), - grid_color = fg, + # grid_color = fg, minor_label_color = fg, major_label_color = fg, key_title_color = fg, @@ -199,7 +195,6 @@ function getLineGeom(d::Dict) elseif lt == :steppost Gadfly.Geom.step elseif lt == :steppre - # direction: Either :hv for horizontal then vertical, or :vh for Gadfly.Geom.step(direction = :vh) elseif lt == :hline Gadfly.Geom.hline(color = getColor(d[:color]), size = d[:linewidth] * Gadfly.px) @@ -215,7 +210,7 @@ function getGadflyLineTheme(d::Dict) fc = getColor(d[:fillcolor]) Gadfly.Theme(; default_color = lc, - line_width = d[:linewidth] * Gadfly.px, + line_width = (d[:linetype] == :sticks ? 1 : d[:linewidth]) * Gadfly.px, line_style = Gadfly.get_stroke_vector(d[:linestyle]), lowlight_color = x->RGB(fc), # fill/ribbon lowlight_opacity = alpha(fc), # fill/ribbon @@ -224,7 +219,8 @@ function getGadflyLineTheme(d::Dict) end # add a line as a new layer -function addGadflyLine!(gplt, d::Dict, geoms...) +function addGadflyLine!(plt::Plot, d::Dict, geoms...) + gplt = getGadflyContext(plt) gfargs = vcat(geoms..., getGadflyLineTheme(d)) kwargs = Dict() @@ -245,7 +241,7 @@ function addGadflyLine!(gplt, d::Dict, geoms...) elseif lt == :vline kwargs[:xintercept] = d[:y] elseif lt == :sticks - w = 0.1 * mean(diff(d[:x])) + w = 0.01 * mean(diff(d[:x])) kwargs[:xmin] = d[:x] - w kwargs[:xmax] = d[:x] + w end @@ -274,7 +270,8 @@ function getGadflyMarkerTheme(d::Dict) ) end -function addGadflyMarker!(gplt, d::Dict, geoms...) +function addGadflyMarker!(plt::Plot, d::Dict, geoms...) + gplt = getGadflyContext(plt) gfargs = vcat(geoms..., getGadflyMarkerTheme(d), getMarkerGeom(d)) @@ -296,9 +293,49 @@ end # --------------------------------------------------------------------------- +function addToGadflyLegend(plt::Plot, d::Dict) + + # add the legend? + if plt.initargs[:legend] + gplt = getGadflyContext(plt) + + # add the legend if needed + if all(g -> !isa(g, Gadfly.Guide.ManualColorKey), gplt.guides) + unshift!(gplt.guides, Gadfly.Guide.manual_color_key("", @compat(AbstractString)[], Color[])) + end + + # now add the series to the legend + for guide in gplt.guides + if isa(guide, Gadfly.Guide.ManualColorKey) + # TODO: there's a BUG in gadfly if you pass in the same color more than once, + # since gadfly will call unique(colors), but doesn't also merge the rows that match + # Should ensure from this side that colors which are the same are merged together + + c = getColor(d[d[:markershape] == :none ? :color : :markercolor]) + foundit = false + + # extend the label if we found this color + for i in 1:length(guide.colors) + if c == guide.colors[i] + guide.labels[i] *= ", " * d[:label] + foundit = true + end + end + + # didn't find the color, so add a new entry into the legend + if !foundit + push!(guide.labels, d[:label]) + push!(guide.colors, c) + end + end + end + + end + +end -function addGadflySeries!(gplt, d::Dict) +function addGadflySeries!(plt::Plot, d::Dict) # add a regression line? # TODO: make more flexible @@ -307,7 +344,7 @@ function addGadflySeries!(gplt, d::Dict) # lines geom = getLineGeom(d) if geom != nothing - addGadflyLine!(gplt, d, geom, smooth...) + addGadflyLine!(plt, d, geom, smooth...) # don't add a regression for markers too smooth = Any[] @@ -323,21 +360,10 @@ function addGadflySeries!(gplt, d::Dict) # markers if d[:markershape] != :none - addGadflyMarker!(gplt, d, smooth...) - end - - # add to the legend - for guide in gplt.guides - if isa(guide, Gadfly.Guide.ManualColorKey) - # TODO: there's a BUG in gadfly if you pass in the same color more than once, - # since gadfly will call unique(colors), but doesn't also merge the rows that match - # Should ensure from this side that colors which are the same are merged together - - push!(guide.labels, d[:label]) - push!(guide.colors, getColor(d[d[:markershape] == :none ? :color : :markercolor])) - end + addGadflyMarker!(plt, d, smooth...) end + lt in (:hist, :heatmap, :hexbin) || addToGadflyLegend(plt, d) end diff --git a/src/backends/immerse.jl b/src/backends/immerse.jl index 464171ff..4615323a 100644 --- a/src/backends/immerse.jl +++ b/src/backends/immerse.jl @@ -37,7 +37,7 @@ end # plot one data series function plot!(::ImmersePackage, plt::Plot; kw...) d = Dict(kw) - addGadflySeries!(getGadflyContext(plt), d) + addGadflySeries!(plt, d) push!(plt.seriesargs, d) plt end