working on subplot
This commit is contained in:
parent
fb2561e7e5
commit
8cb710ff5d
@ -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)
|
||||
|
||||
34
src/args.jl
34
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)
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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...)
|
||||
|
||||
|
||||
10
src/qwt.jl
10
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
|
||||
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user