added bar_position; fixed orientation handling; pyplot cleanup

This commit is contained in:
Thomas Breloff 2016-04-18 15:12:13 -04:00
parent 236a7dadc6
commit a2ea9db90e
4 changed files with 333 additions and 383 deletions

View File

@ -147,6 +147,7 @@ _seriesDefaults[:zcolor] = nothing # value for color scale
# _seriesDefaults[:nlevels] = 15 # _seriesDefaults[:nlevels] = 15
_seriesDefaults[:levels] = 15 _seriesDefaults[:levels] = 15
_seriesDefaults[:orientation] = :vertical _seriesDefaults[:orientation] = :vertical
_seriesDefaults[:bar_position] = :overlay # for bar plots and histograms: could also be stack (stack up) or dodge (side by side)
_seriesDefaults[:xerror] = nothing _seriesDefaults[:xerror] = nothing
_seriesDefaults[:yerror] = nothing _seriesDefaults[:yerror] = nothing
_seriesDefaults[:ribbon] = nothing _seriesDefaults[:ribbon] = nothing

View File

@ -39,7 +39,9 @@ function getLineGeom(d::KW)
elseif lt == :hist2d elseif lt == :hist2d
Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins) Gadfly.Geom.histogram2d(xbincount = xbins, ybincount = ybins)
elseif lt == :hist elseif lt == :hist
Gadfly.Geom.histogram(bincount = xbins) Gadfly.Geom.histogram(bincount = xbins,
orientation = isvertical(d) ? :vertical : :horizontal,
position = d[:bar_position] == :stack ? :stack : :dodge)
elseif lt == :path elseif lt == :path
Gadfly.Geom.path Gadfly.Geom.path
elseif lt in (:bar, :sticks) elseif lt in (:bar, :sticks)

View File

@ -168,7 +168,6 @@ getAxis(plt::Plot{PyPlotBackend}, axis::Symbol) = (axis == :right ? getRightAxis
# left axis is PyPlot.<func>, right axis is "f.axes[0].twinx().<func>" # left axis is PyPlot.<func>, right axis is "f.axes[0].twinx().<func>"
function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol) function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol)
# # need to access mplot3d functions differently # # need to access mplot3d functions differently
# if linetype == :surface # if linetype == :surface
# return mplot3d.pymember("Axes3D")[:plot_surface] # return mplot3d.pymember("Axes3D")[:plot_surface]
@ -296,8 +295,8 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
# handle mismatched x/y sizes, as PyPlot doesn't like that # handle mismatched x/y sizes, as PyPlot doesn't like that
x, y = d[:x], d[:y] x, y = d[:x], d[:y]
if !isa(get(d, :z, nothing), Surface) nx, ny = length(x), length(y)
nx, ny = map(length, (x,y)) if !isa(get(d, :z, nothing), Surface) && nx != ny
if nx < ny if nx < ny
d[:x] = Float64[x[mod1(i,nx)] for i=1:ny] d[:x] = Float64[x[mod1(i,nx)] for i=1:ny]
else else
@ -310,8 +309,13 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
error("linetype $(lt) is unsupported in PyPlot. Choose from: $(supportedTypes(pkg))") error("linetype $(lt) is unsupported in PyPlot. Choose from: $(supportedTypes(pkg))")
end end
color = getPyPlotColor(d[:linecolor], d[:linealpha]) linecolor = getPyPlotColor(d[:linecolor], d[:linealpha])
markercolor = getPyPlotColor(d[:markercolor], d[:markeralpha])
fillcolor = getPyPlotColor(d[:fillcolor], d[:fillalpha])
strokecolor = getPyPlotColor(d[:markerstrokecolor], d[:markerstrokealpha])
linecmap = getPyPlotColorMap(d[:linecolor], d[:linealpha])
fillcmap = getPyPlotColorMap(d[:fillcolor], d[:fillalpha])
linestyle = getPyPlotLineStyle(lt, d[:linestyle])
if lt == :sticks if lt == :sticks
d,_ = sticksHack(;d...) d,_ = sticksHack(;d...)
@ -322,9 +326,6 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
end end
elseif lt in (:hline,:vline) elseif lt in (:hline,:vline)
linewidth = d[:linewidth]
linecolor = color
linestyle = getPyPlotLineStyle(lt, d[:linestyle])
for yi in d[:y] for yi in d[:y]
func = ax[lt == :hline ? :axhline : :axvline] func = ax[lt == :hline ? :axhline : :axvline]
func(yi, linewidth=d[:linewidth], color=linecolor, linestyle=linestyle) func(yi, linewidth=d[:linewidth], color=linecolor, linestyle=linestyle)
@ -332,9 +333,7 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
end end
# lt = d[:linetype]
extra_kwargs = KW() extra_kwargs = KW()
plotfunc = getPyPlotFunction(plt, d[:axis], lt) plotfunc = getPyPlotFunction(plt, d[:axis], lt)
# we have different args depending on plot type # we have different args depending on plot type
@ -346,41 +345,43 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
if like_histogram(lt) if like_histogram(lt)
extra_kwargs[:bins] = d[:nbins] extra_kwargs[:bins] = d[:nbins]
extra_kwargs[:normed] = lt == :density extra_kwargs[:normed] = lt == :density
extra_kwargs[:orientation] = isvertical(d) ? "vertical" : "horizontal"
extra_kwargs[:histtype] = d[:bar_position] == :stack ? "barstacked" : "bar"
else else
extra_kwargs[:linewidth] = (lt == :sticks ? 0.1 : 0.9) extra_kwargs[:linewidth] = (lt == :sticks ? 0.1 : 0.9)
end end
elseif lt in (:hist2d, :hexbin) elseif lt in (:hist2d, :hexbin)
extra_kwargs[:gridsize] = d[:nbins] extra_kwargs[:gridsize] = d[:nbins]
extra_kwargs[:cmap] = getPyPlotColorMap(d[:linecolor]) extra_kwargs[:cmap] = linecmap
elseif lt == :contour elseif lt == :contour
extra_kwargs[:cmap] = getPyPlotColorMap(d[:linecolor]) extra_kwargs[:cmap] = linecmap
extra_kwargs[:linewidths] = d[:linewidth] extra_kwargs[:linewidths] = d[:linewidth]
extra_kwargs[:linestyles] = getPyPlotLineStyle(lt, d[:linestyle]) extra_kwargs[:linestyles] = linestyle
# TODO: will need to call contourf to fill in the contours # TODO: will need to call contourf to fill in the contours
elseif lt in (:surface, :wireframe) elseif lt in (:surface, :wireframe)
if lt == :surface if lt == :surface
extra_kwargs[:cmap] = getPyPlotColorMap(d[:fillcolor], d[:fillalpha]) extra_kwargs[:cmap] = fillcmap
end end
extra_kwargs[:rstride] = 1 extra_kwargs[:rstride] = 1
extra_kwargs[:cstride] = 1 extra_kwargs[:cstride] = 1
extra_kwargs[:linewidth] = d[:linewidth] extra_kwargs[:linewidth] = d[:linewidth]
extra_kwargs[:edgecolor] = getPyPlotColor(d[:linecolor], d[:linealpha]) extra_kwargs[:edgecolor] = linecolor
elseif lt == :heatmap elseif lt == :heatmap
extra_kwargs[:cmap] = getPyPlotColorMap(d[:fillcolor], d[:fillalpha]) extra_kwargs[:cmap] = fillcmap
elseif lt == :shape elseif lt == :shape
extra_kwargs[:edgecolor] = getPyPlotColor(d[:markerstrokecolor], d[:markerstrokealpha]) extra_kwargs[:edgecolor] = strokecolor
extra_kwargs[:facecolor] = getPyPlotColor(d[:markercolor], d[:markeralpha]) extra_kwargs[:facecolor] = markercolor
extra_kwargs[:linewidth] = d[:markerstrokewidth] extra_kwargs[:linewidth] = d[:markerstrokewidth]
extra_kwargs[:fill] = true extra_kwargs[:fill] = true
else else
extra_kwargs[:linestyle] = getPyPlotLineStyle(lt, d[:linestyle]) extra_kwargs[:linestyle] = linestyle
extra_kwargs[:marker] = getPyPlotMarker(d[:markershape]) extra_kwargs[:marker] = getPyPlotMarker(d[:markershape])
if lt in (:scatter, :scatter3d) if lt in (:scatter, :scatter3d)
@ -397,19 +398,18 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
# total hack due to PyPlot bug (see issue #145). # total hack due to PyPlot bug (see issue #145).
# hack: duplicate the color vector when the total rgba fields is the same as the series length # hack: duplicate the color vector when the total rgba fields is the same as the series length
if (typeof(ppc) <: AbstractArray && length(ppc)*4 == length(x)) || if (typeof(ppc) <: AbstractArray && length(ppc)*4 == length(x)) || (typeof(ppc) <: Tuple && length(x) == 4)
(typeof(ppc) <: Tuple && length(x) == 4)
ppc = vcat(ppc, ppc) ppc = vcat(ppc, ppc)
end end
extra_kwargs[:c] = ppc extra_kwargs[:c] = ppc
end end
extra_kwargs[:edgecolors] = getPyPlotColor(d[:markerstrokecolor], d[:markerstrokealpha]) extra_kwargs[:edgecolors] = strokecolor
extra_kwargs[:linewidths] = d[:markerstrokewidth] extra_kwargs[:linewidths] = d[:markerstrokewidth]
else else
extra_kwargs[:markersize] = d[:markersize] extra_kwargs[:markersize] = d[:markersize]
extra_kwargs[:markerfacecolor] = getPyPlotColor(d[:markercolor], d[:markeralpha]) extra_kwargs[:markerfacecolor] = markercolor
extra_kwargs[:markeredgecolor] = getPyPlotColor(d[:markerstrokecolor], d[:markerstrokealpha]) extra_kwargs[:markeredgecolor] = strokecolor
extra_kwargs[:markeredgewidth] = d[:markerstrokewidth] extra_kwargs[:markeredgewidth] = d[:markerstrokewidth]
extra_kwargs[:drawstyle] = getPyPlotStepStyle(lt) extra_kwargs[:drawstyle] = getPyPlotStepStyle(lt)
end end
@ -418,7 +418,7 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
# set these for all types # set these for all types
if !(lt in (:contour,:surface,:wireframe,:heatmap)) if !(lt in (:contour,:surface,:wireframe,:heatmap))
if !(lt in (:scatter, :scatter3d, :shape)) if !(lt in (:scatter, :scatter3d, :shape))
extra_kwargs[:color] = color extra_kwargs[:color] = linecolor
extra_kwargs[:linewidth] = d[:linewidth] extra_kwargs[:linewidth] = d[:linewidth]
end end
extra_kwargs[:label] = d[:label] extra_kwargs[:label] = d[:label]
@ -427,6 +427,8 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
# do the plot # do the plot
d[:serieshandle] = if like_histogram(lt) d[:serieshandle] = if like_histogram(lt)
extra_kwargs[:color] = fillcolor
extra_kwargs[:edgecolor] = linecolor
plotfunc(d[:y]; extra_kwargs...)[1] plotfunc(d[:y]; extra_kwargs...)[1]
elseif lt == :contour elseif lt == :contour
@ -443,7 +445,7 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
end end
handle = plotfunc(x, y, surf, extra_args...; extra_kwargs...) handle = plotfunc(x, y, surf, extra_args...; extra_kwargs...)
if d[:fillrange] != nothing if d[:fillrange] != nothing
extra_kwargs[:cmap] = getPyPlotColorMap(d[:fillcolor], d[:fillalpha]) extra_kwargs[:cmap] = fillcmap
delete!(extra_kwargs, :linewidths) delete!(extra_kwargs, :linewidths)
handle = ax[:contourf](x, y, surf, extra_args...; extra_kwargs...) handle = ax[:contourf](x, y, surf, extra_args...; extra_kwargs...)
end end
@ -485,14 +487,11 @@ function _add_series(pkg::PyPlotBackend, plt::Plot; kw...)
PyPlot.colorbar(d[:serieshandle], ax=ax) PyPlot.colorbar(d[:serieshandle], ax=ax)
end end
# @show extra_kwargs
# this sets the bg color inside the grid # this sets the bg color inside the grid
ax[:set_axis_bgcolor](getPyPlotColor(plt.plotargs[:background_color])) ax[:set_axis_bgcolor](getPyPlotColor(plt.plotargs[:background_color]))
fillrange = d[:fillrange] fillrange = d[:fillrange]
if fillrange != nothing && lt != :contour if fillrange != nothing && lt != :contour
fillcolor = getPyPlotColor(d[:fillcolor], d[:fillalpha])
if typeof(fillrange) <: @compat(Union{Real, AVec}) if typeof(fillrange) <: @compat(Union{Real, AVec})
ax[:fill_between](d[:x], fillrange, d[:y], facecolor = fillcolor, zorder = plt.n) ax[:fill_between](d[:x], fillrange, d[:y], facecolor = fillcolor, zorder = plt.n)
else else
@ -507,12 +506,12 @@ end
# ----------------------------------------------------------------- # -----------------------------------------------------------------
# given a dimension (:x, :y, or :z), loop over the seriesargs KWs to find the min/max of the underlying data
function minmaxseries(ds, vec, axis) function minmaxseries(ds, dimension, axis)
lo, hi = Inf, -Inf lo, hi = Inf, -Inf
for d in ds for d in ds
d[:axis] == axis || continue d[:axis] == axis || continue
v = d[vec] v = d[dimension]
if length(v) > 0 if length(v) > 0
vlo, vhi = extrema(v) vlo, vhi = extrema(v)
lo = min(lo, vlo) lo = min(lo, vlo)
@ -545,16 +544,6 @@ end
# -------------------------------------------------------------------------- # --------------------------------------------------------------------------
# function getxy(plt::Plot{PyPlotBackend}, i::Integer)
# series = plt.seriesargs[i][:serieshandle]
# try
# return series[:get_data]()
# catch
# xy = series[:get_offsets]()
# return vec(xy[:,1]), vec(xy[:,2])
# end
# end
function setxy!{X,Y}(plt::Plot{PyPlotBackend}, xy::Tuple{X,Y}, i::Integer) function setxy!{X,Y}(plt::Plot{PyPlotBackend}, xy::Tuple{X,Y}, i::Integer)
d = plt.seriesargs[i] d = plt.seriesargs[i]
d[:x], d[:y] = xy d[:x], d[:y] = xy
@ -571,18 +560,11 @@ end
function setxyz!{X,Y,Z}(plt::Plot{PyPlotBackend}, xyz::Tuple{X,Y,Z}, i::Integer) function setxyz!{X,Y,Z}(plt::Plot{PyPlotBackend}, xyz::Tuple{X,Y,Z}, i::Integer)
d = plt.seriesargs[i] d = plt.seriesargs[i]
# @show typeof(d), typeof(xyz)
d[:x], d[:y], d[:z] = xyz d[:x], d[:y], d[:z] = xyz
series = d[:serieshandle] series = d[:serieshandle]
# @show keys(series)
# try
series[:set_data](d[:x], d[:y]) series[:set_data](d[:x], d[:y])
series[:set_3d_properties](d[:z]) series[:set_3d_properties](d[:z])
# catch
# series[:set_offsets](hcat(d[:x], d[:y], d[:z]))
# end
set_lims!(plt, d[:axis]) set_lims!(plt, d[:axis])
# dumpdict(d, "H",true)
plt plt
end end
@ -731,19 +713,13 @@ end
# ----------------------------------------------------------------- # -----------------------------------------------------------------
# NOTE: pyplot needs to build before
function _create_subplot(subplt::Subplot{PyPlotBackend}, isbefore::Bool) function _create_subplot(subplt::Subplot{PyPlotBackend}, isbefore::Bool)
l = subplt.layout l = subplt.layout
# w,h = map(px2inch, getplotargs(subplt,1)[:size])
# bgcolor = getPyPlotColor(getplotargs(subplt,1)[:background_color])
# fig = PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = DPI, tight_layout = true)
plotargs = getplotargs(subplt, 1) plotargs = getplotargs(subplt, 1)
fig = pyplot_figure(plotargs) fig = pyplot_figure(plotargs)
nr = nrows(l) nr = nrows(l)
for (i,(r,c)) in enumerate(l) for (i,(r,c)) in enumerate(l)
# add the plot to the figure # add the plot to the figure
nc = ncols(l, r) nc = ncols(l, r)
fakeidx = (r-1) * nc + c fakeidx = (r-1) * nc + c
@ -753,7 +729,6 @@ function _create_subplot(subplt::Subplot{PyPlotBackend}, isbefore::Bool)
pyplot_3d_setup!(subplt.plts[i].o, plotargs) pyplot_3d_setup!(subplt.plts[i].o, plotargs)
end end
# subplt.o = PyPlotFigWrapper(fig, [])
subplt.o = PyPlotAxisWrapper(nothing, nothing, fig, []) subplt.o = PyPlotAxisWrapper(nothing, nothing, fig, [])
pyplot_3d_setup!(subplt.o, plotargs) pyplot_3d_setup!(subplt.o, plotargs)
true true
@ -777,7 +752,6 @@ function subplot(plts::AVec{Plot{PyPlotBackend}}, layout::SubplotLayout, d::KW)
for (i,plt) in enumerate(plts) for (i,plt) in enumerate(plts)
for seriesargs in plt.seriesargs for seriesargs in plt.seriesargs
# _add_series_subplot(newplts[i]; seriesargs...)
_add_series_subplot(newplts[i], seriesargs) _add_series_subplot(newplts[i], seriesargs)
end end
end end
@ -829,6 +803,8 @@ function addPyPlotLegend(plt::Plot, ax)
end end
end end
# -----------------------------------------------------------------
function finalizePlot(plt::Plot{PyPlotBackend}) function finalizePlot(plt::Plot{PyPlotBackend})
ax = getLeftAxis(plt) ax = getLeftAxis(plt)
addPyPlotLegend(plt, ax) addPyPlotLegend(plt, ax)
@ -847,19 +823,8 @@ function finalizePlot(subplt::Subplot{PyPlotBackend})
PyPlot.draw() PyPlot.draw()
end end
# # allow for writing any supported mime
# for mime in keys(PyPlot.aggformats)
# @eval function Base.writemime(io::IO, m::MIME{symbol{$mime}}, plt::Plot{PyPlotBackend})
# finalizePlot(plt)
# writemime(io, m, getfig(plt.o))
# end
# end
# function Base.writemime(io::IO, m::@compat(Union{MIME"image/svg+xml", MIME"image/png"}, plt::Plot{PyPlotBackend})
# finalizePlot(plt)
# writemime(io, m, getfig(plt.o))
# end
# -----------------------------------------------------------------
# NOTE: to bring up a GUI window in IJulia, need some extra steps # NOTE: to bring up a GUI window in IJulia, need some extra steps
function Base.display(::PlotsDisplay, plt::AbstractPlot{PyPlotBackend}) function Base.display(::PlotsDisplay, plt::AbstractPlot{PyPlotBackend})
@ -877,23 +842,6 @@ function Base.display(::PlotsDisplay, plt::AbstractPlot{PyPlotBackend})
end end
# function Base.display(::PlotsDisplay, subplt::Subplot{PyPlotBackend})
# finalizePlot(subplt)
# PyPlot.ion()
# PyPlot.figure(getfig(subplt.o).o[:number])
# PyPlot.draw_if_interactive()
# PyPlot.ioff()
# # display(getfig(subplt.o))
# end
# # allow for writing any supported mime
# for mime in (MIME"image/png", MIME"application/pdf", MIME"application/postscript")
# @eval function Base.writemime(io::IO, ::$mime, plt::AbstractPlot{PyPlotBackend})
# finalizePlot(plt)
# writemime(io, $mime(), getfig(plt.o))
# end
# end
const _pyplot_mimeformats = Dict( const _pyplot_mimeformats = Dict(
"application/eps" => "eps", "application/eps" => "eps",
"image/eps" => "eps", "image/eps" => "eps",
@ -901,14 +849,15 @@ const _pyplot_mimeformats = Dict(
"image/png" => "png", "image/png" => "png",
"application/postscript" => "ps", "application/postscript" => "ps",
"image/svg+xml" => "svg" "image/svg+xml" => "svg"
) )
for (mime, fmt) in _pyplot_mimeformats for (mime, fmt) in _pyplot_mimeformats
@eval function Base.writemime(io::IO, ::MIME{symbol($mime)}, plt::AbstractPlot{PyPlotBackend}) @eval function Base.writemime(io::IO, ::MIME{symbol($mime)}, plt::AbstractPlot{PyPlotBackend})
finalizePlot(plt) finalizePlot(plt)
fig = getfig(plt.o) fig = getfig(plt.o)
fig.o["canvas"][:print_figure](io, fig.o["canvas"][:print_figure](
io,
format=$fmt, format=$fmt,
# bbox_inches = "tight", # bbox_inches = "tight",
# figsize = map(px2inch, plt.plotargs[:size]), # figsize = map(px2inch, plt.plotargs[:size]),
@ -918,9 +867,3 @@ for (mime, fmt) in _pyplot_mimeformats
) )
end end
end end
# function Base.writemime(io::IO, m::MIME"image/png", subplt::Subplot{PyPlotBackend})
# finalizePlot(subplt)
# writemime(io, m, getfig(subplt.o))
# end

View File

@ -210,6 +210,10 @@ end
# 1 argument # 1 argument
# -------------------------------------------------------------------- # --------------------------------------------------------------------
function process_inputs(plt::AbstractPlot, d::KW, n::Integer)
d[:x], d[:y], d[:z] = zeros(0), zeros(0), zeros(0)
end
# no special handling... assume x and z are nothing # no special handling... assume x and z are nothing
function process_inputs(plt::AbstractPlot, d::KW, y) function process_inputs(plt::AbstractPlot, d::KW, y)
d[:y] = y d[:y] = y