Skip to content

Commit

Permalink
add plot2d
Browse files Browse the repository at this point in the history
  • Loading branch information
Uwe Fechner committed May 5, 2024
1 parent 9d430fc commit 267c740
Showing 1 changed file with 90 additions and 1 deletion.
91 changes: 90 additions & 1 deletion src/ControlPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PyPlot.show as plshow
import Base.display
import JLD2

export plot, plotx, plotxy, plt, load, save
export plot, plotx, plotxy, plot2d, plt, load, save

mutable struct PlotX
X
Expand Down Expand Up @@ -171,4 +171,93 @@ function display(P::PlotX)
nothing
end

function plot2d_(pos, reltime; zoom=true, front=false, segments=6, fig="", dz_zoom= 1.5, dz=-5.0, dx=-16.0, lines, sc, txt)
x = Float64[]
z = Float64[]
for i in eachindex(pos)
if front
push!(x, pos[i][2])
else
push!(x, pos[i][1])
end
push!(z, pos[i][3])
end
x_max = maximum(x)
z_max = maximum(z)
xlabel = "x [m]"
if front xlabel = "y [m]" end
if isnothing(lines)
if fig != ""
plt.figure(fig)
end
lines=[]
line, = plt.plot(x,z; linewidth="1")
push!(lines, line)
sc = plt.scatter(x, z; s=25, color="red")
if zoom
txt = plt.annotate("t=$(round(reltime,digits=1)) s",
xy=(x_max, z_max+dz_zoom), fontsize = 14)
plt.xlim(x_max-15.0, x_max+5)
plt.ylim(z_max-15.0, z_max+5)
else
txt = plt.annotate("t=$(round(reltime,digits=1)) s",
xy=(x_max+dx, z_max+dz), fontsize = 14)
plt.xlim(0, x_max+5)
plt.ylim(0, z_max+5)
end
if length(pos) > segments+1
s=segments
line, = plt.plot([x[s+1],x[s+4]],[z[s+1],z[s+4]], linewidth="1"); push!(lines, line) # S6
line, = plt.plot([x[s+2],x[s+5]],[z[s+2],z[s+5]], linewidth="1"); push!(lines, line) # S8
line, = plt.plot([x[s+3],x[s+5]],[z[s+3],z[s+5]], linewidth="1"); push!(lines, line) # S7
line, = plt.plot([x[s+2],x[s+4]],[z[s+2],z[s+4]], linewidth="1"); push!(lines, line) # S2
line, = plt.plot([x[s+1],x[s+5]],[z[s+1],z[s+5]], linewidth="1"); push!(lines, line) # S5
end
plt.xlabel(xlabel, fontsize=14)
plt.ylabel("z [m]", fontsize=14)
plt.grid(true)
plt.grid(which="major", color="#DDDDDD")
else
lines[1].set_xdata(x)
lines[1].set_ydata(z)
if length(pos) > segments+1
s=segments
lines[2].set_xdata([x[s+1],x[s+4]]) # S6
lines[2].set_ydata([z[s+1],z[s+4]]) # S6
lines[3].set_xdata([x[s+2],x[s+5]]) # S8
lines[3].set_ydata([z[s+2],z[s+5]]) # S8
lines[4].set_xdata([x[s+3],x[s+5]]) # S7
lines[4].set_ydata([z[s+3],z[s+5]]) # S7
lines[5].set_xdata([x[s+2],x[s+4]]) # S2
lines[5].set_ydata([z[s+2],z[s+4]]) # S2
lines[6].set_xdata([x[s+1],x[s+5]]) # S5
lines[6].set_ydata([z[s+1],z[s+5]]) # S5
end
sc.set_offsets(hcat(x,z))
# xy=(x_max, z_max+8.0)
txt.set_text("t=$(round(reltime,digits=1)) s")
if zoom
txt.set_x(x_max)
txt.set_y(z_max+dz_zoom)
plt.xlim(x_max-15.0, x_max+5)
plt.ylim(z_max-15.0, z_max+5)
else
txt.set_x(x_max+dx)
txt.set_y(z_max+dz)
plt.xlim(0, x_max+5)
plt.ylim(0, z_max+5)
end
plt.gcf().canvas.draw()
end
sleep(0.01)
lines, sc, txt
end

plot2d = let lines = nothing, sc = nothing, txt = nothing # Note: Must all be on same line as let!
function(pos, reltime=0.0; kwargs...)
lines, sc, txt = plot2d_(pos, reltime; lines, sc, txt, kwargs...)
end
end


end

0 comments on commit 267c740

Please sign in to comment.