使用matplotlib

时间:2017-01-20 07:12:52

标签: matplotlib julia

我正在尝试使用matplotlib旋转一组应该代表Julia汽车的矩形。以下代码有效。然而,当我试图旋转汽车时,看起来它变得更厚,因为它接近90度。这是代码:

using PyCall
using PyPlot
@pyimport matplotlib.animation as animation
@pyimport matplotlib.patches as patches
@pyimport matplotlib as mpl




function drawCar(pos::Array{Float64})

    x = pos[1]
    y = pos[2]
    psi = pos[3]
    pos = [x;y]

    # define outer car geometry
    length = 36.5 / 100.0
    width = 20.0 / 100.0
    btmLeft_vertex = pos + [-length/2,-width/2]


    # define tire geometry
    tire_width = 2.0/100.0
    tire_length = 5.0/100.0
    tire_distance = 8.0/100.0

    # define patches that make up the car's appearance
    # car for outer boundary and tires at tl=top left, tr=top right, bl=bottom left, br=bottom right
    car = patches.Rectangle(btmLeft_vertex,length,width,fill=false, linewidth=3.0,color="black")
    tl = patches.Rectangle(pos + [-tire_distance-tire_length,width/2-tire_width],tire_length,tire_width,color="black")
    tr = patches.Rectangle(pos + [tire_distance,width/2-tire_width],tire_length,tire_width,color="black")
    bl = patches.Rectangle(pos + [-tire_distance-tire_length,-width/2],tire_length,tire_width,color="black")
    br = patches.Rectangle(pos + [tire_distance,-width/2],tire_length,tire_width,color="black")

    car_parts = [car,tl,tr,bl,br]

    return car_parts    
end

function updateCarParts(fig,car_parts,angle)


    ax = gca()
    # define a rotation transformation and a transformation from data coordinate system to display coordinate system
    t1 = mpl.transforms[:Affine2D]()
    t1[:rotate_deg_around](0.0,0.0, angle)
    t2 = ax[:transData]
    # combine transformations
    t3 = t1[:__add__](t2)

    # apply rotation transformation to all car parts
    for p in car_parts
      p[:set_transform](t3)
     end
     return car_parts

end


# Play / Start the animation 
function play()

    fig = figure()
    hold(true)

    # define initial position and orientation of the car
    pos = [0.0,0.0,0.0]

    # get the patches that make up the car and add them to the current figure
    car_parts = drawCar(pos)
    ax = gca()
    for p in car_parts
        ax[:add_patch](p)
    end

    # define animation function
    function animate(frameNum)
      frameNum += 1
      # update the orientation of the car parts
      angle = frameNum
      car_parts = updateCarParts(fig,car_parts,angle)
      return nothing
    end

  # define animation object
  anim = animation.FuncAnimation(fig, animate, frames=200, interval=50,repeat=true)
  axis([-0.5,0.5,-0.5,0.5],"equal")
  grid(true, which="both")

  end

我不知道,我做错了什么。谢谢你的帮助。

编辑:这是两张0度(顶部)旋转并旋转90度(底部)的图像。底部图像中的汽车看起来更厚。 slim car (0 deg) thick car (90 deg)

2 个答案:

答案 0 :(得分:0)

我相信(未经测试)你需要这个 axis([-0.5,0.5,-0.5,0.5], "equal") 当你做每个情节时,即在animate函数中。

答案 1 :(得分:0)

在play()函数结束时添加axis equal命令解决了问题。谢谢David P. Sanders。

以下是更新后的代码:

 using PyCall
 using PyPlot
 @pyimport matplotlib.animation as animation
 @pyimport matplotlib.patches as patches
 @pyimport matplotlib as mpl

function drawCar(pos::Array{Float64})
    x = pos[1]
    y = pos[2]
    psi = pos[3]
    pos = [x;y]

    # define outer car geometry
    length = 36.5 / 100.0
    width = 20.0 / 100.0
    btmLeft_vertex = pos + [-length/2,-width/2]


    # define tire geometry
    tire_width = 2.0/100.0
    tire_length = 5.0/100.0
    tire_distance = 8.0/100.0

    # define patches that make up the car's appearance
    # car for outer boundary and tires at tl=top left, tr=top right, bl=bottom left, br=bottom right
    car = patches.Rectangle(btmLeft_vertex,length,width,fill=false, linewidth=3.0,color="black")
    tl = patches.Rectangle(pos + [-tire_distance-tire_length,width/2-tire_width],tire_length,tire_width,color="black")
    tr = patches.Rectangle(pos + [tire_distance,width/2-tire_width],tire_length,tire_width,color="black")
    bl = patches.Rectangle(pos + [-tire_distance-tire_length,-width/2],tire_length,tire_width,color="black")
    br = patches.Rectangle(pos + [tire_distance,-width/2],tire_length,tire_width,color="black")

    car_parts = [car,tl,tr,bl,br]

    return car_parts    
end

function updateCarParts(fig,car_parts,angle)
    ax = gca()
    # define a rotation transformation and a transformation from data coordinate system to display coordinate system
    t1 = mpl.transforms[:Affine2D]()
    t1[:rotate_deg_around](0.0,0.0, angle)
    t2 = ax[:transData]
    # combine transformations
    t3 = t1[:__add__](t2)

    # apply rotation transformation to all car parts
    for p in car_parts
      p[:set_transform](t3)
  end
  return car_parts
end

# Play / Start the animation 
function play()

    fig = figure()
    hold(true)

    # define initial position and orientation of the car
    pos = [0.0,0.0,0.0]

    # get the patches that make up the car and add them to the current figure
    car_parts = drawCar(pos)
    ax = gca()
    for p in car_parts
        ax[:add_patch](p)
    end

    # define animation function
    function animate(frameNum)
      frameNum += 1
      # update the orientation of the car parts
      angle = frameNum
      car_parts = updateCarParts(fig,car_parts,angle)
      return nothing
    end

  # define animation object
  anim = animation.FuncAnimation(fig, animate, frames=200, interval=50,repeat=true)
  grid(true, which="both")
  ax[:set_xlim]([-0.5,0.5])
  ax[:set_ylim]([-0.5,0.5])
  ax[:set_aspect]("equal")
end