legend and colorbar overhaul

This commit is contained in:
Thomas Breloff 2016-02-05 15:10:12 -05:00
parent f74d619748
commit a319c0c94a
9 changed files with 66 additions and 16 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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,

View File

@ -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
)

View File

@ -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,

View File

@ -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

View File

@ -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