diff --git a/README.md b/README.md index 65c7fe22..7459a9d6 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,10 @@ When plotting multiple lines, you can give every line the same trait by using th - [ ] TikzGraphs.jl - [ ] GraphLayout.jl +# Backends + +See the wiki at: https://github.com/JuliaPlot/juliaplot_docs/wiki + # Author Thomas Breloff (@tbreloff) diff --git a/src/args.jl b/src/args.jl index 2a300403..72648449 100644 --- a/src/args.jl +++ b/src/args.jl @@ -82,16 +82,10 @@ function getRGBColor(c, n::Int = 0) end -# note: i is the index of this series within this call, n is the index of the series from all calls to plot/subplot -function getPlotKeywordArgs(kw, i::Int, n::Int) +# note: idx is the index of this series within this call, n is the index of the series from all calls to plot/subplot +function getPlotKeywordArgs(kw, idx::Int, n::Int) d = Dict(kw) - if n == 0 - delete!(d, :x) - delete!(d, :y) - end - # outd = Dict() - # default to a white background, but only on the initial call (so we don't change the background automatically) if haskey(d, :background_color) d[:background_color] = getRGBColor(d[:background_color]) @@ -102,17 +96,25 @@ function getPlotKeywordArgs(kw, i::Int, n::Int) # fill in d with either 1) plural value, 2) value, 3) default for k in keys(PLOT_DEFAULTS) plural = makeplural(k) - if haskey(d, plural) - d[k] = d[plural][i] - elseif haskey(d, k) - d[k] = d[k] - else - d[k] = PLOT_DEFAULTS[k] + # if haskey(d, plural) + # d[k] = d[plural][idx] + if !haskey(d, k) + if n == 0 || k != :size + d[k] = haskey(d, plural) ? d[plural][idx] : PLOT_DEFAULTS[k] + end end + delete!(d, plural) end - # once the plot is created, we can get line/marker colors - if n > 0 + + + # handle plot initialization differently + if n == 0 + delete!(d, :x) + delete!(d, :y) + else + # once the plot is created, we can get line/marker colors + # update color d[:color] = getRGBColor(d[:color], n) diff --git a/src/gadfly.jl b/src/gadfly.jl index 655fad78..4530b3dc 100644 --- a/src/gadfly.jl +++ b/src/gadfly.jl @@ -112,3 +112,22 @@ function savepng(::GadflyPackage, plt::Plot, fn::String; end +# ------------------------------- + +# # create the underlying object (each backend will do this differently) +# o = buildSubplotObject(plts, pkg, layout) + +function buildSubplotObject!(::GadflyPackage, subplt::Subplot) + i = 0 + rows = [] + for rowcnt in subplt.layout.rowcounts + push!(rows, Gadfly.hstack([plt.o for plt in subplt.plts[(1:rowcnt) + i]]...)) + i += rowcnt + end + subplt.o = Gadfly.vstack(rows...) +end + + +function Base.display(::GadflyPackage, subplt::Subplot) + display(subplt.o) +end diff --git a/src/plot.jl b/src/plot.jl index e6ab6bff..769b0db1 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -96,6 +96,11 @@ function plot!(args...; kw...) plot!(currentPlot(), args...; kw...) end +# not allowed: +function plot!(subplt::Subplot, args...; kw...) + error("Can't call plot! on a Subplot!") +end + # this adds to a specific plot... most plot commands will flow through here function plot!(plt::Plot, args...; kw...) diff --git a/src/qwt.jl b/src/qwt.jl index 571207ae..097580d5 100644 --- a/src/qwt.jl +++ b/src/qwt.jl @@ -47,16 +47,14 @@ savepng(::QwtPackage, plt::PlottingObject, fn::String, args...) = Qwt.savepng(pl # # create the underlying object (each backend will do this differently) # o = buildSubplotObject(plts, pkg, layout) -function buildSubplotObject(plts::Vector{Plot}, pkg::QwtPackage, layout::SubplotLayout) - @show plts pkg layout +function buildSubplotObject!(::QwtPackage, subplt::Subplot) i = 0 rows = [] - for rowcnt in layout.rowcounts - push!(rows, Qwt.hsplitter([plt.o for plt in plts[(1:rowcnt) + i]]...)) + for rowcnt in subplt.layout.rowcounts + push!(rows, Qwt.hsplitter([plt.o for plt in subplt.plts[(1:rowcnt) + i]]...)) i += rowcnt end - @show rows - Qwt.vsplitter(rows...) + subplt.o = Qwt.vsplitter(rows...) end diff --git a/src/subplot.jl b/src/subplot.jl index e0909e21..dea9f1bf 100644 --- a/src/subplot.jl +++ b/src/subplot.jl @@ -3,26 +3,26 @@ # create a layout directly SubplotLayout(rowcounts::AbstractVector{Int}) = SubplotLayout(sum(rowcounts), rowcounts) -# create a layout given counts... numrows/numcols == -1 implies we figure out a good number automatically -function SubplotLayout(numplts::Int, numrows::Int, numcols::Int) +# create a layout given counts... nr/nc == -1 implies we figure out a good number automatically +function SubplotLayout(numplts::Int, nr::Int, nc::Int) # figure out how many rows/columns we need - if numrows == -1 - if numcols == -1 - numrows = round(Int, sqrt(numplts)) - numcols = ceil(Int, numplts / numrows) + if nr == -1 + if nc == -1 + nr = round(Int, sqrt(numplts)) + nc = ceil(Int, numplts / nr) else - numrows = ceil(Int, numplts / numcols) + nr = ceil(Int, numplts / nc) end else - numcols = ceil(Int, numplts / numrows) + nc = ceil(Int, numplts / nr) end # create the rowcounts vector i = 0 rowcounts = Int[] - for r in 1:numrows - cnt = min(numcols, numplts - i) + for r in 1:nr + cnt = min(nc, numplts - i) push!(rowcounts, cnt) i += cnt end @@ -48,11 +48,13 @@ getplot(subplt::Subplot) = subplt.plts[mod1(subplt.n, subplt.p)] doc""" Create a series of plots: +``` y = rand(100,3) - subplot(y; n = 3) # create an automatic grid, and let it figure out the numrows/numcols... will put plots 1 and 2 on the first row, and plot 3 by itself on the 2nd row - subplot(y; n = 3, numrows = 1) # create an automatic grid, but fix the number of rows to 1 (so there are n columns) - subplot(y; n = 3, numcols = 1) # create an automatic grid, but fix the number of columns to 1 (so there are n rows) - subplot(y; layout = [1, 2]) # explicit layout by row... plot #1 goes by itself in the first row, plots 2 and 3 split the 2nd row (note the n kw is unnecessary) + subplot(y; n = 3) # create an automatic grid, and let it figure out the nr/nc... will put plots 1 and 2 on the first row, and plot 3 by itself on the 2nd row + subplot(y; n = 3, nr = 1) # create an automatic grid, but fix the number of rows to 1 (so there are n columns) + subplot(y; n = 3, nc = 1) # create an automatic grid, but fix the number of columns to 1 (so there are n rows) + subplot(y; layout = [1, 2]) # explicit layout by row... plot #1 goes by itself in the first row, plots 2 and 3 split the 2nd row (note the n kw is unnecessary) +``` """ function subplot(args...; kw...) d = Dict(kw) @@ -64,7 +66,7 @@ function subplot(args...; kw...) if !haskey(d, :n) error("You must specify either layout or n when creating a subplot: ", d) end - layout = SubplotLayout(d[:n], get(d, :numrows, -1), get(d, :numcols, -1)) + layout = SubplotLayout(d[:n], get(d, :nr, -1), get(d, :nc, -1)) end # initialize the individual plots @@ -72,12 +74,11 @@ function subplot(args...; kw...) kw0 = getPlotKeywordArgs(kw, 1, 0) plts = Plot[plot(pkg; kw0..., show=false) for i in 1:length(layout)] - # create the underlying object (each backend will do this differently) - o = buildSubplotObject(plts, pkg, layout) - # create the object and do the plotting - subplt = Subplot(o, plts, pkg, length(layout), 0, layout) + subplt = Subplot(nothing, plts, pkg, length(layout), 0, layout) subplot!(subplt, args...; kw...) + + subplt end doc""" @@ -105,6 +106,10 @@ function subplot!(subplt::Subplot, args...; kw...) plot!(plt; d...) end + # create the underlying object (each backend will do this differently) + buildSubplotObject!(subplt.plotter, subplt) + + # set this to be current currentPlot!(subplt) # do we want to show it? @@ -117,35 +122,3 @@ function subplot!(subplt::Subplot, args...; kw...) end -# # # this creates a new plot with args/kw and sets it to be the current plot -# # function plot(args...; kw...) -# # plt = plot(plotter(); getPlotKeywordArgs(kw, 1, 0)...) # create a new, blank plot -# # plot!(plt, args...; kw...) # add to it -# # end - -# # # this adds to the current plot -# # function plot!(args...; kw...) -# # plot!(currentPlot(), args...; kw...) -# # end - -# # this adds to a specific plot... most plot commands will flow through here -# function plot!(plt::Plot, args...; kw...) - -# kwList = createKWargsList(plt, args...; kw...) -# for (i,d) in enumerate(kwList) -# plt.n += 1 -# plot!(plt.plotter, plt; d...) -# end - -# currentPlot!(plt) - -# # do we want to show it? -# d = Dict(kw) -# if haskey(d, :show) && d[:show] -# display(plt) -# end - -# plt -# end - -