diff --git a/src/args.jl b/src/args.jl index 36d6cc2a..2479a050 100644 --- a/src/args.jl +++ b/src/args.jl @@ -654,7 +654,7 @@ function getSeriesArgs(pkg::PlottingPackage, initargs::Dict, kw, commandIndex::I # set label label = d[:label] label = (label == "AUTO" ? "y$globalIndex" : label) - if d[:axis] == :right && length(label) >= 4 && label[end-3:end] != " (R)" + if d[:axis] == :right && !(length(label) >= 4 && label[end-3:end] != " (R)") label = string(label, " (R)") end d[:label] = label diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index f56f4971..cc9c09bf 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -153,13 +153,13 @@ end function plot(pkg::PyPlotPackage; kw...) # create the figure d = Dict(kw) - w,h = map(px2inch, d[:size]) - bgcolor = getPyPlotColor(d[:background_color]) # standalone plots will create a figure, but not if part of a subplot (do it later) if haskey(d, :subplot) wrap = nothing else + w,h = map(px2inch, d[:size]) + bgcolor = getPyPlotColor(d[:background_color]) wrap = PyPlotFigWrapper(PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = 96)) end @@ -257,9 +257,13 @@ function plot!(pkg::PyPlotPackage, plt::Plot; kw...) d[:serieshandle] = if lt == :hist plotfunc(d[:y]; extra_kwargs...)[1] elseif lt == :contour - handle = plotfunc(d[:x], d[:y], d[:surface], d[:nlevels]; extra_kwargs...) + # NOTE: x/y are backwards in pyplot, so we switch the x and y args, + # and take the transpose of the surface matrix + x, y = d[:y], d[:x] + surf = d[:surface]' + handle = plotfunc(x, y, surf, d[:nlevels]; extra_kwargs...) if d[:fillrange] != nothing - handle = ax[:contourf](d[:x], d[:y], d[:surface], d[:nlevels]; cmap = getPyPlotColorMap(d[:fillcolor])) + handle = ax[:contourf](x, y, surf, d[:nlevels]; cmap = getPyPlotColorMap(d[:fillcolor])) end handle elseif lt in (:scatter, :heatmap, :hexbin) @@ -461,7 +465,7 @@ end # ----------------------------------------------------------------- -# create the underlying object (each backend will do this differently) +# NOTE: pyplot needs to build before function buildSubplotObject!(subplt::Subplot{PyPlotPackage}, isbefore::Bool) l = subplt.layout @@ -484,6 +488,33 @@ function buildSubplotObject!(subplt::Subplot{PyPlotPackage}, isbefore::Bool) true end +# this will be called internally, when creating a subplot from existing plots +# NOTE: if I ever need to "Rebuild a "ubplot from individual Plot's"... this is what I should use! +function subplot(plts::AVec{Plot{PyPlotPackage}}, layout::SubplotLayout, d::Dict) + validateSubplotSupported() + + p = length(layout) + n = sum([plt.n for plt in plts]) + + pkg = PyPlotPackage() + newplts = Plot{PyPlotPackage}[plot(pkg; subplot=true, plt.initargs...) for plt in plts] + + subplt = Subplot(nothing, newplts, PyPlotPackage(), p, n, layout, d, true, false, false, (r,c) -> (nothing,nothing)) + + preprocessSubplot(subplt, d) + buildSubplotObject!(subplt, true) + + for (i,plt) in enumerate(plts) + for seriesargs in plt.seriesargs + _plot_from_subplot!(newplts[i]; seriesargs...) + end + end + + postprocessSubplot(subplt, d) + + subplt +end + function handleLinkInner(plt::Plot{PyPlotPackage}, isx::Bool) if isx @@ -517,13 +548,22 @@ function addPyPlotLegend(plt::Plot, ax) end function finalizePlot(plt::Plot{PyPlotPackage}) - wrap = plt.o ax = getLeftAxis(plt) addPyPlotLegend(plt, ax) updateAxisColors(ax, getPyPlotColor(plt.initargs[:foreground_color])) PyPlot.draw() end +function finalizePlot(subplt::Subplot{PyPlotPackage}) + fig = subplt.o.fig + for (i,plt) in enumerate(subplt.plts) + ax = getLeftAxis(plt) + addPyPlotLegend(plt, ax) + updateAxisColors(ax, getPyPlotColor(plt.initargs[:foreground_color])) + end + PyPlot.draw() +end + # # allow for writing any supported mime # for mime in keys(PyPlot.aggformats) # @eval function Base.writemime(io::IO, m::MIME{symbol{$mime}}, plt::Plot{PyPlotPackage}) @@ -544,21 +584,16 @@ function Base.display(::PlotsDisplay, plt::PlottingObject{PyPlotPackage}) if isa(Base.Multimedia.displays[end], Base.REPL.REPLDisplay) display(getfig(plt.o)) else - PyPlot.ion() - PyPlot.figure(getfig(plt.o).o[:number]) - PyPlot.draw_if_interactive() - PyPlot.ioff() + # # PyPlot.ion() + # PyPlot.figure(getfig(plt.o).o[:number]) + # PyPlot.draw_if_interactive() + # # PyPlot.ioff() end + # PyPlot.plt[:show](block=false) + getfig(plt.o)[:show]() end -function finalizePlot(subplt::Subplot{PyPlotPackage}) - fig = subplt.o.fig - for (i,plt) in enumerate(subplt.plts) - finalizePlot(plt) - end -end - # function Base.display(::PlotsDisplay, subplt::Subplot{PyPlotPackage}) # finalizePlot(subplt) # PyPlot.ion() diff --git a/src/plot.jl b/src/plot.jl index 6c27f34c..d8fc738d 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -271,9 +271,9 @@ function createKWargsList(plt::PlottingObject, x, y; kw...) # build the series arg dict numUncounted = get(d, :numUncounted, 0) n = plt.n + i + numUncounted - dumpdict(d, "before getSeriesArgs") + # dumpdict(d, "before getSeriesArgs") d = getSeriesArgs(plt.backend, getinitargs(plt, n), d, i + numUncounted, convertSeriesIndex(plt, n), n) - dumpdict(d, "after getSeriesArgs") + # dumpdict(d, "after getSeriesArgs") d[:x], d[:y] = computeXandY(xs[mod1(i,mx)], ys[mod1(i,my)]) if haskey(d, :idxfilter) diff --git a/src/plotter.jl b/src/plotter.jl index b1e1fa9f..c1dea79c 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -14,15 +14,23 @@ export immerse, pyplot, qwt, - unicodeplots, - winston + unicodeplots + # winston gadfly() = backend(:gadfly) immerse() = backend(:immerse) pyplot() = backend(:pyplot) qwt() = backend(:qwt) unicodeplots() = backend(:unicodeplots) -winston() = backend(:winston) +# winston() = backend(:winston) + +const _backendNames = Dict( + GadflyPackage() => :gadfly, + ImmersePackage() => :immerse, + PyPlotPackage() => :pyplot, + QwtPackage() => :qwt, + UnicodePlotsPackage() => :unicodeplots, + ) include("backends/supported.jl") @@ -211,8 +219,13 @@ function backend() end """ -Set the plot backend. Choose from: :qwt, :gadfly, :unicodeplots +Set the plot backend. Choose from: :qwt, :gadfly, :unicodeplots, :immerse, :pyplot """ +function backend(pkg::PlottingPackage) + CURRENT_BACKEND.sym = _backendNames(pkg) + CURRENT_BACKEND.pkg = pkg +end + function backend(modname) # set the PlottingPackage diff --git a/src/subplot.jl b/src/subplot.jl index ea2a501d..cbf6a798 100644 --- a/src/subplot.jl +++ b/src/subplot.jl @@ -349,6 +349,7 @@ function subplot!(subplt::Subplot, args...; kw...) subplt.n += 1 plt = getplot(subplt) + plt.n += 1 # # update the plot's initargs for things such as palettes, etc # for (k,v) in subplt.initargs