From 1cb0c0071bcfa1e041efff58bdc4e93c8761c30b Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Sat, 9 Jul 2016 12:40:27 -0400 Subject: [PATCH] improvements to layout padding; handle axis attr better; fix histogram2d --- src/backends.jl | 21 +++++++++++++++++---- src/backends/pyplot.jl | 14 ++++++++++---- src/plot.jl | 13 ++++++++----- src/recipes.jl | 2 +- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/backends.jl b/src/backends.jl index 8cdfe0cb..aa8a7c9b 100644 --- a/src/backends.jl +++ b/src/backends.jl @@ -57,13 +57,26 @@ _before_layout_calcs(plt::Plot) = nothing title_padding(sp::Subplot) = sp[:title] == "" ? 0mm : sp[:titlefont].pointsize * pt guide_padding(axis::Axis) = axis[:guide] == "" ? 0mm : axis[:guidefont].pointsize * pt +# TODO: this should account for both tick font and the size/length/rotation of tick labels +function tick_padding(axis::Axis) + ptsz = axis[:tickfont].pointsize * pt + if axis[:ticks] in (nothing,false) + 0mm + elseif axis[:letter] == :x + 2mm + ptsz + else + 8mm + end +end + # Set the (left, top, right, bottom) minimum padding around the plot area # to fit ticks, tick labels, guides, colorbars, etc. function _update_min_padding!(sp::Subplot) - leftpad = 10mm + sp[:left_margin] + guide_padding(sp[:yaxis]) - toppad = 2mm + sp[:top_margin] + title_padding(sp) - rightpad = 3mm + sp[:right_margin] - bottompad = 5mm + sp[:bottom_margin] + guide_padding(sp[:xaxis]) + # TODO: something different when `is3d(sp) == true` + leftpad = tick_padding(sp[:yaxis]) + sp[:left_margin] + guide_padding(sp[:yaxis]) + toppad = sp[:top_margin] + title_padding(sp) + rightpad = sp[:right_margin] + bottompad = tick_padding(sp[:xaxis]) + sp[:bottom_margin] + guide_padding(sp[:xaxis]) # @show (leftpad, toppad, rightpad, bottompad) sp.minpad = (leftpad, toppad, rightpad, bottompad) end diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index 156c4431..bd086a39 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -1015,10 +1015,10 @@ function _update_min_padding!(sp::Subplot{PyPlotBackend}) # TODO: this should initialize to the margin from sp.attr # figure out how much the axis components and title "stick out" from the plot area # leftpad = toppad = rightpad = bottompad = 1mm - leftpad = sp[:left_margin] - toppad = sp[:top_margin] - rightpad = sp[:right_margin] - bottompad = sp[:bottom_margin] + leftpad = 0mm + toppad = 0mm + rightpad = 0mm + bottompad = 0mm for bb in (py_bbox_axis(ax, "x"), py_bbox_axis(ax, "y"), py_bbox_title(ax)) if ispositive(width(bb)) && ispositive(height(bb)) leftpad = max(leftpad, left(plotbb) - left(bb)) @@ -1035,6 +1035,12 @@ function _update_min_padding!(sp::Subplot{PyPlotBackend}) rightpad = rightpad + sp.attr[:cbar_width] end + # add in the user-specified margin + leftpad += sp[:left_margin] + toppad += sp[:top_margin] + rightpad += sp[:right_margin] + bottompad += sp[:bottom_margin] + sp.minpad = (leftpad, toppad, rightpad, bottompad) end diff --git a/src/plot.jl b/src/plot.jl index fcd4993f..99381d9a 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -490,13 +490,16 @@ function _plot!(plt::Plot, d::KW, args...) sp = get_subplot(plt, cycle(sps == :auto ? plt.subplots : plt.subplots[sps], command_idx(kw_list,kw))) kw[:subplot] = sp + # extract subplot/axis attributes from kw and add to sp_attr attr = KW() for (k,v) in kw - for defdict in (_subplot_defaults, - _axis_defaults, - _axis_defaults_byletter) - if haskey(defdict, k) - attr[k] = pop!(kw, k) + if haskey(_subplot_defaults, k) || haskey(_axis_defaults_byletter, k) + attr[k] = pop!(kw, k) + end + if haskey(_axis_defaults, k) + v = pop!(kw, k) + for letter in (:x,:y,:z) + attr[Symbol(letter,k)] = v end end end diff --git a/src/recipes.jl b/src/recipes.jl index 38f3b0c9..729f1e2f 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -522,7 +522,7 @@ function my_hist_2d(x, y, bins; normed = false, weights = nothing) xedges, yedges, counts ./ norm_denom end -centers(v::AVec) = v[1] + cumsum(diff(v)) +centers(v::AVec) = 0.5 * (v[1:end-1] + v[2:end]) @recipe function f(::Type{Val{:histogram2d}}, x, y, z) xedges, yedges, counts = my_hist_2d(x, y, d[:bins],