From 8d5412a38f8e9cff1cdb2c3d5aa66517a553eeed Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Sun, 13 Sep 2015 21:34:28 -0400 Subject: [PATCH] working on pyplot --- docs/example_generation.jl | 2 +- src/backends/pyplot.jl | 100 ++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 14 deletions(-) diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 09f5b2a5..12315c6e 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -36,7 +36,7 @@ const examples = PlotExample[ [:(plot(rand(10); title="TITLE", xlabel="XLABEL", ylabel="YLABEL", background_color = RGB(0.5,0.5,0.5)))]), PlotExample("Two-axis", "Use the `axis` or `axiss` arguments.\n\nNote: This is only supported with Qwt right now", - [:(plot(Vector[randn(100), randn(100)*100]; axiss = [:left,:right]))]), + [:(plot(Vector[randn(100), randn(100)*100]; axiss = [:left,:right], ylabel="LEFT", yrightlabel="RIGHT"))]), PlotExample("Vectors w/ pluralized args", "Plot multiple series with different numbers of points. Mix arguments that apply to all series (singular... see `marker`) with arguments unique to each series (pluralized... see `colors`).", [:(plot(Vector[rand(10), rand(20)]; marker=:ellipse, markersize=8, colors=[:red,:blue]))]), diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index 35b90c42..e8ef5d6a 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -69,15 +69,32 @@ function getPyPlotDrawStyle(linetype::Symbol) return "default" end +# get a reference to the right axis +getLeftAxis(o) = o.o[:axes][1] +getRightAxis(o) = getLeftAxis(o)[:twinx]() + +# left axis is PyPlot., right axis is "f.axes[0].twinx()." +function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol) + if axis == :right + ax = getRightAxis(plt.o) + ax[:set_ylabel](plt.initargs[:yrightlabel]) + return ax[linetype == :hist ? :hist : (linetype in (:sticks,:bar) ? :bar : :plot)] + end + return linetype == :hist ? PyPlot.plt[:hist] : (linetype in (:sticks,:bar) ? PyPlot.bar : PyPlot.plot) +end + function plot(pkg::PyPlotPackage; kw...) # create the figure d = Dict(kw) w,h = map(px2inch, d[:size]) bgcolor = getPyPlotColor(d[:background_color]) - @show w h o = PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = 96) + PyPlot.title(d[:title]) + PyPlot.xlabel(d[:xlabel]) + PyPlot.ylabel(d[:ylabel]) + plt = Plot(o, pkg, 0, d, Dict[]) plt end @@ -89,10 +106,6 @@ end # - fillto/area # - heatmap # - subplot -# title # string or symbol, title of the plot -# xlabel # string or symbol, label on the bottom (x) axis -# ylabel # string or symbol, label on the left (y) axis -# yrightlabel # string or symbol, label on the right (y) axis # reg # true or false, add a regression line for each line # pos # (Int,Int), move the enclosing window to this position # windowtitle # string or symbol, set the title of the enclosing windowtitle @@ -101,17 +114,74 @@ end function plot!(::PyPlotPackage, plt::Plot; kw...) d = Dict(kw) - lt = d[:linetype] - PyPlot.plot(d[:x], d[:y]; figure = plt.o, + extraargs = Dict() + + plotfunc = getPyPlotFunction(plt, d[:axis], lt) + + # we have different args depending on plot type + if lt in (:hist, :sticks, :bar) + + extraargs[:bottom] = d[:fillto] + if lt == :hist + extraargs[:bins] = d[:nbins] + else + extraargs[:width] = (lt == :sticks ? 0.01 : 0.9) + end + + else + + # all but color/label + extraargs[:linestyle] = getPyPlotLineStyle(lt, d[:linestyle]) + extraargs[:marker] = getPyPlotMarker(d[:marker]) + extraargs[:markersize] = d[:markersize] + extraargs[:markerfacecolor] = getPyPlotColor(d[:markercolor]) + extraargs[:drawstyle] = getPyPlotDrawStyle(lt) + + end + + # if lt == :hist + # extraargs[:bins] = d[:nbins] + # elseif lt == :sticks + # extraargs[:width] = 0.01 + # extraargs[:bottom] = d[:fillto] + # elseif lt == :bar + # extraargs[:width] = 0.9 + # extraargs[:bottom] = d[:fillto] + # end + + # namespace = d[:axis] == :right ? plt.o[:axes]()[:twiny]() : PyPlot + # plotfunc = plot + # if lt == :hist + # plotfunc = hist + # extraargs[:bins] = d[:nbins] + # elseif lt in (:sticks, :bar) + # plotfunc = bar + # extraargs[:width] = (lt == :sticks ? 0.01 : 0.9) + # end + +# >>> p.plot([1,2],[2,1]) +# [] +# >>> a2 = f.axes[0].twinx() +# >>> a2.plot([1,2],[1,2]) +# [] +# >>> f.show() + + # PyPlot.plot + + dump(plotfunc) + + plotfunc(d[:x], d[:y]; + figure = plt.o, color = getPyPlotColor(d[:color]), linewidth = d[:width], - linestyle = getPyPlotLineStyle(lt, d[:linestyle]), - marker = getPyPlotMarker(d[:marker]), - markersize = d[:markersize], - markerfacecolor = getPyPlotColor(d[:markercolor]), - drawstyle = getPyPlotDrawStyle(lt), + # linestyle = getPyPlotLineStyle(lt, d[:linestyle]), + # marker = getPyPlotMarker(d[:marker]), + # markersize = d[:markersize], + # markerfacecolor = getPyPlotColor(d[:markercolor]), + # drawstyle = getPyPlotDrawStyle(lt), label = d[:label], + extraargs... ) if plt.initargs[:legend] @@ -128,7 +198,11 @@ end # ------------------------------- -savepng(::PyPlotPackage, plt::PlottingObject, fn::String, args...) = error("unsupported") +function savepng(::PyPlotPackage, plt::PlottingObject, fn::String, args...) + f = open(fn) + writemime(f, MIME"image/png") + close(f) +end # -------------------------------