From a319c0c94adae6e8f6eabb75d88bee18add018bb Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Fri, 5 Feb 2016 15:10:12 -0500 Subject: [PATCH] legend and colorbar overhaul --- src/args.jl | 34 +++++++++++++++++++++++++++++++++- src/backends/gadfly.jl | 13 +++++++++---- src/backends/gadfly_shapes.jl | 2 +- src/backends/gr.jl | 2 +- src/backends/plotly.jl | 4 ++-- src/backends/pyplot.jl | 21 ++++++++++++++++----- src/backends/supported.jl | 2 ++ src/backends/unicodeplots.jl | 2 +- src/backends/winston.jl | 2 +- 9 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/args.jl b/src/args.jl index 78e3c59f..6fb2bfbb 100644 --- a/src/args.jl +++ b/src/args.jl @@ -150,7 +150,8 @@ _plotDefaults[:title] = "" _plotDefaults[:xlabel] = "" _plotDefaults[:ylabel] = "" _plotDefaults[:yrightlabel] = "" -_plotDefaults[:legend] = true +_plotDefaults[:legend] = :best +_plotDefaults[:colorbar] = :legend _plotDefaults[:background_color] = colorant"white" _plotDefaults[:foreground_color] = :auto _plotDefaults[:xlims] = :auto @@ -272,6 +273,9 @@ end :ylabel2 => :yrightlabel, :y2label => :yrightlabel, :leg => :legend, + :key => :legend, + :cbar => :colorbar, + :cb => :colorbar, :bg => :background_color, :bgcolor => :background_color, :bg_color => :background_color, @@ -532,6 +536,14 @@ function preprocessArgs!(d::Dict) # convert into strokes and brushes + # legends + if haskey(d, :legend) + d[:legend] = convertLegendValue(d[:legend]) + end + if haskey(d, :colorbar) + d[:colorbar] = convertLegendValue(d[:colorbar]) + end + # handle subplot links if haskey(d, :link) l = d[:link] @@ -639,6 +651,19 @@ function setDictValue(d_in::Dict, d_out::Dict, k::Symbol, idx::Int, defaults::Di end end +function convertLegendValue(val::Symbol) + if val in (:both, :all, :yes) + :best + elseif val in (:no, :none) + :none + elseif val in (:right, :left, :top, :bottom, :inside, :best, :legend) + val + else + error("Invalid symbol for legend: $val") + end +end +convertLegendValue(val::Bool) = val ? :best : :none + # ----------------------------------------------------------------------------- # build the argument dictionary for the plot @@ -659,6 +684,13 @@ function getPlotArgs(pkg::PlottingPackage, kw, idx::Int; set_defaults = true) end end + # handle legend/colorbar + d[:legend] = convertLegendValue(d[:legend]) + d[:colorbar] = convertLegendValue(d[:colorbar]) + if d[:colorbar] == :legend + d[:colorbar] = d[:legend] + end + # convert color handlePlotColors(pkg, d) diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index 4a72205e..620d20b1 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -201,6 +201,7 @@ function getGadflyMarkerTheme(d::Dict, plotargs::Dict) end function addGadflyContColorScale(plt::Plot{GadflyPackage}, c) + plt.plotargs[:colorbar] == :none && return if !isa(c, ColorGradient) c = colorscheme(:bluesreds) end @@ -233,7 +234,7 @@ end function addToGadflyLegend(plt::Plot, d::Dict) # add the legend? - if plt.plotargs[:legend] + if plt.plotargs[:legend] != :none gplt = getGadflyContext(plt) # add the legend if needed @@ -491,9 +492,13 @@ function updateGadflyPlotTheme(plt::Plot, d::Dict) # # TODO: should this be part of the main `plot` command in plot.jl??? # d = merge!(plt.plotargs, d) - # hide the legend? - if !get(d, :legend, true) - kwargs[:key_position] = :none + # # hide the legend? + # if !get(d, :legend, true) + # kwargs[:key_position] = :none + # end + leg = d[d[:legend] == :none ? :colorbar : :legend] + if leg != :best + kwargs[:key_position] = leg == :inside ? :right : leg end if !get(d, :grid, true) diff --git a/src/backends/gadfly_shapes.jl b/src/backends/gadfly_shapes.jl index 73ccd4ec..25672598 100644 --- a/src/backends/gadfly_shapes.jl +++ b/src/backends/gadfly_shapes.jl @@ -66,7 +66,7 @@ function Gadfly.render(geom::ShapeGeometry, theme::Gadfly.Theme, aes::Gadfly.Aes end function gadflyshape(sv::Shape) - ShapeGeometry([(x,-y) for (x,y) in sv.vertices]) + ShapeGeometry(sv.vertices) end diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 77244e05..8dcb5f65 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -435,7 +435,7 @@ function gr_display(plt::Plot{GRPackage}, clear=true, update=true, GR.restorestate() end - if d[:legend] && legend + if d[:legend] != :none && legend GR.savestate() GR.selntran(0) GR.setscale(0) diff --git a/src/backends/plotly.jl b/src/backends/plotly.jl index 9672638d..267ce197 100644 --- a/src/backends/plotly.jl +++ b/src/backends/plotly.jl @@ -231,8 +231,8 @@ function plotly_layout(d::Dict) d_out[:yaxis] = plotlyaxis(d, false) # legend - d_out[:showlegend] = d[:legend] - if d[:legend] + d_out[:showlegend] = d[:legend] != :none + if d[:legend] != :none d_out[:legend] = Dict{Symbol,Any}( :bgcolor => bgcolor, :bordercolor => fgcolor, diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index 80772df7..200701e3 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -326,7 +326,10 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...) if lt in (:scatter, :scatter3d) extra_kwargs[:s] = d[:markersize].^2 c = d[:markercolor] - if isa(c, ColorGradient) && d[:zcolor] != nothing + if d[:zcolor] != nothing + if !isa(c, ColorGradient) + c = colorscheme(:bluesreds) + end extra_kwargs[:c] = convert(Vector{Float64}, d[:zcolor]) extra_kwargs[:cmap] = getPyPlotColorMap(c, d[:markeralpha]) else @@ -405,8 +408,8 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...) handleSmooth(plt, ax, d, d[:smooth]) # add the colorbar legend - if plt.plotargs[:legend] && haskey(extra_kwargs, :cmap) - PyPlot.colorbar(d[:serieshandle]) + if plt.plotargs[:colorbar] != :none && haskey(extra_kwargs, :cmap) + PyPlot.colorbar(d[:serieshandle], ax=ax) end # @show extra_kwargs @@ -671,15 +674,23 @@ end # ----------------------------------------------------------------- +const _pyplot_legend_pos = Dict( + :right => "right", + :left => "center left", + :top => "upper center", + :bottom => "lower center" + ) + # function addPyPlotLegend(plt::Plot) function addPyPlotLegend(plt::Plot, ax) - if plt.plotargs[:legend] + leg = plt.plotargs[:legend] + if leg != :none # gotta do this to ensure both axes are included args = filter(x -> !(x[:linetype] in (:hist,:density,:hexbin,:heatmap,:hline,:vline,:contour, :surface, :wireframe, :path3d, :scatter3d)), plt.seriesargs) if length(args) > 0 leg = ax[:legend]([d[:serieshandle] for d in args], [d[:label] for d in args], - loc="best", + loc = get(_pyplot_legend_pos, leg, "best"), fontsize = plt.plotargs[:legendfont].pointsize # framealpha = 0.6 ) diff --git a/src/backends/supported.jl b/src/backends/supported.jl index f10d75cc..db18577a 100644 --- a/src/backends/supported.jl +++ b/src/backends/supported.jl @@ -33,6 +33,7 @@ supportedArgs(::GadflyPackage) = [ :label, :layout, :legend, + :colorbar, :linestyle, :linetype, :linewidth, @@ -114,6 +115,7 @@ supportedArgs(::PyPlotPackage) = [ :label, :layout, :legend, + :colorbar, :linestyle, :linetype, :linewidth, diff --git a/src/backends/unicodeplots.jl b/src/backends/unicodeplots.jl index 6c959241..b2da0c11 100644 --- a/src/backends/unicodeplots.jl +++ b/src/backends/unicodeplots.jl @@ -59,7 +59,7 @@ function rebuildUnicodePlot!(plt::Plot) # now use the ! functions to add to the plot for d in sargs - addUnicodeSeries!(o, d, iargs[:legend], xlim, ylim) + addUnicodeSeries!(o, d, iargs[:legend] != :none, xlim, ylim) end # save the object diff --git a/src/backends/winston.jl b/src/backends/winston.jl index 1e12f7ad..1bdae23b 100644 --- a/src/backends/winston.jl +++ b/src/backends/winston.jl @@ -205,7 +205,7 @@ end # ---------------------------------------------------------------- function addWinstonLegend(plt::Plot, wplt) - if plt.plotargs[:legend] + if plt.plotargs[:legend] != :none Winston.legend(wplt, [sd[:label] for sd in plt.seriesargs]) end end