Animation keeps changing xlims ylims

Leibniz
Tue, 29 Jul 2025 18:12:50 +0000
New to Julia

Greetings,

I have been struggling for hours to create an animation (particles in a box) where the xlims and ylims of the plot window remain fixed during the run.

@userplot SystemPlot

@recipe function f(sp::SystemPlot)

   # Data frame holding x and y coordinates of N particles.
    frame_data = sp.args[1]   # Index necessary because sp.args is a tuple.

    # Some attributes because particles are plotted as filled circles.
    radius = get(plotattributes, :radius, 0.5)
    cos_theta = get(plotattributes, :cos_theta, nothing)
    sin_theta = get(plotattributes, :sin_theta, nothing)


    # Axis limits. They should remain fixed!
    xlims --> get(plotattributes, :xlims, nothing)
    ylims --> get(plotattributes, :ylims, nothing)

    aspect_ratio --> :equal
    legend --> false
    xlabel --> "x"
    ylabel --> "y"
    title --> "Frame $(frame_data.Frame[1])"

    for i in 1:nrow(frame_data)

        # some function to create circle shapes.
        x, y = draw_circle(frame_data.x[i], frame_data.y[i], radius, cos_theta, sin_theta)
        shape = Shape(x, y)

        @series begin
            seriestype := :shape
            fillcolor  := :blue
            alpha      := 0.5
            linecolor  := :black
            shape
         end
    end

But if I call this thing with

    x_lims =  # some given tupel, e.g., (0,10)
    y_lims =  # some given tupel, e.g., (0,10)

    anim = @animate for idx in frame_idx
        frame_data = frames[idx]
        systemplot(frame_data;
                   cos_theta=cos_theta,
                   sin_theta=sin_theta,
                   radius=radius,
                   xlims=x_lims,
                   ylims=y_lims)
    end

the xlims and ylims of the resulting plot window change from frame to frame.
What am I doing wrong? It looks like the recipe does not properly process the limits.

Regards!

3 posts - 2 participants