-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
geom.vector? #791
Comments
Here is a quick implementation of vectors/arrows/segments. For this initial implementation, I've simply tried to make use of the aesthetic args that already exist. Suggestions for improvements welcome! I think the following example shows how to use it. You'll need this file: And here is an example: using DataFrames
include("Geom_segments.jl")
function expand_grid(n)
q = linspace(-10, 10, n)
return vcat([[i j] for i in q, j in q]...)
end
function dsinc(x,y, sc)
z = hypot(x,y)
dz = cos(z)./z - sin(z)./(z.^2)
θ = atan2(y,x)
dx = sc*abs(dz).*cos(θ)
dy = sc*abs(dz).*sin(θ)
return DataFrame(xa=x-dx, ya=y-dy, xb=x+dx, yb=y+dy, dz=dz)
end
function sincf(x, y)
z = hypot(x,y)
z = sin(z)./(z)
return DataFrame(x=x, y=y, z=(z))
end
X = expand_grid(50)
X2 = expand_grid(20)
D1 = sincf(X[:,1], X[:,2])
D2 = dsinc(X2[:,1], X2[:,2], 2.0)
theme1 = Gadfly.Theme(default_color=colorant"black", line_style=[1mm, 0.0mm])
theme2 = Gadfly.Theme(line_style = [0.75mm, 0.5mm])
coord = Coord.cartesian(xmin=-10.0, xmax=10.0, ymin=-10.0, ymax=10.0)
l1 = layer(D1, x=:x, y=:y, color=:z, Geom.rectbin, order=1)
l2 = layer(D2, x=:xa, xmax=:xb, y=:ya, ymax=:yb, shape=true,
Geom_segment, theme1, order=2 )
l3 = layer(D2, x=:xa, xmax=:xb, y=:ya, ymax=:yb, color=:dz, shape=true,
Geom_segment, theme1, order=2)
p1 = plot(coord, l1, l2,
Guide.title("f(x,y)=sin(hypot(x,y))/hypot(x,y)"),
)
p2 = plot(coord, l3,
Scale.color_continuous(minvalue=-0.5, maxvalue=0.1),
Guide.title("f'(x,y)"), Guide.xlabel("x"), Guide.ylabel("")
)
# hstack(p1, p2)
Note in this example I have plotted the gradient field in a particular way. |
@Mattriks This is what I need. Why not merge? |
See the conversation starting from March 30 at #815 about the current status of Gadfly development. |
Oh, I see. It's a pitty! No wonder Plots.jl discards gadfly recently. |
Since the development of Gadfly has been moving forward it would be great to have functionality like this inbuilt! |
#823 was merged last august. Geom.segment. perhaps we should close this issue? |
Yes maybe that is already powerful enough, I was looking for a way to draw vectorfields and I don't found |
Maybe @Mattriks can give some insight since he was the original developer of |
Note you can use |
Here is an example. Like using CoupledFields, DataFrames, RDatasets
function VecField{T<:Float64}(X::Matrix{T}, Z::Vector{T}, sc::T)
# sc controls the size of the vectors
kpars = GaussianKP(X)
∇g = hcat(gradvecfield([1.0 -7.0], X, Z[:,1:1], kpars)...)'
vecf = [X-∇g*sc X+∇g*sc]
return DataFrame(x=X[:,1], y=X[:,2], x1=vecf[:,1], y1=vecf[:,2], x2=vecf[:,3], y2=vecf[:,4], col=Z[:,1])
end
volcano = Matrix{Float64}(dataset("datasets", "volcano"))
volc = volcano[1:4:end, 1:4:end]
Z = vec(volc)
X = vcat([[x y] for x in 1.0:size(volc,1), y in 1.0:size(volc,2)]...)
D = VecField(X, Z, 0.05)
coord = Coord.cartesian(xmin=1, xmax=22, ymin=1, ymax=16)
p = plot(D, coord,
layer(x=:x1, y=:y1, xend=:x2, yend=:y2, color=:col, Geom.vector),
layer(z=volc, x=1:22, y=1:16, Geom.contour(levels=7)),
Scale.x_continuous(minvalue=1.0, maxvalue=22.0),
Scale.y_continuous(minvalue=1.0, maxvalue=16.0),
Guide.xlabel("x"), Guide.ylabel("y"),
Theme(key_position=:none)
)
|
please submit a PR! is |
In ggplot you can plot vectors using |
Here is my custom statistic module Stat
using CoupledFields, Gadfly
type VecFieldStatistic <: Gadfly.StatisticElement
smoothness::Float64
scale::Float64
samples::Int
function VecFieldStatistic(; smoothness=1.0, scale=1.0, samples=20)
new(smoothness, scale, samples)
end
end
function Gadfly.input_aesthetics(stat::VecFieldStatistic)
return [:z, :x, :y, :color]
end
function Gadfly.output_aesthetics(stat::VecFieldStatistic)
return [:x, :y, :xend, :yend, :color]
end
function Gadfly.default_scales(stat::VecFieldStatistic)
return [Gadfly.Scale.x_continuous(), Gadfly.Scale.y_continuous()]
end
const vectorfield = VecFieldStatistic
function Gadfly.Stat.apply_statistic(stat::VecFieldStatistic,
scales::Dict{Symbol, Gadfly.ScaleElement},
coord::Gadfly.CoordinateElement,
aes::Gadfly.Aesthetics)
xs = aes.x === nothing ? nothing : convert(Vector{Float64}, aes.x)
ys = aes.y === nothing ? nothing : convert(Vector{Float64}, aes.y)
if typeof(aes.z) <: Function
if xs == nothing && aes.xmin != nothing && aes.xmax != nothing
xs = linspace(aes.xmin[1], aes.xmax[1], stat.samples)
end
if ys == nothing && aes.ymin != nothing && aes.ymax != nothing
ys = linspace(aes.ymin[1], aes.ymax[1], stat.samples)
end
zs = Float64[aes.z(x, y) for x in xs, y in ys]
elseif typeof(aes.z) <: Matrix
zs = convert(Matrix{Float64}, aes.z)
if xs == nothing
xs = collect(Float64, 1:size(zs)[1])
end
if ys == nothing
ys = collect(Float64, 1:size(zs)[2])
end
if size(zs) != (length(xs), length(ys))
error("Stat.vectorfield requires dimension of z to be length(x) by length(y)")
end
else
error("Stat.vectorfield requires either a matrix or a function")
end
X = vcat([[x y] for x in aes.x, y in aes.y]...)
Z = vec(zs)
kpars = GaussianKP(X)
∇g = hcat(gradvecfield([stat.smoothness -7.0], X, Z[:,1:1], kpars)...)'
vecf = [X-∇g*stat.scale X+∇g*stat.scale]
aes.z = nothing
aes.x = vecf[:,1]
aes.y = vecf[:,2]
aes.xend = vecf[:,3]
aes.yend = vecf[:,4]
color_scale = get(scales, :color, Gadfly.Scale.color_continuous_gradient())
Scale.apply_scale(color_scale, [aes], Gadfly.Data(color=Z))
end
end
using Stat Now function sincf(x, y)
z = hypot(x,y)
z = sin(z)./z
return z
end
xlr = linspace(-14, 14, 22)
xhr = linspace(-15, 15, 50)
# Note contour and vectorfield are statistics:
# Geom.contour is a short expression for Geom.path + Stat.contour
layer1 = layer(z=(x,y)->sincf(x,y), x=xhr, y=xhr, Geom.path, Gadfly.Stat.contour)
layer2 = layer(z=(x,y)->sincf(x,y), x=xlr, y=xlr, Geom.vector,
Stat.vectorfield(smoothness=0.2, scale=5.0), order=2 )
p = plot(layer1, layer2,
Scale.x_continuous(minvalue=-15.0, maxvalue=15.0),
Scale.y_continuous(minvalue=-15.0, maxvalue=15.0),
Guide.xlabel("x"), Guide.ylabel("y"),
Guide.colorkey("z"),
Theme(panel_fill=colorant"black")
) The next step is to implement |
see #992 |
i think up to now its not possible to draw a vector in Gadfly. It were nice to have in geometry an option geom.vector.
Thank You for your great job!
Albert
The text was updated successfully, but these errors were encountered: