From 32e816b713a3f88848dc63eb9621b4775be679a7 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Thu, 2 Jun 2016 20:22:58 -0500 Subject: [PATCH] working on axis limit fixes --- src/args.jl | 2 +- src/axes.jl | 52 ++++++++++++++++++++++++++++++++++++++++++ src/backends/gr.jl | 4 ++-- src/backends/pyplot.jl | 38 ++++++++++++++++-------------- src/plot.jl | 34 ++++++++++++++------------- 5 files changed, 94 insertions(+), 36 deletions(-) diff --git a/src/args.jl b/src/args.jl index 6b7bebb1..0f2b3c61 100644 --- a/src/args.jl +++ b/src/args.jl @@ -165,7 +165,7 @@ const _series_defaults = KW( :levels => 15, :orientation => :vertical, :bar_position => :overlay, # for bar plots and histograms: could also be stack (stack up) or dodge (side by side) - :bar_width => 0.8, + :bar_width => nothing, :bar_edges => false, :xerror => nothing, :yerror => nothing, diff --git a/src/axes.jl b/src/axes.jl index 11eafb94..814f8b10 100644 --- a/src/axes.jl +++ b/src/axes.jl @@ -130,6 +130,58 @@ function expand_extrema!{N<:Number}(axis::Axis, v::AVec{N}) ex end + +function expand_extrema!(sp::Subplot, d::KW) + # first expand for the data + for letter in (:x, :y, :z) + data = d[letter] + axis = sp.attr[Symbol(letter, "axis")] + if eltype(data) <: Number + expand_extrema!(axis, data) + elseif isa(data, Surface) && eltype(data.surf) <: Number + expand_extrema!(axis, data) + elseif data != nothing + # TODO: need more here... gotta track the discrete reference value + # as well as any coord offset (think of boxplot shape coords... they all + # correspond to the same x-value) + # @show letter,eltype(data),typeof(data) + d[letter], d[Symbol(letter,"_discrete_indices")] = discrete_value!(axis, data) + end + end + + # # expand for fillrange/bar_width + # fillaxis, baraxis = sp.attr[:yaxis], sp.attr[:xaxis] + # if isvertical(d) + # fillaxis, baraxis = baraxis, fillaxis + # end + + # expand for fillrange + vert = isvertical(d) + fr = d[:fillrange] + if fr == nothing && d[:seriestype] == :bar + fr = 0.0 + end + expand_extrema!(sp.attr[vert ? :yaxis : :xaxis], fr) + # @show d[:fillrange] d[:bar_width] + + # expand for bar_width + if d[:seriestype] == :bar + dsym = vert ? :x : :y + data = d[dsym] + + bw = d[:bar_width] + if bw == nothing + bw = d[:bar_width] = mean(diff(data)) + end + @show data bw + + axis = sp.attr[Symbol(dsym, :axis)] + expand_extrema!(axis, maximum(data) + 0.5maximum(bw)) + expand_extrema!(axis, minimum(data) - 0.5minimum(bw)) + end + +end + # ------------------------------------------------------------------------- # push the limits out slightly diff --git a/src/backends/gr.jl b/src/backends/gr.jl index 836e7446..39401eb9 100644 --- a/src/backends/gr.jl +++ b/src/backends/gr.jl @@ -633,14 +633,14 @@ function gr_display(sp::Subplot{GRBackend}, w, h, viewport_canvas) for axis_idx = 1:num_axes xmin, xmax, ymin, ymax = extrema[axis_idx,:] if scale & GR.OPTION_X_LOG == 0 - xmin, xmax = GR.adjustlimits(xmin, xmax) + # xmin, xmax = GR.adjustlimits(xmin, xmax) majorx = 5 xtick = GR.tick(xmin, xmax) / majorx else xtick = majorx = 1 end if scale & GR.OPTION_Y_LOG == 0 - ymin, ymax = GR.adjustlimits(ymin, ymax) + # ymin, ymax = GR.adjustlimits(ymin, ymax) majory = 5 ytick = GR.tick(ymin, ymax) / majory else diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index c79f169e..617b2706 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -40,7 +40,7 @@ supportedArgs(::PyPlotBackend) = [ supportedAxes(::PyPlotBackend) = _allAxes supportedTypes(::PyPlotBackend) = [ :none, :line, :path, :steppre, :steppost, :shape, - :scatter, :histogram2d, :hexbin, :histogram, :density, + :scatter, :histogram2d, :hexbin, #:histogram, #:density, :bar, :sticks, #:box, :violin, :quiver, :hline, :vline, :heatmap, :pie, :image, :contour, :contour3d, :path3d, :scatter3d, :surface, :wireframe @@ -507,7 +507,11 @@ function _series_added(plt::Plot{PyPlotBackend}, series::Series) end if st == :bar - extrakw[isvertical(d) ? :width : :height] = d[:bar_width] + bw = d[:bar_width] + if bw == nothing + bw = mean(diff(isvertical(d) ? x : y)) + end + extrakw[isvertical(d) ? :width : :height] = bw fr = get(d, :fillrange, nothing) if fr != nothing extrakw[:bottom] = fr @@ -568,21 +572,21 @@ function _series_added(plt::Plot{PyPlotBackend}, series::Series) push!(handles, handle) end - if st == :histogram - handle = ax[:hist](y; - label = d[:label], - zorder = plt.n, - color = pyfillcolor(d), - edgecolor = pylinecolor(d), - linewidth = d[:linewidth], - bins = d[:bins], - normed = d[:normalize], - weights = d[:weights], - orientation = (isvertical(d) ? "vertical" : "horizontal"), - histtype = (d[:bar_position] == :stack ? "barstacked" : "bar") - )[3] - push!(handles, handle) - end + # if st == :histogram + # handle = ax[:hist](y; + # label = d[:label], + # zorder = plt.n, + # color = pyfillcolor(d), + # edgecolor = pylinecolor(d), + # linewidth = d[:linewidth], + # bins = d[:bins], + # normed = d[:normalize], + # weights = d[:weights], + # orientation = (isvertical(d) ? "vertical" : "horizontal"), + # histtype = (d[:bar_position] == :stack ? "barstacked" : "bar") + # )[3] + # push!(handles, handle) + # end if st == :histogram2d handle = ax[:hist2d](x, y; diff --git a/src/plot.jl b/src/plot.jl index 78343281..88a16bd3 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -171,24 +171,26 @@ function _apply_series_recipe(plt::Plot, d::KW) end # adjust extrema and discrete info - if st != :image - for letter in (:x, :y, :z) - data = d[letter] - axis = sp.attr[Symbol(letter, "axis")] - if eltype(data) <: Number - expand_extrema!(axis, data) - elseif isa(data, Surface) && eltype(data.surf) <: Number - expand_extrema!(axis, data) - elseif data != nothing - # TODO: need more here... gotta track the discrete reference value - # as well as any coord offset (think of boxplot shape coords... they all - # correspond to the same x-value) - # @show letter,eltype(data),typeof(data) - d[letter], d[Symbol(letter,"_discrete_indices")] = discrete_value!(axis, data) - end - end + if !(st in (:image, :histogram, :histogram2d)) + expand_extrema!(sp, d) + # for letter in (:x, :y, :z) + # data = d[letter] + # axis = sp.attr[Symbol(letter, "axis")] + # if eltype(data) <: Number + # expand_extrema!(axis, data) + # elseif isa(data, Surface) && eltype(data.surf) <: Number + # expand_extrema!(axis, data) + # elseif data != nothing + # # TODO: need more here... gotta track the discrete reference value + # # as well as any coord offset (think of boxplot shape coords... they all + # # correspond to the same x-value) + # # @show letter,eltype(data),typeof(data) + # d[letter], d[Symbol(letter,"_discrete_indices")] = discrete_value!(axis, data) + # end + # end end + # add the series! warnOnUnsupportedArgs(plt.backend, d) warnOnUnsupported(plt.backend, d)