From f284857438063caec66b08777cdc7064c38595bb Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Mon, 28 Sep 2015 17:56:32 -0400 Subject: [PATCH] added qwt hline/vline; working on setTicksFromStringVector --- examples/ticks_and_limits.ipynb | 45 +++++++++++++++------------------ src/backends/qwt.jl | 40 ++++++++++++++++++++++++----- src/plot.jl | 28 +++++++++++++++----- 3 files changed, 76 insertions(+), 37 deletions(-) diff --git a/examples/ticks_and_limits.ipynb b/examples/ticks_and_limits.ipynb index e053f39a..e05eeb4b 100644 --- a/examples/ticks_and_limits.ipynb +++ b/examples/ticks_and_limits.ipynb @@ -2,42 +2,39 @@ "cells": [ { "cell_type": "code", - "execution_count": 4, + "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { - "name": "stdout", + "name": "stderr", "output_type": "stream", "text": [ - "[Plots.jl] Switched to backend: gadfly" + "INFO: Recompiling stale cache file /home/tom/.julia/lib/v0.4/Plots.ji for module Plots.\n" ] }, { - "ename": "MethodError", - "evalue": "MethodError: `isless` has no method matching isless(::ASCIIString, ::Int64)\nClosest candidates are:\n isless(!Matched::AbstractFloat, ::Real)\n isless(!Matched::Real, ::Real)\n isless(!Matched::Char, ::Integer)\n ...", + "name": "stdout", + "output_type": "stream", + "text": [ + "[Plots.jl] Default backend: immerse\n", + "[Plots.jl] Switched to backend: gadfly\n", + "[Plots.jl] Initializing backend: gadfly\n", + "v = Union{ASCIIString,UTF8String}[\"x01\",\"x01\",\"x01\",\"x01\",\"x01\",\"x01\",\"x01\",\"x01\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x02\",\"x03\",\"x03\",\"x03\",\"x03\",\"x03\",\"x03\",\"x03\",\"x03\",\"x03\",\"x04\",\"x04\",\"x04\",\"x04\",\"x04\",\"x04\",\"x04\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x05\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x06\",\"x07\",\"x07\",\"x07\",\"x07\",\"x07\",\"x07\",\"x07\",\"x07\",\"x07\",\"x07\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x08\",\"x09\",\"x09\",\"x09\",\"x09\",\"x09\",\"x09\",\"x09\",\"x09\",\"x10\",\"x10\",\"x10\",\"x10\",\"x10\",\"x10\",\"x10\",\"x10\",\"x10\",\"x10\"]" + ] + }, + { + "ename": "LoadError", + "evalue": "LoadError: KeyError: xticks not found\nwhile loading In[1], in expression starting on line 6", "output_type": "error", "traceback": [ - "MethodError: `isless` has no method matching isless(::ASCIIString, ::Int64)\nClosest candidates are:\n isless(!Matched::AbstractFloat, ::Real)\n isless(!Matched::Real, ::Real)\n isless(!Matched::Char, ::Integer)\n ...", + "LoadError: KeyError: xticks not found\nwhile loading In[1], in expression starting on line 6", "", - " in min at operators.jl:58", - " in apply_statistic at /home/tom/.julia/v0.4/Gadfly/src/statistics.jl:818", - " in apply_statistics at /home/tom/.julia/v0.4/Gadfly/src/statistics.jl:38", - " in render_prepare at /home/tom/.julia/v0.4/Gadfly/src/Gadfly.jl:676", - " in render at /home/tom/.julia/v0.4/Gadfly/src/Gadfly.jl:717", - " in draw at /home/tom/.julia/v0.4/Gadfly/src/Gadfly.jl:821", - " in writemime at /home/tom/.julia/v0.4/Plots/src/backends/gadfly.jl:352", - " in base64encode at base64.jl:160", - " in display_dict at /home/tom/.julia/v0.4/IJulia/src/execute_request.jl:32" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "ticks = Union{ASCIIString,UTF8String}[\"x01\",\"x02\",\"x03\",\"x04\",\"x05\",\"x06\",\"x07\",\"x08\",\"x09\",\"x10\"]\n" + " in setTicksFromStringVector at /home/tom/.julia/v0.4/Plots/src/plot.jl:138", + " in plot! at /home/tom/.julia/v0.4/Plots/src/plot.jl:104", + " in plot at /home/tom/.julia/v0.4/Plots/src/plot.jl:58", + " in scatter at /home/tom/.julia/v0.4/Plots/src/Plots.jl:81" ] } ], @@ -47,7 +44,7 @@ "xstr = [@sprintf(\"x%02d\",i) for i in 1:10]\n", "x = sort(sample(xstr, 100))\n", "y = rand(length(x))\n", - "scatter(x,y)" + "scatter(x,y)\n" ] }, { diff --git a/src/backends/qwt.jl b/src/backends/qwt.jl index 7b2212aa..049e4fb1 100644 --- a/src/backends/qwt.jl +++ b/src/backends/qwt.jl @@ -48,7 +48,7 @@ supportedArgs(::QwtPackage) = [ :yrightlabel, # :yticks, ] -supportedTypes(::QwtPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar] +supportedTypes(::QwtPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] supportedMarkers(::QwtPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star1, :star2, :hexagon] # ------------------------------- @@ -67,16 +67,27 @@ function replaceLinetypeAlias(d) end end -function adjustQwtKeywords(iscreating::Bool; kw...) +function adjustQwtKeywords(plt::Plot{QwtPackage}, iscreating::Bool; kw...) d = Dict(kw) - if d[:linetype] == :scatter + lt = d[:linetype] + if lt == :scatter d[:linetype] = :none if d[:marker] == :none d[:marker] = :ellipse end - elseif !iscreating && d[:linetype] == :bar + + elseif lt in (:hline, :vline) + addLineMarker(plt, d) + d[:linetype] = :none + d[:marker] = :ellipse + d[:markersize] = 1 + if lt == :vline + d[:x], d[:y] = d[:y], d[:x] + end + + elseif !iscreating && lt == :bar d = barHack(; kw...) - elseif !iscreating && d[:linetype] == :hist + elseif !iscreating && lt == :hist d = barHack(; histogramHack(; kw...)...) end @@ -92,7 +103,7 @@ function plot(pkg::QwtPackage; kw...) end function plot!(::QwtPackage, plt::Plot; kw...) - d = adjustQwtKeywords(false; kw...) + d = adjustQwtKeywords(plt, false; kw...) Qwt.oplot(plt.o; d...) push!(plt.seriesargs, d) plt @@ -107,6 +118,23 @@ end # ---------------------------------------------------------------- + # curve.setPen(Qt.QPen(Qt.QColor(color), width, self.getLineStyle(linestyle))) +function addLineMarker(plt::Plot{QwtPackage}, d::Dict) + for yi in d[:y] + marker = Qwt.QWT.QwtPlotMarker() + ishorizontal = (d[:linetype] == :hline) + marker[:setLineStyle](ishorizontal ? 1 : 2) + marker[ishorizontal ? :setYValue : :setXValue](yi) + qcolor = Qwt.convertRGBToQColor(d[:color]) + linestyle = plt.o.widget[:getLineStyle](string(d[:linestyle])) + marker[:setLinePen](Qwt.QT.QPen(qcolor, d[:width], linestyle)) + marker[:attach](plt.o.widget) + end + + # marker[:setValue](x, y) + # marker[:setLabel](Qwt.QWT.QwtText(val)) + # marker[:attach](plt.o.widget) +end function createQwtAnnotation(plt::Plot, x, y, val::AbstractString) marker = Qwt.QWT.QwtPlotMarker() diff --git a/src/plot.jl b/src/plot.jl index e5e28979..993d9a61 100644 --- a/src/plot.jl +++ b/src/plot.jl @@ -59,11 +59,6 @@ function plot(args...; kw...) end -# function plot_display(args...; kw...) -# plt = plot(args...; kw...) -# display(plt) -# plt -# end # this adds to the current plot function plot!(args...; kw...) @@ -109,6 +104,8 @@ function plot!(plt::Plot, args...; kw...) setTicksFromStringVector(d, di, :x, :xticks) setTicksFromStringVector(d, di, :y, :yticks) + @show di[:x] di[:y] + # println("Plotting: ", di) plot!(plt.backend, plt; di...) @@ -116,6 +113,8 @@ function plot!(plt::Plot, args...; kw...) addAnnotations(plt, d) + # @show d[:xticks] d[:yticks] + # add title, axis labels, ticks, etc updatePlotItems(plt, d) current(plt) @@ -135,11 +134,26 @@ function setTicksFromStringVector(d::Dict, di::Dict, sym::Symbol, ticksym::Symbo # if the x or y values are strings, set ticks to the unique values, and x/y to the indices of the ticks # @show get(d,ticksym,:auto) == :auto isa(di[sym], AbstractArray) isa(eltype(di[sym]), AbstractString) # @show get(d,ticksym,:auto) eltype(di[sym]) - if get(d,ticksym,:auto) == :auto && isa(di[sym], AbstractArray) && issubtype(eltype(di[sym]), AbstractString) + + haskey(d, ticksym) || return + d[ticksym] == :auto || return + + v = di[sym] + @show v + isa(v, AbstractArray) || return + + T = eltype(v) + @show T + if T <: AbstractString || all(x -> x <: AbstractString, T.types) + + # if get(d,ticksym,:auto) == :auto && isa(di[sym], AbstractArray) && + # (issubtype(eltype(di[sym]), AbstractString) || all(x->x<:AbstractString, eltype(di[sym]).types)) + @show sym ticksym di[sym] d[ticksym] + ticks = unique(di[sym]) @show ticks di[sym] = Int[findnext(ticks, v, 1) for v in di[sym]] - d[ticksym] = ticks + d[ticksym] = UTF8String[t for t in ticks] end # @show sym ticksym di[sym] d[ticksym] end