diff --git a/docs/example_generation.jl b/docs/example_generation.jl index c37ec04d..44795ec3 100644 --- a/docs/example_generation.jl +++ b/docs/example_generation.jl @@ -61,7 +61,7 @@ const examples = PlotExample[ PlotExample("Two-axis", "Use the `axis` arguments.\n\nNote: Currently only supported with Qwt and PyPlot", [ - :(plot(Vector[randn(100), randn(100)*100], axis = [:l :r], ylabel="LEFT", yrightlabel="RIGHT")) + :(plot(Vector[randn(100), randn(100)*100], axis = [:l :r], ylabel="LEFT", yrightlabel="RIGHT", xlabel="X", title="TITLE")) ]), PlotExample("Arguments", "Plot multiple series with different numbers of points. Mix arguments that apply to all series (marker/markersize) with arguments unique to each series (colors). Special arguments `line`, `marker`, and `fill` will automatically figure out what arguments to set (for example, we are setting the `linestyle`, `linewidth`, and `color` arguments with `line`.) Note that we pass a matrix of colors, and this applies the colors to each series.", diff --git a/src/Plots.jl b/src/Plots.jl index 2478c6fb..e45c6529 100644 --- a/src/Plots.jl +++ b/src/Plots.jl @@ -29,6 +29,8 @@ export histogram!, heatmap, heatmap!, + hexbin, + hexbin!, sticks, sticks!, hline, @@ -127,6 +129,8 @@ histogram(args...; kw...) = plot(args...; kw..., linetype = :hist) histogram!(args...; kw...) = plot!(args...; kw..., linetype = :hist) heatmap(args...; kw...) = plot(args...; kw..., linetype = :heatmap) heatmap!(args...; kw...) = plot!(args...; kw..., linetype = :heatmap) +hexbin(args...; kw...) = plot(args...; kw..., linetype = :hexbin) +hexbin!(args...; kw...) = plot!(args...; kw..., linetype = :hexbin) sticks(args...; kw...) = plot(args...; kw..., linetype = :sticks, marker = :ellipse) sticks!(args...; kw...) = plot!(args...; kw..., linetype = :sticks, marker = :ellipse) hline(args...; kw...) = plot(args...; kw..., linetype = :hline) diff --git a/src/backends/gadfly.jl b/src/backends/gadfly.jl index 127c791a..043d5536 100644 --- a/src/backends/gadfly.jl +++ b/src/backends/gadfly.jl @@ -275,6 +275,8 @@ function addGadflySeries!(plt::Plot, d::Dict) lt = d[:linetype] if lt == :ohlc error("Haven't re-implemented after refactoring") + elseif lt in (:heatmap, :hexbin) && (isa(d[:linecolor], ColorGradient) || isa(d[:linecolor], ColorFunction)) + push!(gplt.scales, Gadfly.Scale.ContinuousColorScale(p -> RGB(getColorZ(d[:linecolor], p)))) elseif lt == :scatter && d[:markershape] == :none d[:markershape] = :ellipse end diff --git a/src/backends/pyplot.jl b/src/backends/pyplot.jl index e0e3de46..72c20c00 100644 --- a/src/backends/pyplot.jl +++ b/src/backends/pyplot.jl @@ -75,31 +75,47 @@ function getPyPlotStepStyle(linetype::Symbol) end -immutable PyPlotFigWrapper +# immutable PyPlotFigWrapper +# fig +# kwargs # for add_subplot +# end + +type PyPlotAxisWrapper + ax + rightax fig kwargs # for add_subplot end -immutable PyPlotAxisWrapper - ax - fig -end - -getfig(wrap::@compat(Union{PyPlotAxisWrapper,PyPlotFigWrapper})) = wrap.fig +# getfig(wrap::@compat(Union{PyPlotAxisWrapper,PyPlotFigWrapper})) = wrap.fig +getfig(wrap::PyPlotAxisWrapper) = wrap.fig # get a reference to the correct axis -function getLeftAxis(wrap::PyPlotFigWrapper) - axes = wrap.fig.o[:axes] - if isempty(axes) - return wrap.fig.o[:add_subplot](111; wrap.kwargs...) +function getLeftAxis(wrap::PyPlotAxisWrapper) + if wrap.ax == nothing + axes = wrap.fig.o[:axes] + if isempty(axes) + return wrap.fig.o[:add_subplot](111; wrap.kwargs...) + end + axes[1] + else + wrap.ax end - axes[1] end -getLeftAxis(wrap::PyPlotAxisWrapper) = wrap.ax +# getLeftAxis(wrap::PyPlotAxisWrapper) = wrap.ax +# getRightAxis(x) = getLeftAxis(x)[:twinx]() + +function getRightAxis(wrap::PyPlotAxisWrapper) + if wrap.rightax == nothing + wrap.rightax = getLeftAxis(wrap)[:twinx]() + end + wrap.rightax +end + getLeftAxis(plt::Plot{PyPlotPackage}) = getLeftAxis(plt.o) -getRightAxis(x) = getLeftAxis(x)[:twinx]() +getRightAxis(plt::Plot{PyPlotPackage}) = getRightAxis(plt.o) getAxis(plt::Plot{PyPlotPackage}, axis::Symbol) = (axis == :right ? getRightAxis : getLeftAxis)(plt) # left axis is PyPlot., right axis is "f.axes[0].twinx()." @@ -107,7 +123,7 @@ function getPyPlotFunction(plt::Plot, axis::Symbol, linetype::Symbol) # in the 2-axis case we need to get: [:] ax = getAxis(plt, axis) - ax[:set_ylabel](plt.plotargs[:yrightlabel]) + # ax[:set_ylabel](plt.plotargs[:yrightlabel]) fmap = @compat Dict( :hist => :hist, :sticks => :bar, @@ -150,8 +166,9 @@ handleSmooth(plt::Plot{PyPlotPackage}, ax, d::Dict, smooth::Real) = handleSmooth -makePyPlotCurrent(wrap::PyPlotFigWrapper) = PyPlot.figure(wrap.fig.o[:number]) -makePyPlotCurrent(wrap::PyPlotAxisWrapper) = nothing #PyPlot.sca(wrap.ax.o) +# makePyPlotCurrent(wrap::PyPlotFigWrapper) = PyPlot.figure(wrap.fig.o[:number]) +# makePyPlotCurrent(wrap::PyPlotAxisWrapper) = nothing #PyPlot.sca(wrap.ax.o) +makePyPlotCurrent(wrap::PyPlotAxisWrapper) = wrap.ax == nothing ? PyPlot.figure(wrap.fig.o[:number]) : nothing makePyPlotCurrent(plt::Plot{PyPlotPackage}) = makePyPlotCurrent(plt.o) @@ -180,7 +197,7 @@ function _create_plot(pkg::PyPlotPackage; kw...) else w,h = map(px2inch, d[:size]) bgcolor = getPyPlotColor(d[:background_color]) - wrap = PyPlotFigWrapper(PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = 96), []) + wrap = PyPlotAxisWrapper(nothing, nothing, PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = DPI, tight_layout = true), []) end plt = Plot(wrap, pkg, 0, d, Dict[]) @@ -192,7 +209,7 @@ function _add_series(pkg::PyPlotPackage, plt::Plot; kw...) d = Dict(kw) lt = d[:linetype] - if lt in _3dTypes && isa(plt.o, PyPlotFigWrapper) + if lt in _3dTypes # && isa(plt.o, PyPlotFigWrapper) push!(plt.o.kwargs, (:projection, "3d")) end @@ -524,7 +541,7 @@ function _create_subplot(subplt::Subplot{PyPlotPackage}, isbefore::Bool) w,h = map(px2inch, getplotargs(subplt,1)[:size]) bgcolor = getPyPlotColor(getplotargs(subplt,1)[:background_color]) - fig = PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = 96) + fig = PyPlot.figure(; figsize = (w,h), facecolor = bgcolor, dpi = DPI, tight_layout = true) nr = nrows(l) for (i,(r,c)) in enumerate(l) @@ -534,10 +551,11 @@ function _create_subplot(subplt::Subplot{PyPlotPackage}, isbefore::Bool) fakeidx = (r-1) * nc + c ax = fig[:add_subplot](nr, nc, fakeidx) - subplt.plts[i].o = PyPlotAxisWrapper(ax, fig) + subplt.plts[i].o = PyPlotAxisWrapper(ax, nothing, fig, []) end - subplt.o = PyPlotFigWrapper(fig, []) + # subplt.o = PyPlotFigWrapper(fig, []) + subplt.o = PyPlotAxisWrapper(nothing, nothing, fig, []) true end @@ -590,12 +608,13 @@ function addPyPlotLegend(plt::Plot, ax) # gotta do this to ensure both axes are included args = filter(x -> !(x[:linetype] in (:hist,:hexbin,:heatmap,:hline,:vline,:contour, :path3d, :scatter3d)), plt.seriesargs) if length(args) > 0 - ax[:legend]([d[:serieshandle] for d in args], + leg = ax[:legend]([d[:serieshandle] for d in args], [d[:label] for d in args], loc="best", fontsize = plt.plotargs[:legendfont].pointsize # framealpha = 0.6 ) + leg[:set_zorder](1000) end end end @@ -614,6 +633,7 @@ function finalizePlot(subplt::Subplot{PyPlotPackage}) addPyPlotLegend(plt, ax) updateAxisColors(ax, getPyPlotColor(plt.plotargs[:foreground_color])) end + # fig[:tight_layout]() PyPlot.draw() end @@ -680,10 +700,11 @@ for (mime, fmt) in _pyplot_mimeformats fig = getfig(plt.o) fig.o["canvas"][:print_figure](io, format=$fmt, - # bbox_inches="tight", + # bbox_inches = "tight", + # figsize = map(px2inch, plt.plotargs[:size]), facecolor = fig.o["get_facecolor"](), edgecolor = "none", - dpi = 96 + dpi = DPI ) end end diff --git a/src/utils.jl b/src/utils.jl index 74d77e26..f253318b 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -436,12 +436,14 @@ end # Some conversion functions # note: I borrowed these conversion constants from Compose.jl's Measure -const INCH_SCALAR = 25.4 -const PX_SCALAR = 1 / 3.78 -inch2px(inches::Real) = float(inches * INCH_SCALAR / PX_SCALAR) -px2inch(px::Real) = float(px * PX_SCALAR / INCH_SCALAR) -inch2mm(inches::Real) = float(inches * INCH_SCALAR) -mm2inch(mm::Real) = float(mm / INCH_SCALAR) -px2mm(px::Real) = float(px * PX_SCALAR) -mm2px(mm::Real) = float(px / PX_SCALAR) +const PX_PER_INCH = 100 +const DPI = PX_PER_INCH +const MM_PER_INCH = 25.4 +const MM_PER_PX = MM_PER_INCH / PX_PER_INCH +inch2px(inches::Real) = float(inches * PX_PER_INCH) +px2inch(px::Real) = float(px / PX_PER_INCH) +inch2mm(inches::Real) = float(inches * MM_PER_INCH) +mm2inch(mm::Real) = float(mm / MM_PER_INCH) +px2mm(px::Real) = float(px * MM_PER_PX) +mm2px(mm::Real) = float(px / MM_PER_PX) diff --git a/test/refimg/gadfly/ref6.png b/test/refimg/gadfly/ref6.png index adb8f064..73c729c8 100644 Binary files a/test/refimg/gadfly/ref6.png and b/test/refimg/gadfly/ref6.png differ diff --git a/test/refimg/pyplot/ref1.png b/test/refimg/pyplot/ref1.png index 200e5e2e..81630754 100644 Binary files a/test/refimg/pyplot/ref1.png and b/test/refimg/pyplot/ref1.png differ diff --git a/test/refimg/pyplot/ref10.png b/test/refimg/pyplot/ref10.png index cb603fd7..38b78943 100644 Binary files a/test/refimg/pyplot/ref10.png and b/test/refimg/pyplot/ref10.png differ diff --git a/test/refimg/pyplot/ref11.png b/test/refimg/pyplot/ref11.png index f9b54051..b6027f37 100644 Binary files a/test/refimg/pyplot/ref11.png and b/test/refimg/pyplot/ref11.png differ diff --git a/test/refimg/pyplot/ref12.png b/test/refimg/pyplot/ref12.png index 107ff487..0176a97e 100644 Binary files a/test/refimg/pyplot/ref12.png and b/test/refimg/pyplot/ref12.png differ diff --git a/test/refimg/pyplot/ref13.png b/test/refimg/pyplot/ref13.png index 21517017..4b80aa7f 100644 Binary files a/test/refimg/pyplot/ref13.png and b/test/refimg/pyplot/ref13.png differ diff --git a/test/refimg/pyplot/ref14.png b/test/refimg/pyplot/ref14.png index dc34215d..674b7574 100644 Binary files a/test/refimg/pyplot/ref14.png and b/test/refimg/pyplot/ref14.png differ diff --git a/test/refimg/pyplot/ref15.png b/test/refimg/pyplot/ref15.png index 1da336e5..bd6f8e01 100644 Binary files a/test/refimg/pyplot/ref15.png and b/test/refimg/pyplot/ref15.png differ diff --git a/test/refimg/pyplot/ref16.png b/test/refimg/pyplot/ref16.png index 41883a66..43b7e453 100644 Binary files a/test/refimg/pyplot/ref16.png and b/test/refimg/pyplot/ref16.png differ diff --git a/test/refimg/pyplot/ref17.png b/test/refimg/pyplot/ref17.png index 07623d30..5edcaddb 100644 Binary files a/test/refimg/pyplot/ref17.png and b/test/refimg/pyplot/ref17.png differ diff --git a/test/refimg/pyplot/ref18.png b/test/refimg/pyplot/ref18.png index b25603b7..aeb86a88 100644 Binary files a/test/refimg/pyplot/ref18.png and b/test/refimg/pyplot/ref18.png differ diff --git a/test/refimg/pyplot/ref2.png b/test/refimg/pyplot/ref2.png index 7bdb0f88..229a4238 100644 Binary files a/test/refimg/pyplot/ref2.png and b/test/refimg/pyplot/ref2.png differ diff --git a/test/refimg/pyplot/ref20.png b/test/refimg/pyplot/ref20.png index b3931b0b..861ebd4a 100644 Binary files a/test/refimg/pyplot/ref20.png and b/test/refimg/pyplot/ref20.png differ diff --git a/test/refimg/pyplot/ref21.png b/test/refimg/pyplot/ref21.png index c75c93d7..b7be9cdb 100644 Binary files a/test/refimg/pyplot/ref21.png and b/test/refimg/pyplot/ref21.png differ diff --git a/test/refimg/pyplot/ref3.png b/test/refimg/pyplot/ref3.png index 76bfa86f..07b4f8eb 100644 Binary files a/test/refimg/pyplot/ref3.png and b/test/refimg/pyplot/ref3.png differ diff --git a/test/refimg/pyplot/ref4.png b/test/refimg/pyplot/ref4.png index ea4be4ba..625988f4 100644 Binary files a/test/refimg/pyplot/ref4.png and b/test/refimg/pyplot/ref4.png differ diff --git a/test/refimg/pyplot/ref5.png b/test/refimg/pyplot/ref5.png index 1617096c..b652b762 100644 Binary files a/test/refimg/pyplot/ref5.png and b/test/refimg/pyplot/ref5.png differ diff --git a/test/refimg/pyplot/ref6.png b/test/refimg/pyplot/ref6.png index 1087b3ef..032d66c7 100644 Binary files a/test/refimg/pyplot/ref6.png and b/test/refimg/pyplot/ref6.png differ diff --git a/test/refimg/pyplot/ref7.png b/test/refimg/pyplot/ref7.png index 878f6adc..7c1687ac 100644 Binary files a/test/refimg/pyplot/ref7.png and b/test/refimg/pyplot/ref7.png differ diff --git a/test/refimg/pyplot/ref8.png b/test/refimg/pyplot/ref8.png index bb3c6d5e..c0a184e2 100644 Binary files a/test/refimg/pyplot/ref8.png and b/test/refimg/pyplot/ref8.png differ diff --git a/test/refimg/pyplot/ref9.png b/test/refimg/pyplot/ref9.png index 7a6c985f..22b5162b 100644 Binary files a/test/refimg/pyplot/ref9.png and b/test/refimg/pyplot/ref9.png differ