Merge pull request #233 from jheinen/dev

gr: support for colormaps
This commit is contained in:
Tom Breloff 2016-05-08 10:30:40 -04:00
commit 7bce4a963e

View File

@ -135,6 +135,7 @@ end
function gr_polaraxes(rmin, rmax) function gr_polaraxes(rmin, rmax)
GR.savestate() GR.savestate()
GR.setlinetype(GR.LINETYPE_SOLID)
GR.setlinecolorind(88) GR.setlinecolorind(88)
tick = 0.5 * GR.tick(rmin, rmax) tick = 0.5 * GR.tick(rmin, rmax)
n = round(Int, (rmax - rmin) / tick + 0.5) n = round(Int, (rmax - rmin) / tick + 0.5)
@ -147,9 +148,10 @@ function gr_polaraxes(rmin, rmax)
end end
GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF) GR.settextalign(GR.TEXT_HALIGN_LEFT, GR.TEXT_VALIGN_HALF)
x, y = GR.wctondc(0.05, r) x, y = GR.wctondc(0.05, r)
GR.text(x, y, @sprintf("%g", rmin + i * tick)) GR.text(x, y, string(signif(rmin + i * tick, 12)))
else else
GR.setlinecolorind(90) GR.setlinecolorind(90)
GR.drawarc(-r, r, -r, r, 0, 359)
end end
end end
for alpha in 0:45:315 for alpha in 0:45:315
@ -164,6 +166,25 @@ function gr_polaraxes(rmin, rmax)
GR.restorestate() GR.restorestate()
end end
function gr_getzlims(d, zmin, zmax, adjust)
if haskey(d, :zlims)
if d[:zlims] != :auto
zlims = d[:zlims]
if zlims[1] != NaN
zmin = zlims[1]
end
if zlims[2] != NaN
zmax = zlims[2]
end
adjust = false
end
end
if adjust
zmin, zmax = GR.adjustrange(zmin, zmax)
end
zmin, zmax
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
@ -239,7 +260,11 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
elseif lt == :ohlc elseif lt == :ohlc
x, y = 1:size(p[:y], 1), p[:y] x, y = 1:size(p[:y], 1), p[:y]
elseif lt in [:hist, :density] elseif lt in [:hist, :density]
x, y = Base.hist(p[:y]) if haskey(p, :bins)
x, y = Base.hist(p[:y], p[:bins])
else
x, y = Base.hist(p[:y])
end
elseif lt 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]
@ -402,6 +427,17 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
for p in plt.seriesargs for p in plt.seriesargs
lt = p[:linetype] lt = p[:linetype]
if lt in (:hist2d, :hexbin, :contour, :surface, :wireframe, :heatmap)
if haskey(d, :color_palette)
ci = 1000
for cv in d[:color_palette]
GR.setcolorrep(ci, cv.r, cv.g, cv.b)
ci += 1
end
else
GR.setcolormap(GR.COLORMAP_COOLWARM)
end
end
if get(d, :polar, false) if get(d, :polar, false)
lt = :polar lt = :polar
end end
@ -495,7 +531,11 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
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 lt in [:hist, :density] elseif lt in [:hist, :density]
h = Base.hist(p[:y]) if haskey(p, :bins)
h = Base.hist(p[:y], p[:bins])
else
h = Base.hist(p[:y])
end
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)
GR.setfillcolorind(gr_getcolorind(p[:fillcolor])) GR.setfillcolorind(gr_getcolorind(p[:fillcolor]))
@ -525,25 +565,24 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
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))
n, m = size(counts) n, m = size(counts)
GR.setcolormap(GR.COLORMAP_COOLWARM)
GR.cellarray(xmin, xmax, ymin, ymax, n, m, counts) GR.cellarray(xmin, xmax, ymin, ymax, n, m, counts)
GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05, GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05,
viewport[3], viewport[4]) viewport[3], viewport[4])
GR.setspace(0, maximum(counts), 0, 90) zmin, zmax = gr_getzlims(d, 0, maximum(counts), false)
GR.setspace(zmin, zmax, 0, 90)
diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2) diag = sqrt((viewport[2] - viewport[1])^2 + (viewport[4] - viewport[3])^2)
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 lt == :contour elseif lt == :contour
x, y, z = p[:x], p[:y], transpose_z(p, p[:z].surf, false) x, y, z = p[:x], p[:y], transpose_z(p, p[:z].surf, false)
zmin, zmax = minimum(z), maximum(z) zmin, zmax = gr_getzlims(d, minimum(z), maximum(z), false)
GR.setspace(zmin, zmax, 0, 90)
if typeof(p[:levels]) <: Array if typeof(p[:levels]) <: Array
h = p[:levels] h = p[:levels]
else else
h = linspace(zmin, zmax, p[:levels]) h = linspace(zmin, zmax, p[:levels])
end end
GR.setspace(zmin, zmax, 0, 90)
GR.setcolormap(GR.COLORMAP_COOLWARM)
GR.contour(x, y, h, reshape(z, length(x) * length(y)), 1000) GR.contour(x, y, h, reshape(z, length(x) * length(y)), 1000)
GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05, GR.setviewport(viewport[2] + 0.02, viewport[2] + 0.05,
viewport[3], viewport[4]) viewport[3], viewport[4])
@ -557,7 +596,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005) GR.axes(0, ztick, xmax, zmin, 0, 1, 0.005)
elseif lt in [:surface, :wireframe] elseif lt in [:surface, :wireframe]
x, y, z = p[:x], p[:y], transpose_z(p, p[:z].surf, false) x, y, z = p[:x], p[:y], transpose_z(p, p[:z].surf, false)
zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) zmin, zmax = gr_getzlims(d, minimum(z), maximum(z), true)
GR.setspace(zmin, zmax, 40, 70) GR.setspace(zmin, zmax, 40, 70)
xtick = GR.tick(xmin, xmax) / 2 xtick = GR.tick(xmin, xmax) / 2
ytick = GR.tick(ymin, ymax) / 2 ytick = GR.tick(ymin, ymax) / 2
@ -572,7 +611,6 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
end end
z = reshape(z, length(x) * length(y)) z = reshape(z, length(x) * length(y))
if lt == :surface if lt == :surface
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
GR.setfillcolorind(0) GR.setfillcolorind(0)
@ -589,9 +627,8 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
end end
elseif lt == :heatmap elseif lt == :heatmap
x, y, z = p[:x], p[:y], transpose_z(p, p[:z].surf, false) x, y, z = p[:x], p[:y], transpose_z(p, p[:z].surf, false)
zmin, zmax = GR.adjustrange(minimum(z), maximum(z)) zmin, zmax = gr_getzlims(d, minimum(z), maximum(z), true)
GR.setspace(zmin, zmax, 0, 90) GR.setspace(zmin, zmax, 0, 90)
GR.setcolormap(GR.COLORMAP_COOLWARM)
z = reshape(z, length(x) * length(y)) z = reshape(z, length(x) * length(y))
GR.surface(x, y, z, GR.OPTION_CELL_ARRAY) GR.surface(x, y, z, GR.OPTION_CELL_ARRAY)
if cmap if cmap
@ -601,7 +638,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
end end
elseif lt 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_getzlims(d, minimum(z), maximum(z), true)
GR.setspace(zmin, zmax, 40, 70) GR.setspace(zmin, zmax, 40, 70)
xtick = GR.tick(xmin, xmax) / 2 xtick = GR.tick(xmin, xmax) / 2
ytick = GR.tick(ymin, ymax) / 2 ytick = GR.tick(ymin, ymax) / 2
@ -695,7 +732,7 @@ function gr_display(plt::Plot{GRBackend}, clear=true, update=true,
GR.setwindow(-1, 1, -1, 1) GR.setwindow(-1, 1, -1, 1)
rmin, rmax = GR.adjustrange(minimum(r), maximum(r)) rmin, rmax = GR.adjustrange(minimum(r), maximum(r))
gr_polaraxes(rmin, rmax) gr_polaraxes(rmin, rmax)
phi, r, = p[:x], p[:y] phi, r = p[:x], p[:y]
r = 0.5 * (r - rmin) / (rmax - rmin) r = 0.5 * (r - rmin) / (rmax - rmin)
n = length(r) n = length(r)
x = zeros(n) x = zeros(n)