diff --git a/docs/winston_examples.md b/docs/winston_examples.md new file mode 100644 index 00000000..0c3906d0 --- /dev/null +++ b/docs/winston_examples.md @@ -0,0 +1,166 @@ +# Examples for backend: winston + +- Supported arguments: `args`, `axis`, `color`, `kwargs`, `label`, `legend`, `linestyle`, `linetype`, `marker`, `markersize`, `nbins`, `reg`, `size`, `title`, `width`, `windowtitle`, `xlabel`, `ylabel`, `yrightlabel` +- Supported values for axis: `:auto`, `:left` +- Supported values for linetype: `:none`, `:line`, `:sticks`, `:scatter`, `:hist`, `:bar` +- Supported values for linestyle: `:auto`, `:solid`, `:dashdotdot`, `:dot`, `:dash`, `:dashdot` +- Supported values for marker: `:auto`, `:hexagon`, `:none`, `:dtriangle`, `:ellipse`, `:xcross`, `:rect`, `:star1`, `:star2`, `:cross`, `:utriangle`, `:diamond` +- Is `subplot`/`subplot!` supported? No + +### Initialize + +```julia +using Plots +winston!() +``` + +### Lines + +A simple line plot of the 3 columns. + +```julia +plot(rand(100,3)) +``` + +![](../img/winston/winston_example_1.png) + +### Functions + +Plot multiple functions. You can also put the function first. + +```julia +plot(0:0.01:4π,[sin,cos]) +``` + +![](../img/winston/winston_example_2.png) + +### + +You can also call it with plot(f, xmin, xmax). + +```julia +plot([sin,cos],0,4π) +``` + +![](../img/winston/winston_example_3.png) + +### + +Or make a parametric plot (i.e. plot: (fx(u), fy(u))) with plot(fx, fy, umin, umax). + +```julia +plot(sin,(x->begin # /home/tom/.julia/v0.4/Plots/docs/example_generation.jl, line 33: + sin(2x) + end),0,2π) +``` + +![](../img/winston/winston_example_4.png) + +### Global + +Change the guides/background without a separate call. + +```julia +plot(rand(10); title="TITLE",xlabel="XLABEL",ylabel="YLABEL",background_color=RGB(0.5,0.5,0.5)) +``` + +![](../img/winston/winston_example_5.png) + +### Two-axis + +Use the `axis` or `axiss` arguments. + +Note: Currently only supported with Qwt and PyPlot + +```julia +plot(Vector[randn(100),randn(100) * 100]; axiss=[:left,:right],ylabel="LEFT",yrightlabel="RIGHT") +``` + +![](../img/winston/winston_example_6.png) + +### 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`). + +```julia +plot(Vector[rand(10),rand(20)]; marker=:ellipse,markersize=8,colors=[:red,:blue]) +``` + +![](../img/winston/winston_example_7.png) + +### Build plot in pieces + +Start with a base plot... + +```julia +plot(rand(100) / 3; reg=true,fillto=0) +``` + +![](../img/winston/winston_example_8.png) + +### + +and add to it later. + +```julia +scatter!(rand(100); markersize=6,color=:blue) +``` + +![](../img/winston/winston_example_9.png) + +### Suported line types + +All options: (:line, :orderedline, :step, :stepinverted, :sticks, :scatter, :none, :heatmap, :hexbin, :hist, :bar) + +```julia +types = intersect(supportedTypes(),[:line,:step,:stepinverted,:sticks,:scatter]) +n = length(types) +x = Vector[sort(rand(20)) for i = 1:n] +y = rand(20,n) +plot(x,y; linetypes=types,labels=map(string,types)) +``` + +![](../img/winston/winston_example_11.png) + +### Supported line styles + +All options: (:solid, :dash, :dot, :dashdot, :dashdotdot) + +```julia +styles = setdiff(supportedStyles(),[:auto]) +plot(rand(20,length(styles)); linestyle=:auto,labels=map(string,styles)) +``` + +![](../img/winston/winston_example_12.png) + +### Supported marker types + +All options: (:none, :auto, :ellipse, :rect, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon) + +```julia +markers = setdiff(supportedMarkers(),[:none,:auto]) +plot([fill(i,10) for i = 1:length(markers)]; marker=:auto,labels=map(string,markers),markersize=10) +``` + +![](../img/winston/winston_example_13.png) + +### Bar + +x is the midpoint of the bar. (todo: allow passing of edges instead of midpoints) + +```julia +bar(randn(1000)) +``` + +![](../img/winston/winston_example_14.png) + +### Histogram + +note: fillto isn't supported on all backends + +```julia +histogram(randn(1000); nbins=50,fillto=20) +``` + +![](../img/winston/winston_example_15.png) + diff --git a/img/winston/winston_example_1.png b/img/winston/winston_example_1.png new file mode 100644 index 00000000..c277cc99 Binary files /dev/null and b/img/winston/winston_example_1.png differ diff --git a/img/winston/winston_example_11.png b/img/winston/winston_example_11.png new file mode 100644 index 00000000..add04b33 Binary files /dev/null and b/img/winston/winston_example_11.png differ diff --git a/img/winston/winston_example_12.png b/img/winston/winston_example_12.png new file mode 100644 index 00000000..97f85a3e Binary files /dev/null and b/img/winston/winston_example_12.png differ diff --git a/img/winston/winston_example_13.png b/img/winston/winston_example_13.png new file mode 100644 index 00000000..67ed99d8 Binary files /dev/null and b/img/winston/winston_example_13.png differ diff --git a/img/winston/winston_example_14.png b/img/winston/winston_example_14.png new file mode 100644 index 00000000..476a62da Binary files /dev/null and b/img/winston/winston_example_14.png differ diff --git a/img/winston/winston_example_15.png b/img/winston/winston_example_15.png new file mode 100644 index 00000000..3884cff2 Binary files /dev/null and b/img/winston/winston_example_15.png differ diff --git a/img/winston/winston_example_2.png b/img/winston/winston_example_2.png new file mode 100644 index 00000000..0eda683d Binary files /dev/null and b/img/winston/winston_example_2.png differ diff --git a/img/winston/winston_example_3.png b/img/winston/winston_example_3.png new file mode 100644 index 00000000..76e9a3dc Binary files /dev/null and b/img/winston/winston_example_3.png differ diff --git a/img/winston/winston_example_4.png b/img/winston/winston_example_4.png new file mode 100644 index 00000000..cad6bac9 Binary files /dev/null and b/img/winston/winston_example_4.png differ diff --git a/img/winston/winston_example_5.png b/img/winston/winston_example_5.png new file mode 100644 index 00000000..f72c6ef1 Binary files /dev/null and b/img/winston/winston_example_5.png differ diff --git a/img/winston/winston_example_6.png b/img/winston/winston_example_6.png new file mode 100644 index 00000000..d00c450f Binary files /dev/null and b/img/winston/winston_example_6.png differ diff --git a/img/winston/winston_example_7.png b/img/winston/winston_example_7.png new file mode 100644 index 00000000..ce0a7e7e Binary files /dev/null and b/img/winston/winston_example_7.png differ diff --git a/img/winston/winston_example_8.png b/img/winston/winston_example_8.png new file mode 100644 index 00000000..e9071f42 Binary files /dev/null and b/img/winston/winston_example_8.png differ diff --git a/img/winston/winston_example_9.png b/img/winston/winston_example_9.png new file mode 100644 index 00000000..1e229465 Binary files /dev/null and b/img/winston/winston_example_9.png differ diff --git a/src/backends/winston.jl b/src/backends/winston.jl index db904eb7..289d5d38 100644 --- a/src/backends/winston.jl +++ b/src/backends/winston.jl @@ -32,7 +32,7 @@ const winston_marker = Dict(:none=>".", ) -supportedArgs(::WinstonPackage) = ARGS +supportedArgs(::WinstonPackage) = setdiff(ARGS, [:heatmap_c, :fillto, :pos, :markercolor, :background_color]) supportedAxes(::WinstonPackage) = [:auto, :left] supportedTypes(::WinstonPackage) = [:none, :line, :sticks, :scatter, :hist, :bar] supportedStyles(::WinstonPackage) = unshift!(collect(keys(winston_linestyle)), :auto) # vcat(:auto, keys(winston_linestyle)) @@ -60,14 +60,19 @@ function plot(pkg::WinstonPackage; kw...) # the call to figure does a few things here: # get a new unique id # create a new GtkWindow (or Tk?) - w,h = d[:size] - figidx = Winston.figure(; name = d[:windowtitle], width = w, height = h) - # skip the current fig stuff... just grab the fig directly - fig = Winston._display.figs[figidx] + # w,h = d[:size] + # canvas = Gtk.GtkCanvasLeaf() + # window = Gtk.GtkWindowLeaf(canvas, d[:windowtitle], w, h) + + # figidx = Winston.figure(; name = d[:windowtitle], width = w, height = h) + + # # skip the current fig stuff... just grab the fig directly + # fig = Winston._display.figs[figidx] # overwrite the placeholder FramedPlot with our own - fig.plot = Winston.FramedPlot(title = d[:title], xlabel = d[:xlabel], ylabel = d[:ylabel]) + # fig.plot = Winston.FramedPlot(title = d[:title], xlabel = d[:xlabel], ylabel = d[:ylabel]) + wplt = Winston.FramedPlot(title = d[:title], xlabel = d[:xlabel], ylabel = d[:ylabel]) # # using the figure index returned from Winston.figure, make this plot current and get the # # Figure object (fields: window::GtkWindow and plot::FramedPlot) @@ -81,22 +86,36 @@ function plot(pkg::WinstonPackage; kw...) # Winston.setattr(fig.plot, "ylabel", d[:ylabel]) # Winston.setattr(fig.plot, "title", d[:title]) - Plot((fig, figidx), pkg, 0, d, Dict[]) + Plot(wplt, pkg, 0, d, Dict[]) + # Plot((window, canvas, wplt), pkg, 0, d, Dict[]) + # Plot((fig, figidx), pkg, 0, d, Dict[]) end copy_remove(d::Dict, s::Symbol) = delete!(copy(d), s) -function addRegressionLineWinston(d::Dict) +function addRegressionLineWinston(d::Dict, wplt) xs, ys = regressionXY(d[:x], d[:y]) - Winston.add(plt.o.plot, Winston.Curve(xs, ys, kind="dotted")) + Winston.add(wplt, Winston.Curve(xs, ys, kind="dotted")) +end + +function getWinstonItems(plt::Plot) + if isa(plt.o, Winston.FramedPlot) + wplt = plt.o + window, canvas = nothing, nothing + else + window, canvas, wplt = plt.o + end + window, canvas, wplt end function plot!(::WinstonPackage, plt::Plot; kw...) d = Dict(kw) - # make this figure current - fig, figidx = plt.o - Winston.switchfig(Winston._display, figidx) + # # make this figure current + # fig, figidx = plt.o + # Winston.switchfig(Winston._display, figidx) + + window, canvas, wplt = getWinstonItems(plt) # until we call it normally, do the hack if d[:linetype] == :bar @@ -106,27 +125,24 @@ function plot!(::WinstonPackage, plt::Plot; kw...) e = Dict() e[:color] = d[:color] - # label # string or symbol, applies to that line, may go in a legend e[:linewidth] = d[:width] e[:kind] = winston_linestyle[d[:linestyle]] e[:symbolkind] = winston_marker[d[:marker]] # markercolor # same choices as `color`, or :match will set the color to be the same as `color` - e[:symbolsize] = d[:markersize] / 3 + e[:symbolsize] = d[:markersize] / 5 # fillto # fillto value for area plots - # size # (Int,Int), resize the enclosing window # pos # (Int,Int), move the enclosing window to this position - # windowtitle # string or symbol, set the title of the enclosing windowtitle # screen # Integer, move enclosing window to this screen number (for multiscreen desktops) ## lintype :line, :step, :stepinverted, :sticks, :dots, :none, :heatmap, :hexbin, :hist, :bar if d[:linetype] == :none - Winston.add(fig.plot, Winston.Points(d[:x], d[:y]; copy_remove(e, :kind)...)) + Winston.add(wplt, Winston.Points(d[:x], d[:y]; copy_remove(e, :kind)...)) elseif d[:linetype] == :line - Winston.add(fig.plot, Winston.Curve(d[:x], d[:y]; e...)) + Winston.add(wplt, Winston.Curve(d[:x], d[:y]; e...)) elseif d[:linetype] == :scatter if d[:marker] == :none @@ -140,7 +156,7 @@ function plot!(::WinstonPackage, plt::Plot; kw...) # fn = Winston.XXX elseif d[:linetype] == :sticks - Winston.add(fig.plot, Winston.Stems(d[:x], d[:y]; e...)) + Winston.add(wplt, Winston.Stems(d[:x], d[:y]; e...)) # elseif d[:linetype] == :dots # fn = Winston.XXX @@ -153,7 +169,7 @@ function plot!(::WinstonPackage, plt::Plot; kw...) elseif d[:linetype] == :hist hst = hist(d[:y], d[:nbins]) - Winston.add(fig.plot, Winston.Histogram(hst...; copy_remove(e, :nbins)...)) + Winston.add(wplt, Winston.Histogram(hst...; copy_remove(e, :nbins)...)) # elseif d[:linetype] == :bar # # fn = Winston.XXX @@ -166,28 +182,48 @@ function plot!(::WinstonPackage, plt::Plot; kw...) # marker if d[:marker] != :none - Winston.add(fig.plot, Winston.Points(d[:x], d[:y]; copy_remove(e, :kind)...)) + Winston.add(wplt, Winston.Points(d[:x], d[:y]; copy_remove(e, :kind)...)) end # optionally add a regression line - d[:reg] && d[:linetype] != :hist && addRegressionLineWinston(d) + d[:reg] && d[:linetype] != :hist && addRegressionLineWinston(d, wplt) push!(plt.seriesargs, d) - println("DONE HERE ", figidx) plt end +function addWinstonLegend(plt::Plot, wplt) + Winston.legend(wplt, [sd[:label] for sd in plt.seriesargs]) +end + + function Base.display(::WinstonPackage, plt::Plot) # recreate the legend - fig, figidx = plt.o - println("before legend") - Winston.legend(fig.plot, [sd[:label] for sd in plt.seriesargs]) - println("after legend") + # fig, figidx = plt.o - # display the Figure - display(fig) + window, canvas, wplt = getWinstonItems(plt) + + if window == nothing + # initialize window + w,h = plt.initargs[:size] + canvas = Gtk.GtkCanvasLeaf() + window = Gtk.GtkWindowLeaf(canvas, plt.initargs[:windowtitle], w, h) + # wplt = plt.o + plt.o = (window, canvas, wplt) + # else + # window, canvas, wplt = plt.o + end + + addWinstonLegend(plt, wplt) + + Winston.display(canvas, wplt) + Gtk.showall(window) + + + # # display the Figure + # display(fig) # display(plt.o.window) @@ -199,7 +235,9 @@ end function savepng(::WinstonPackage, plt::PlottingObject, fn::String; kw...) f = open(fn, "w") - writemime(f, "image/png", plt.o.plot) + window, canvas, wplt = getWinstonItems(plt) + addWinstonLegend(plt, wplt) + writemime(f, "image/png", wplt) close(f) end diff --git a/src/plotter.jl b/src/plotter.jl index 86cf6c95..bceca753 100644 --- a/src/plotter.jl +++ b/src/plotter.jl @@ -132,8 +132,8 @@ function plotter() elseif currentBackendSymbol == :winston try - @eval import Winston - @eval export Winston + @eval import Winston, Gtk + @eval export Winston, Gtk catch error("Couldn't import Winston. Install it with: Pkg.add(\"Winston\")") end