From 1c70346a61d8857167d24aca96b4c7f3c27e5d6a Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Wed, 7 Oct 2015 13:45:38 -0400 Subject: [PATCH] bringing pyplot up to speed --- docs/example_generation.jl | 15 ++++++--------- src/backends/pyplot.jl | 32 ++++++++++++++++++++++++++------ src/colors.jl | 8 ++++---- src/subplot.jl | 14 +++++++++++--- 4 files changed, 47 insertions(+), 22 deletions(-) diff --git a/docs/example_generation.jl b/docs/example_generation.jl index 1980d3a1..842b5555 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -23,17 +23,14 @@ const examples = PlotExample[ "A simple line plot of the columns.", [:(plot(cumsum(randn(50,10),1), w=3))]), PlotExample("Functions", - "Plot multiple functions. You can also put the function first.", + "Plot multiple functions. You can also put the function first, or use the form `plot(f, xmin, xmax)` where f is a Function or AbstractVector{Function}.", [:(plot(0:0.01:4π, [sin,cos]))]), - PlotExample("", - "You can also call it with plot(f, xmin, xmax).", - [:(plot([sin,cos], 0, 4π))]), PlotExample("", "Or make a parametric plot (i.e. plot: (fx(u), fy(u))) with plot(fx, fy, umin, umax).", - [:(plot(sin, x->sin(2x), 0, 2π, legend=false, fill=0))]), + [:(plot(sin, x->sin(2x), 0, 2π, leg=false, fill=(0,:orange)))]), PlotExample("Global", - "Change the guides/background/limits/ticks. You can also use shorthand functions: `title!`, `xlabel!`, `ylabel!`, `xlims!`, `ylims!`, `xticks!`, `yticks!`", - [:(plot(rand(10), title="TITLE", xlabel="XLABEL", ylabel="YLABEL", background_color = RGB(0.2,0.2,0.2), xlim=(-3,13), yticks=0:0.1:1))]), + "Change the guides/background/limits/ticks. Convenience args `xaxis` and `yaxis` allow you to pass a tuple or value which will be mapped to the relevant args automatically. The `xaxis` below will be replaced with `xlabel` and `xlims` args automatically during the preprocessing step. You can also use shorthand functions: `title!`, `xaxis!`, `yaxis!`, `xlabel!`, `ylabel!`, `xlims!`, `ylims!`, `xticks!`, `yticks!`", + [:(plot(rand(20,3), title="TITLE", xaxis=("XLABEL",(-5,30),0:2:20,:flip), yaxis=("YLABEL",:log10), background_color = RGB(0.2,0.2,0.2), leg=false))]), PlotExample("Two-axis", "Use the `axis` arguments.\n\nNote: Currently only supported with Qwt and PyPlot", [:(plot(Vector[randn(100), randn(100)*100]; axis = [:l :r], ylabel="LEFT", yrightlabel="RIGHT"))]), @@ -42,10 +39,10 @@ const examples = PlotExample[ [:(plot(Vector[rand(10), rand(20)]; marker=:ellipse, markersize=8, c=(:red, :blue)))]), PlotExample("Build plot in pieces", "Start with a base plot...", - [:(plot(rand(100)/3, reg=true, fill=0))]), + [:(plot(rand(100)/3, reg=true, fill=(0,:green)))]), PlotExample("", "and add to it later.", - [:(scatter!(rand(100), markersize=6, c=:blue))]), + [:(scatter!(rand(100), markersize=6, c=:orange))]), PlotExample("Heatmaps", "", [:(heatmap(randn(10000),randn(10000), nbins=100))]), diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index dd3ecd31..06f5edf5 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -15,7 +15,8 @@ supportedArgs(::PyPlotPackage) = [ :axis, :background_color, :color, - # :fill, + :fillrange, + :fillcolor, :foreground_color, :group, # :heatmap_c, @@ -64,6 +65,7 @@ subplotSupported(::PyPlotPackage) = false # convert colorant to 4-tuple RGBA getPyPlotColor(c::Colorant) = map(f->float(f(c)), (red, green, blue, alpha)) +getPyPlotColor(scheme::ColorScheme) = getPyPlotColor(getColor(scheme)) # get the style (solid, dashed, etc) function getPyPlotLineStyle(linetype::Symbol, linestyle::Symbol) @@ -277,7 +279,18 @@ function plot!(pkg::PyPlotPackage, plt::Plot; kw...) end # this sets the bg color inside the grid - fig.o[:axes][1][:set_axis_bgcolor](getPyPlotColor(plt.initargs[:background_color])) + ax = getLeftAxis(fig) + ax[:set_axis_bgcolor](getPyPlotColor(plt.initargs[:background_color])) + + fillrange = d[:fillrange] + if fillrange != nothing + fillcolor = getPyPlotColor(d[:fillcolor]) + if typeof(fillrange) <: Union{Real, AVec} + ax[:fill_between](d[:x], fillrange, d[:y], facecolor = fillcolor) + else + ax[:fill_between](d[:x], fillrange..., facecolor = fillcolor) + end + end push!(plt.seriesargs, d) plt @@ -323,16 +336,23 @@ function updatePlotItems(plt::Plot{PyPlotPackage}, d::Dict) ax[:set_ylabel](d[:yrightlabel]) end + # scales + ax = getLeftAxis(fig) + haskey(d, :xscale) && applyPyPlotScale(ax, d[:xscale], true) + haskey(d, :yscale) && applyPyPlotScale(ax, d[:yscale], false) + # limits and ticks haskey(d, :xlims) && addPyPlotLims(d[:xlims], true) haskey(d, :ylims) && addPyPlotLims(d[:ylims], false) haskey(d, :xticks) && addPyPlotTicks(d[:xticks], true) haskey(d, :yticks) && addPyPlotTicks(d[:yticks], false) - # scales - ax = getLeftAxis(fig) - haskey(d, :xscale) && applyPyPlotScale(ax, d[:xscale], true) - haskey(d, :yscale) && applyPyPlotScale(ax, d[:yscale], false) + if get(d, :xflip, false) + ax[:invert_xaxis]() + end + if get(d, :yflip, false) + ax[:invert_yaxis]() + end end diff --git a/src/colors.jl b/src/colors.jl index 92796f7b..57d31f94 100644 --- a/src/colors.jl +++ b/src/colors.jl @@ -71,12 +71,12 @@ const _masterColorList = [ function darken(c, v=0.1) rgb = RGB(c) - r = max(0, rgb.r - v) - g = max(0, rgb.g - v) - b = max(0, rgb.b - v) + r = max(0, min(rgb.r - v, 1)) + g = max(0, min(rgb.g - v, 1)) + b = max(0, min(rgb.b - v, 1)) RGB(r,g,b) end -function lighten(c, v=0.1) +function lighten(c, v=0.3) darken(c, -v) end diff --git a/src/subplot.jl b/src/subplot.jl index dc7ae2a9..417f2840 100644 --- a/src/subplot.jl +++ b/src/subplot.jl @@ -47,6 +47,11 @@ convertSeriesIndex(subplt::Subplot, n::Int) = ceil(Int, n / subplt.p) # ------------------------------------------------------------ +function validateSubplotSupported() + if !subplotSupported() + error(CURRENT_BACKEND.sym, " does not support the subplot/subplot! commands at this time. Try one of: ", join(filter(pkg->subplotSupported(backendInstance(pkg)), backends()),", ")) + end +end doc""" Create a series of plots: @@ -59,6 +64,7 @@ Create a series of plots: ``` """ function subplot(args...; kw...) + validateSubplotSupported() d = Dict(kw) preprocessArgs!(d) @@ -102,6 +108,7 @@ Adds to a subplot. # current subplot function subplot!(args...; kw...) + validateSubplotSupported() subplot!(current(), args...; kw...) end @@ -114,9 +121,10 @@ end # # this adds to a specific subplot... most plot commands will flow through here function subplot!(subplt::Subplot, args...; kw...) - if !subplotSupported() - error(CURRENT_BACKEND.sym, " does not support the subplot/subplot! commands at this time. Try one of: ", join(filter(pkg->subplotSupported(backendInstance(pkg)), backends()),", ")) - end + validateSubplotSupported() + # if !subplotSupported() + # error(CURRENT_BACKEND.sym, " does not support the subplot/subplot! commands at this time. Try one of: ", join(filter(pkg->subplotSupported(backendInstance(pkg)), backends()),", ")) + # end d = Dict(kw) preprocessArgs!(d)