From 4c21dd1897a23c4845842199bb039572706fbf95 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Tue, 25 Aug 2015 09:46:21 -0400 Subject: [PATCH] first commit; working on structure and readme --- README.md | 48 +++++++++++++++++++++++++ REQUIRE | 4 ++- src/Plot.jl | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 150 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 371e2041..2877dcd0 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,51 @@ # Plot [![Build Status](https://travis-ci.org/tbreloff/Plot.jl.svg?branch=master)](https://travis-ci.org/tbreloff/Plot.jl) + +Plotting interface and wrapper for several plotting packages. + +First, clone the package, and get any plotting packages you need: + +``` +Pkg.clone("https://github.com/JuliaPlot/Plot.jl.git") +Pkg.clone("https://github.com/tbreloff/Qwt.jl.git") # requires pyqt and pyqwt +Pkg.add("Gadfly") # might also need to Pkg.checkout("Gadfly") and maybe Colors/Compose... I had trouble with it +``` + +Now load it in: + +``` +using Plot +``` + +Do a plot in Qwt, then save a png: + +``` +plotter(:Qwt) +plot(1:10) +savepng(ans, Plot.IMG_DIR * "qwt1.png") +``` + +which saves: + +![qwt_plt](img/qwt1.png) + + +Do a plot in Gadfly, then save a png: + +``` +plotter(:Gadfly) +plot(1:10) +savepng(ans, Plot.IMG_DIR * "gadfly1.png", 6Gadfly.inch, 4Gadfly.inch) +``` + +which saves: + +![gadfly_plt](img/gadfly1.png) + + + +# Author + +Thomas Breloff (@tbreloff) + diff --git a/REQUIRE b/REQUIRE index 2c4ef82c..ac4e90d4 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1 +1,3 @@ -julia 0.3 +julia 0.4- + +Requires diff --git a/src/Plot.jl b/src/Plot.jl index a71f9972..25c3d92a 100644 --- a/src/Plot.jl +++ b/src/Plot.jl @@ -1,5 +1,103 @@ module Plot -# package code goes here +using Requires + +# these are the plotting packages you can load. we use lazymod so that we +# don't "import" the module until we want it +@lazymod Qwt +@lazymod Gadfly + +# --------------------------------------------------------- + +abstract PlottingPackage + +immutable QwtPackage <: PlottingPackage end +immutable GadflyPackage <: PlottingPackage end + +const AVAILABLE_PACKAGES = [:Qwt, :Gadfly] +const INITIALIZED_PACKAGES = Set{Symbol}() + +type CurrentPackage + pkg::Nullable{PlottingPackage} +end + +const CURRENT_PACKAGE = CurrentPackage(Nullable{PlottingPackage}()) +function currentPackage() + if isnull(CURRENT_PACKAGE.pkg) + error("Must choose a plotter. Example: `plotter(:Qwt)`") + end + get(CURRENT_PACKAGE.pkg) +end + +doc""" +Setup the plot environment. +`plotter(:Qwt)` will load package Qwt.jl and map all subsequent plot commands to that package. +Same for `plotter(:Gadfly)`, etc. +""" +function plotter(modname) + if modname == :Qwt + if !(modname in INITIALIZED_PACKAGES) + qwt() + push!(INITIALIZED_PACKAGES, modname) + end + global Qwt = Main.Qwt + CURRENT_PACKAGE.pkg = Nullable(QwtPackage()) + return + elseif modname == :Gadfly + if !(modname in INITIALIZED_PACKAGES) + gadfly() + push!(INITIALIZED_PACKAGES, modname) + end + global Gadfly = Main.Gadfly + CURRENT_PACKAGE.pkg = Nullable(GadflyPackage()) + return + end + error("Unknown plotter $modname. Choose from: $AVAILABLE_PACKAGES") +end + +# --------------------------------------------------------- + +const IMG_DIR = "$(ENV["HOME"])/.julia/v0.4/Plot/img/" + + +# --------------------------------------------------------- + +# Qwt + +plot(::QwtPackage, args...; kwargs...) = Qwt.plot(args...; kwargs...) +savepng(::QwtPackage, plt, fn::String, args...) = Qwt.savepng(plt, fn) + +# --------------------------------------------------------- + +# Gadfly + +plot(::GadflyPackage, y; kwargs...) = Gadfly.plot(; x = 1:length(y), y = y, kwargs...) +plot(::GadflyPackage, x, y; kwargs...) = Gadfly.plot(; x = x, y = y, kwargs...) +plot(::GadflyPackage, args...; kwargs...) = Gadfly.plot(args...; kwargs...) +savepng(::GadflyPackage, plt, fn::String, args...) = Gadfly.draw(Gadfly.PNG(fn, args...), plt) + + +# --------------------------------------------------------- + + +export + plotter, + plot, + savepng + +doc""" +The main plot command. You must call `plotter(:ModuleName)` to set the current plotting environment first. +Commands are converted into the relevant plotting commands for that package: + plotter(:Gadfly) + plot(1:10) # this calls `y = 1:10; Gadfly.plot(x=1:length(y), y=y)` + plotter(:Qwt) + plot(1:10) # this calls `Qwt.plot(1:10)` +""" +plot(args...; kwargs...) = plot(currentPackage(), args...; kwargs...) +savepng(args...; kwargs...) = savepng(currentPackage(), args...; kwargs...) + + + +# --------------------------------------------------------- end # module