improvements to layout padding; handle axis attr better; fix histogram2d

This commit is contained in:
Thomas Breloff 2016-07-09 12:40:27 -04:00
parent a456ac4c90
commit 1cb0c0071b
4 changed files with 36 additions and 14 deletions

View File

@ -57,13 +57,26 @@ _before_layout_calcs(plt::Plot) = nothing
title_padding(sp::Subplot) = sp[:title] == "" ? 0mm : sp[:titlefont].pointsize * pt title_padding(sp::Subplot) = sp[:title] == "" ? 0mm : sp[:titlefont].pointsize * pt
guide_padding(axis::Axis) = axis[:guide] == "" ? 0mm : axis[:guidefont].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 # Set the (left, top, right, bottom) minimum padding around the plot area
# to fit ticks, tick labels, guides, colorbars, etc. # to fit ticks, tick labels, guides, colorbars, etc.
function _update_min_padding!(sp::Subplot) function _update_min_padding!(sp::Subplot)
leftpad = 10mm + sp[:left_margin] + guide_padding(sp[:yaxis]) # TODO: something different when `is3d(sp) == true`
toppad = 2mm + sp[:top_margin] + title_padding(sp) leftpad = tick_padding(sp[:yaxis]) + sp[:left_margin] + guide_padding(sp[:yaxis])
rightpad = 3mm + sp[:right_margin] toppad = sp[:top_margin] + title_padding(sp)
bottompad = 5mm + sp[:bottom_margin] + guide_padding(sp[:xaxis]) rightpad = sp[:right_margin]
bottompad = tick_padding(sp[:xaxis]) + sp[:bottom_margin] + guide_padding(sp[:xaxis])
# @show (leftpad, toppad, rightpad, bottompad) # @show (leftpad, toppad, rightpad, bottompad)
sp.minpad = (leftpad, toppad, rightpad, bottompad) sp.minpad = (leftpad, toppad, rightpad, bottompad)
end end

View File

@ -1015,10 +1015,10 @@ function _update_min_padding!(sp::Subplot{PyPlotBackend})
# TODO: this should initialize to the margin from sp.attr # 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 # figure out how much the axis components and title "stick out" from the plot area
# leftpad = toppad = rightpad = bottompad = 1mm # leftpad = toppad = rightpad = bottompad = 1mm
leftpad = sp[:left_margin] leftpad = 0mm
toppad = sp[:top_margin] toppad = 0mm
rightpad = sp[:right_margin] rightpad = 0mm
bottompad = sp[:bottom_margin] bottompad = 0mm
for bb in (py_bbox_axis(ax, "x"), py_bbox_axis(ax, "y"), py_bbox_title(ax)) for bb in (py_bbox_axis(ax, "x"), py_bbox_axis(ax, "y"), py_bbox_title(ax))
if ispositive(width(bb)) && ispositive(height(bb)) if ispositive(width(bb)) && ispositive(height(bb))
leftpad = max(leftpad, left(plotbb) - left(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] rightpad = rightpad + sp.attr[:cbar_width]
end 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) sp.minpad = (leftpad, toppad, rightpad, bottompad)
end end

View File

@ -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))) sp = get_subplot(plt, cycle(sps == :auto ? plt.subplots : plt.subplots[sps], command_idx(kw_list,kw)))
kw[:subplot] = sp kw[:subplot] = sp
# extract subplot/axis attributes from kw and add to sp_attr
attr = KW() attr = KW()
for (k,v) in kw for (k,v) in kw
for defdict in (_subplot_defaults, if haskey(_subplot_defaults, k) || haskey(_axis_defaults_byletter, k)
_axis_defaults, attr[k] = pop!(kw, k)
_axis_defaults_byletter) end
if haskey(defdict, k) if haskey(_axis_defaults, k)
attr[k] = pop!(kw, k) v = pop!(kw, k)
for letter in (:x,:y,:z)
attr[Symbol(letter,k)] = v
end end
end end
end end

View File

@ -522,7 +522,7 @@ function my_hist_2d(x, y, bins; normed = false, weights = nothing)
xedges, yedges, counts ./ norm_denom xedges, yedges, counts ./ norm_denom
end 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) @recipe function f(::Type{Val{:histogram2d}}, x, y, z)
xedges, yedges, counts = my_hist_2d(x, y, d[:bins], xedges, yedges, counts = my_hist_2d(x, y, d[:bins],