custom markers example; pyplot markers

This commit is contained in:
Thomas Breloff 2015-10-17 22:16:06 -04:00
parent 73779898b7
commit 5df24d6683
4 changed files with 50 additions and 6 deletions

View File

@ -157,8 +157,21 @@ const examples = PlotExample[
[ [
:(y = rand(10)), :(y = rand(10)),
:(plot(y, ann=(3,y[3],text("this is #3",:left)))), :(plot(y, ann=(3,y[3],text("this is #3",:left)))),
:(annotate!([(5,y[5],text("this is #5",16,:red,:center)),(10,y[10],text("this is #10",:right,20,"courier"))])) :(annotate!([(5,y[5],text("this is #5",16,:red,:center)),
(10,y[10],text("this is #10",:right,20,"courier"))]))
]), ]),
PlotExample("Custom Markers",
"A `Plots.Shape` is a light wrapper around vertices of a polygon. For supported backends, pass arbitrary polygons as the marker shapes. Note: The center is (0,0) and the size is expected to be rougly the area of the unit circle.",
[
:(verts = [(-1.0,1.0),(-1.28,0.6),(-0.2,-1.4),(0.2,-1.4),(1.28,0.6),(1.0,1.0),
(-1.0,1.0),(-0.2,-0.6),(0.0,-0.2),(-0.4,0.6),(1.28,0.6),(0.2,-1.4),
(-0.2,-1.4),(0.6,0.2),(-0.2,0.2),(0.0,-0.2),(0.2,0.2),(-0.2,-0.6)])
:(plot(0.1:0.2:0.9, 0.7rand(5)+0.15,
l=(3,:dash,:lightblue),
m=(Shape(verts),30,RGBA(0,0,0,0)),
bg=:pink, fg=:darkblue,
ylim=(0,1), leg=false))
])
] ]

View File

@ -382,6 +382,9 @@ function processAxisArg(d::Dict, axisletter::@compat(AbstractString), arg)
elseif T <: AVec elseif T <: AVec
d[symbol(axisletter * "ticks")] = arg d[symbol(axisletter * "ticks")] = arg
elseif arg == nothing
d[symbol(axisletter * "ticks")] = []
else else
warn("Skipped $(axisletter)axis arg $arg") warn("Skipped $(axisletter)axis arg $arg")

View File

@ -217,24 +217,26 @@ function getMarkerGeom(d::Dict)
end end
function getGadflyMarkerTheme(d::Dict) function getGadflyMarkerTheme(d::Dict, initargs::Dict)
c = getColor(d[:markercolor]) c = getColor(d[:markercolor])
α = d[:markeropacity] α = d[:markeropacity]
if α != nothing if α != nothing
c = RGBA(RGB(c), α) c = RGBA(RGB(c), α)
end end
fg = getColor(initargs[:foreground_color])
Gadfly.Theme( Gadfly.Theme(
default_color = c, default_color = c,
default_point_size = d[:markersize] * Gadfly.px, default_point_size = d[:markersize] * Gadfly.px,
# highlight_color = getColor(initargs[:foreground_color]), # highlight_color = getColor(initargs[:foreground_color]),
discrete_highlight_color = c -> fg,
highlight_width = d[:linewidth] * Gadfly.px, highlight_width = d[:linewidth] * Gadfly.px,
) )
end end
function addGadflyMarker!(plt::Plot, d::Dict, geoms...) function addGadflyMarker!(plt::Plot, d::Dict, initargs::Dict, geoms...)
gfargs = vcat(geoms..., gfargs = vcat(geoms...,
getGadflyMarkerTheme(d), getGadflyMarkerTheme(d, initargs),
getMarkerGeom(d)) getMarkerGeom(d))
kwargs = Dict() kwargs = Dict()
@ -328,7 +330,7 @@ function addGadflySeries!(plt::Plot, d::Dict)
# markers # markers
if d[:markershape] != :none if d[:markershape] != :none
prepend!(layers, addGadflyMarker!(plt, d, smooth...)) prepend!(layers, addGadflyMarker!(plt, d, plt.initargs, smooth...))
end end
lt in (:hist, :heatmap, :hexbin) || addToGadflyLegend(plt, d) lt in (:hist, :heatmap, :hexbin) || addToGadflyLegend(plt, d)
@ -367,10 +369,20 @@ end
function addGadflyTicksGuide(gplt, ticks, isx::Bool) function addGadflyTicksGuide(gplt, ticks, isx::Bool)
ticks == :auto && return ticks == :auto && return
# remove the ticks?
if ticks in (:none, false, nothing)
return addOrReplace(gplt.guides, isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks; label=false)
end
ttype = ticksType(ticks) ttype = ticksType(ticks)
# just the values... put ticks here, but use standard labels
if ttype == :ticks if ttype == :ticks
gtype = isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks gtype = isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks
replaceType(gplt.guides, gtype(ticks = collect(ticks))) replaceType(gplt.guides, gtype(ticks = collect(ticks)))
# set the ticks and the labels
elseif ttype == :ticks_and_labels elseif ttype == :ticks_and_labels
gtype = isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks gtype = isx ? Gadfly.Guide.xticks : Gadfly.Guide.yticks
replaceType(gplt.guides, gtype(ticks = collect(ticks[1]))) replaceType(gplt.guides, gtype(ticks = collect(ticks[1])))
@ -382,6 +394,7 @@ function addGadflyTicksGuide(gplt, ticks, isx::Bool)
labelmap = Dict(zip(ticks...)) labelmap = Dict(zip(ticks...))
labelfunc = val -> labelmap[val] labelfunc = val -> labelmap[val]
push!(gplt.scales, gfunc(levels = ticks[1], labels = labelfunc)) push!(gplt.scales, gfunc(levels = ticks[1], labels = labelfunc))
else else
error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks) error("Invalid input for $(isx ? "xticks" : "yticks"): ", ticks)
end end

View File

@ -63,7 +63,8 @@ supportedArgs(::PyPlotPackage) = [
supportedAxes(::PyPlotPackage) = _allAxes supportedAxes(::PyPlotPackage) = _allAxes
supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline] supportedTypes(::PyPlotPackage) = [:none, :line, :path, :steppre, :steppost, :sticks, :scatter, :heatmap, :hexbin, :hist, :bar, :hline, :vline]
supportedStyles(::PyPlotPackage) = [:auto, :solid, :dash, :dot, :dashdot] supportedStyles(::PyPlotPackage) = [:auto, :solid, :dash, :dot, :dashdot]
supportedMarkers(::PyPlotPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :hexagon] # supportedMarkers(::PyPlotPackage) = [:none, :auto, :rect, :ellipse, :diamond, :utriangle, :dtriangle, :cross, :xcross, :star5, :hexagon]
supportedMarkers(::GadflyPackage) = vcat(_allMarkers, Shape)
supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10] supportedScales(::PyPlotPackage) = [:identity, :log, :log2, :log10]
subplotSupported(::PyPlotPackage) = true subplotSupported(::PyPlotPackage) = true
@ -87,6 +88,16 @@ function getPyPlotLineStyle(linetype::Symbol, linestyle::Symbol)
return "-" return "-"
end end
# function getMarkerGeom(d::Dict)
# shape = d[:markershape]
# gadflyshape(isa(shape, Shape) ? shape : _shapes[shape])
# end
function getPyPlotMarker(marker::Shape)
marker.vertices
end
# get the marker shape # get the marker shape
function getPyPlotMarker(marker::Symbol) function getPyPlotMarker(marker::Symbol)
marker == :none && return " " marker == :none && return " "
@ -98,7 +109,11 @@ function getPyPlotMarker(marker::Symbol)
marker == :cross && return "+" marker == :cross && return "+"
marker == :xcross && return "x" marker == :xcross && return "x"
marker == :star5 && return "*" marker == :star5 && return "*"
marker == :pentagon && return "p"
marker == :hexagon && return "h" marker == :hexagon && return "h"
marker == :octagon && return "8"
haskey(_shapes, marker) && return _shapes[marker].vertices
warn("Unknown marker $marker") warn("Unknown marker $marker")
return "o" return "o"
end end