Added polar plots

This commit is contained in:
Josef Heinen 2016-04-28 16:25:32 +02:00
parent 6c9e818abd
commit e8d77a3c61
2 changed files with 120 additions and 98 deletions

View File

@ -89,6 +89,37 @@ function gr_polyline(x, y)
end end
end end
function gr_polaraxes(rmin, rmax)
GR.savestate()
GR.setlinecolorind(88)
tick = 0.5 * GR.tick(rmin, rmax)
n = round(Int, (rmax - rmin) / tick + 0.5)
for i in 0:n
r = float(i) / n
if i % 2 == 0
GR.setlinecolorind(88)
if i > 0
GR.drawarc(-r, r, -r, r, 0, 359)
end
GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF)
x, y = GR.wctondc(0.05, r)
GR.text(x, y, @sprintf("%g", rmin + i * tick))
else
GR.setlinecolorind(90)
end
end
for alpha in 0:45:315
a = alpha + 90
sinf = sin(a * pi / 180)
cosf = cos(a * pi / 180)
GR.polyline([sinf, 0], [cosf, 0])
GR.settextalign(GR.TEXT_HALIGN_CENTER, GR.TEXT_VALIGN_HALF)
x, y = GR.wctondc(1.1 * sinf, 1.1 * cosf)
GR.textext(x, y, string(alpha, "^o"))
end
GR.restorestate()
end
function gr_display(plt::Plot{GRBackend}, clear=true, update=true, function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
subplot=[0, 1, 0, 1]) subplot=[0, 1, 0, 1])
d = plt.plotargs d = plt.plotargs
@ -151,47 +182,51 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
xmin = ymin = typemax(Float64) xmin = ymin = typemax(Float64)
xmax = ymax = typemin(Float64) xmax = ymax = typemin(Float64)
for p in plt.seriesargs for p in plt.seriesargs
lt = p[:linetype]
if get(d, :polar, false)
lt = :polar
end
if axis == gr_getaxisind(p) if axis == gr_getaxisind(p)
if axis == 2 if axis == 2
num_axes = 2 num_axes = 2
end end
if p[:linetype] == :bar if lt == :bar
x, y = 1:length(p[:y]), p[:y] x, y = 1:length(p[:y]), p[:y]
elseif p[:linetype] == :ohlc elseif lt == :ohlc
x, y = 1:size(p[:y], 1), p[:y] x, y = 1:size(p[:y], 1), p[:y]
elseif p[:linetype] in [:hist, :density] elseif lt in [:hist, :density]
x, y = Base.hist(p[:y]) x, y = Base.hist(p[:y])
elseif p[:linetype] in [:hist2d, :hexbin] elseif lt in [:hist2d, :hexbin]
E = zeros(length(p[:x]),2) E = zeros(length(p[:x]),2)
E[:,1] = p[:x] E[:,1] = p[:x]
E[:,2] = p[:y] E[:,2] = p[:y]
if isa(p[:bins], Tuple) if isa(p[:nbins], Tuple)
xbins, ybins = p[:bins] xbins, ybins = p[:nbins]
else else
xbins = ybins = p[:bins] xbins = ybins = p[:nbins]
end end
cmap = true cmap = true
x, y, H = Base.hist2d(E, xbins, ybins) x, y, H = Base.hist2d(E, xbins, ybins)
elseif p[:linetype] == :pie elseif lt in [:pie, :polar]
axes_2d = false axes_2d = false
xmin, xmax, ymin, ymax = 0, 1, 0, 1 xmin, xmax, ymin, ymax = 0, 1, 0, 1
x, y = p[:x], p[:y] x, y = p[:x], p[:y]
else else
if p[:linetype] in [:contour, :surface, :heatmap] if lt in [:contour, :surface, :heatmap]
cmap = true cmap = true
end end
if p[:linetype] in [:surface, :wireframe, :path3d, :scatter3d] if lt in [:surface, :wireframe, :path3d, :scatter3d]
axes_2d = false axes_2d = false
end end
if p[:linetype] == :heatmap if lt == :heatmap
outside_ticks = true outside_ticks = true
end end
x, y = p[:x], p[:y] x, y = p[:x], p[:y]
end end
if p[:linetype] != :pie if !(lt in [:pie, :polar])
xmin = min(minimum(x), xmin) xmin = min(minimum(x), xmin)
xmax = max(maximum(x), xmax) xmax = max(maximum(x), xmax)
if p[:linetype] == :ohlc if lt == :ohlc
for val in y for val in y
ymin = min(val.open, val.high, val.low, val.close, ymin) ymin = min(val.open, val.high, val.low, val.close, ymin)
ymax = max(val.open, val.high, val.low, val.close, ymax) ymax = max(val.open, val.high, val.low, val.close, ymax)
@ -322,15 +357,19 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
legend = false legend = false
for p in plt.seriesargs for p in plt.seriesargs
lt = p[:linetype]
if get(d, :polar, false)
lt = :polar
end
GR.savestate() GR.savestate()
xmin, xmax, ymin, ymax = extrema[gr_getaxisind(p),:] xmin, xmax, ymin, ymax = extrema[gr_getaxisind(p),:]
GR.setwindow(xmin, xmax, ymin, ymax) GR.setwindow(xmin, xmax, ymin, ymax)
if p[:linetype] in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline, :ohlc] if lt in [:path, :line, :steppre, :steppost, :sticks, :hline, :vline, :ohlc, :polar]
haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]]) haskey(p, :linestyle) && GR.setlinetype(gr_linetype[p[:linestyle]])
haskey(p, :linewidth) && GR.setlinewidth(p[:linewidth]) haskey(p, :linewidth) && GR.setlinewidth(p[:linewidth])
haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor])) haskey(p, :linecolor) && GR.setlinecolorind(gr_getcolorind(p[:linecolor]))
end end
if p[:linetype] == :path if lt == :path
if haskey(p, :fillcolor) if haskey(p, :fillcolor)
GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) GR.setfillcolorind(gr_getcolorind(p[:fillcolor]))
GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillintstyle(GR.INTSTYLE_SOLID)
@ -343,19 +382,19 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
end end
legend = true legend = true
end end
if p[:linetype] == :line if lt == :line
if length(p[:x]) > 1 if length(p[:x]) > 1
gr_polyline(p[:x], p[:y]) gr_polyline(p[:x], p[:y])
end end
legend = true legend = true
elseif p[:linetype] in [:steppre, :steppost] elseif lt in [:steppre, :steppost]
n = length(p[:x]) n = length(p[:x])
x = zeros(2*n + 1) x = zeros(2*n + 1)
y = zeros(2*n + 1) y = zeros(2*n + 1)
x[1], y[1] = p[:x][1], p[:y][1] x[1], y[1] = p[:x][1], p[:y][1]
j = 2 j = 2
for i = 2:n for i = 2:n
if p[:linetype] == :steppre if lt == :steppre
x[j], x[j+1] = p[:x][i-1], p[:x][i] x[j], x[j+1] = p[:x][i-1], p[:x][i]
y[j], y[j+1] = p[:y][i], p[:y][i] y[j], y[j+1] = p[:y][i], p[:y][i]
else else
@ -368,18 +407,18 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.polyline(x, y) GR.polyline(x, y)
end end
legend = true legend = true
elseif p[:linetype] == :sticks elseif lt == :sticks
x, y = p[:x], p[:y] x, y = p[:x], p[:y]
for i = 1:length(y) for i = 1:length(y)
GR.polyline([x[i], x[i]], [ymin, y[i]]) GR.polyline([x[i], x[i]], [ymin, y[i]])
end end
legend = true legend = true
elseif p[:linetype] == :scatter || (p[:markershape] != :none && axes_2d) elseif lt == :scatter || (p[:markershape] != :none && axes_2d)
haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor]))
gr_setmarkershape(p) gr_setmarkershape(p)
if haskey(d, :markersize) if haskey(d, :markersize)
if typeof(d[:markersize]) <: Number if typeof(p[:markersize]) <: Number
GR.setmarkersize(d[:markersize] / 4.0) GR.setmarkersize(p[:markersize] / 4.0)
if length(p[:x]) > 0 if length(p[:x]) > 0
gr_polymarker(p, p[:x], p[:y]) gr_polymarker(p, p[:x], p[:y])
end end
@ -387,11 +426,11 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
c = p[:markercolor] c = p[:markercolor]
GR.setcolormap(-GR.COLORMAP_GLOWING) GR.setcolormap(-GR.COLORMAP_GLOWING)
for i = 1:length(p[:x]) for i = 1:length(p[:x])
if isa(c, ColorGradient) && p[:zcolor] != nothing if isa(c, ColorGradient) && p[:marker_z] != nothing
ci = round(Int, 1000 + p[:zcolor][i] * 255) ci = round(Int, 1000 + p[:marker_z][i] * 255)
GR.setmarkercolorind(ci) GR.setmarkercolorind(ci)
end end
GR.setmarkersize(d[:markersize][i] / 4.0) GR.setmarkersize(p[:markersize][i] / 4.0)
gr_polymarker(p, [p[:x][i]], [p[:y][i]]) gr_polymarker(p, [p[:x][i]], [p[:y][i]])
end end
end end
@ -401,7 +440,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
end end
end end
legend = true legend = true
elseif p[:linetype] == :bar elseif lt == :bar
y = p[:y] y = p[:y]
for i = 1:length(y) for i = 1:length(y)
GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) GR.setfillcolorind(gr_getcolorind(p[:fillcolor]))
@ -411,7 +450,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.setfillintstyle(GR.INTSTYLE_HOLLOW) GR.setfillintstyle(GR.INTSTYLE_HOLLOW)
GR.fillrect(i-0.4, i+0.4, max(0, ymin), y[i]) GR.fillrect(i-0.4, i+0.4, max(0, ymin), y[i])
end end
elseif p[:linetype] in [:hist, :density] elseif lt in [:hist, :density]
h = Base.hist(p[:y]) h = Base.hist(p[:y])
x, y = float(collect(h[1])), float(h[2]) x, y = float(collect(h[1])), float(h[2])
for i = 2:length(y) for i = 2:length(y)
@ -422,22 +461,22 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.setfillintstyle(GR.INTSTYLE_HOLLOW) GR.setfillintstyle(GR.INTSTYLE_HOLLOW)
GR.fillrect(x[i-1], x[i], ymin, y[i]) GR.fillrect(x[i-1], x[i], ymin, y[i])
end end
elseif p[:linetype] in [:hline, :vline] elseif lt in [:hline, :vline]
for xy in p[:y] for xy in p[:y]
if p[:linetype] == :hline if lt == :hline
GR.polyline([xmin, xmax], [xy, xy]) GR.polyline([xmin, xmax], [xy, xy])
else else
GR.polyline([xy, xy], [ymin, ymax]) GR.polyline([xy, xy], [ymin, ymax])
end end
end end
elseif p[:linetype] in [:hist2d, :hexbin] elseif lt in [:hist2d, :hexbin]
E = zeros(length(p[:x]),2) E = zeros(length(p[:x]),2)
E[:,1] = p[:x] E[:,1] = p[:x]
E[:,2] = p[:y] E[:,2] = p[:y]
if isa(p[:bins], Tuple) if isa(p[:nbins], Tuple)
xbins, ybins = p[:bins] xbins, ybins = p[:nbins]
else else
xbins = ybins = p[:bins] xbins = ybins = p[:nbins]
end end
x, y, H = Base.hist2d(E, xbins, ybins) x, y, H = Base.hist2d(E, xbins, ybins)
counts = round(Int32, 1000 + 255 * H / maximum(H)) counts = round(Int32, 1000 + 255 * H / maximum(H))
@ -451,7 +490,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
charheight = max(0.016 * diag, 0.01) charheight = max(0.016 * diag, 0.01)
GR.setcharheight(charheight) GR.setcharheight(charheight)
GR.colormap() GR.colormap()
elseif p[:linetype] == :contour elseif lt == :contour
x, y, z = p[:x], p[:y], p[:z].surf x, y, z = p[:x], p[:y], p[:z].surf
zmin, zmax = minimum(z), maximum(z) zmin, zmax = minimum(z), maximum(z)
if typeof(p[:levels]) <: Array if typeof(p[:levels]) <: Array
@ -472,7 +511,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
charheight = max(0.016 * diag, 0.01) charheight = max(0.016 * diag, 0.01)
GR.setcharheight(charheight) GR.setcharheight(charheight)
GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005) GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005)
elseif p[:linetype] in [:surface, :wireframe] elseif lt in [:surface, :wireframe]
x, y, z = p[:x], p[:y], p[:z].surf x, y, z = p[:x], p[:y], p[:z].surf
zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) zmin, zmax = GR.adjustrange(minimum(z), maximum(z))
GR.setspace(zmin, zmax, 40, 70) GR.setspace(zmin, zmax, 40, 70)
@ -488,7 +527,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0)
end end
z = reshape(z, length(x) * length(y)) z = reshape(z, length(x) * length(y))
if p[:linetype] == :surface if lt == :surface
GR.setcolormap(GR.COLORMAP_COOLWARM) GR.setcolormap(GR.COLORMAP_COOLWARM)
GR.gr3.surface(x, y, z, GR.OPTION_COLORED_MESH) GR.gr3.surface(x, y, z, GR.OPTION_COLORED_MESH)
else else
@ -504,7 +543,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
viewport[3], viewport[4]) viewport[3], viewport[4])
GR.colormap() GR.colormap()
end end
elseif p[:linetype] == :heatmap elseif lt == :heatmap
x, y, z = p[:x], p[:y], p[:z].surf x, y, z = p[:x], p[:y], p[:z].surf
zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) zmin, zmax = GR.adjustrange(minimum(z), maximum(z))
GR.setspace(zmin, zmax, 0, 90) GR.setspace(zmin, zmax, 0, 90)
@ -516,7 +555,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
viewport[3], viewport[4]) viewport[3], viewport[4])
GR.colormap() GR.colormap()
end end
elseif p[:linetype] in [:path3d, :scatter3d] elseif lt in [:path3d, :scatter3d]
x, y, z = p[:x], p[:y], p[:z] x, y, z = p[:x], p[:y], p[:z]
zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) zmin, zmax = GR.adjustrange(minimum(z), maximum(z))
GR.setspace(zmin, zmax, 40, 70) GR.setspace(zmin, zmax, 40, 70)
@ -527,11 +566,11 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
charheight = max(0.018 * diag, 0.01) charheight = max(0.018 * diag, 0.01)
ticksize = 0.01 * (viewport[2] - viewport[1]) ticksize = 0.01 * (viewport[2] - viewport[1])
GR.setlinewidth(1) GR.setlinewidth(1)
if grid_flag && p[:linetype] == :path3d if grid_flag && lt == :path3d
GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2) GR.grid3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2)
GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0) GR.grid3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0)
end end
if p[:linetype] == :scatter3d if lt == :scatter3d
haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor])) haskey(p, :markercolor) && GR.setmarkercolorind(gr_getcolorind(p[:markercolor]))
gr_setmarkershape(p) gr_setmarkershape(p)
for i = 1:length(z) for i = 1:length(z)
@ -548,7 +587,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.setcharheight(charheight) GR.setcharheight(charheight)
GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize) GR.axes3d(xtick, 0, ztick, xmin, ymin, zmin, 2, 0, 2, -ticksize)
GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize) GR.axes3d(0, ytick, 0, xmax, ymin, zmin, 0, 2, 0, ticksize)
elseif p[:linetype] == :ohlc elseif lt == :ohlc
y = p[:y] y = p[:y]
n = size(y, 1) n = size(y, 1)
ticksize = 0.5 * (xmax - xmin) / n ticksize = 0.5 * (xmax - xmin) / n
@ -557,7 +596,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.polyline([i, i], [y[i].low, y[i].high]) GR.polyline([i, i], [y[i].low, y[i].high])
GR.polyline([i, i+ticksize], [y[i].close, y[i].close]) GR.polyline([i, i+ticksize], [y[i].close, y[i].close])
end end
elseif p[:linetype] == :pie elseif lt == :pie
GR.selntran(0) GR.selntran(0)
GR.setfillintstyle(GR.INTSTYLE_SOLID) GR.setfillintstyle(GR.INTSTYLE_SOLID)
xmin, xmax, ymin, ymax = viewport xmin, xmax, ymin, ymax = viewport
@ -602,6 +641,26 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
a1 = a2 a1 = a2
end end
GR.selntran(1) GR.selntran(1)
elseif lt == :polar
xmin, xmax, ymin, ymax = viewport
ymax -= 0.05 * (xmax - xmin)
xcenter = 0.5 * (xmin + xmax)
ycenter = 0.5 * (ymin + ymax)
r = 0.5 * min(xmax - xmin, ymax - ymin)
GR.setviewport(xcenter -r, xcenter + r, ycenter - r, ycenter + r)
GR.setwindow(-1, 1, -1, 1)
rmin, rmax = GR.adjustrange(minimum(r), maximum(r))
gr_polaraxes(rmin, rmax)
phi, r, = p[:x], p[:y]
r = 0.5 * (r - rmin) / (rmax - rmin)
n = length(r)
x = zeros(n)
y = zeros(n)
for i in 1:n
x[i] = r[i] * cos(phi[i])
y[i] = r[i] * sin(phi[i])
end
GR.polyline(x, y)
end end
GR.restorestate() GR.restorestate()
end end

View File

@ -126,68 +126,31 @@ subplotSupported(::PyPlotBackend) = true
supportedArgs(::GRBackend) = [ supportedArgs(::GRBackend) = [
:annotation, :annotation,
:axis, :background_color, :foreground_color, :color_palette,
:background_color,
:linecolor,
:color_palette,
:fillrange,
:fillcolor,
:foreground_color,
:group, :group,
:label, :label,
:layout,
:legend,
:colorbar,
:seriescolor, :seriesalpha,
:linestyle,
:linetype, :linetype,
:linewidth, :linecolor, :linestyle, :linewidth, :linealpha,
:markershape, :markershape, :markercolor, :markersize, :markeralpha,
:markercolor, :markerstrokewidth, :markerstrokecolor, :markerstrokealpha,
:markersize, :fillrange, :fillcolor, :fillalpha,
:markerstrokewidth, :nbins,
:markerstrokecolor, :n, :nc, :nr, :layout,
# :markerstrokestyle,
:n,
:bins,
:nc,
:nr,
# :pos,
:smooth, :smooth,
# :ribbon, :title, :windowtitle, :show, :size,
:show, :x, :xlabel, :xlims, :xticks, :xscale, :xflip,
:size, :y, :ylabel, :ylims, :yticks, :yscale, :yflip,
:title, :axis, :yrightlabel,
:windowtitle, :z, :zlabel, :zlims, :zticks, :zscale, :zflip,
:x,
:xlabel,
:xlims,
:xticks,
:y,
:ylabel,
:ylims,
:yrightlabel,
:yticks,
:xscale,
:yscale,
:xflip,
:yflip,
:z, :z,
:marker_z, # only supported for scatter/scatter3d :tickfont, :guidefont, :legendfont,
:tickfont, :grid, :legend, :colorbar,
:guidefont, :zcolor, :levels,
:legendfont, :xerror, :yerror,
:grid, :ribbon, :quiver,
# :surface,
:nlevels,
:fillalpha,
:linealpha,
:markeralpha,
:xerror,
:yerror,
:ribbon,
:quiver,
:orientation, :orientation,
:overwrite_figure,
:polar,
] ]
supportedAxes(::GRBackend) = _allAxes supportedAxes(::GRBackend) = _allAxes
supportedTypes(::GRBackend) = [:none, :line, :path, :steppre, :steppost, :sticks, supportedTypes(::GRBackend) = [:none, :line, :path, :steppre, :steppost, :sticks,