使用matplotlib围绕Julia中的公共点旋转多个矩形

使用matplotlib围绕Julia中的公共点旋转多个矩形,matplotlib,julia,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

我正在尝试使用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度旋转(底部)的图像。下图中的汽车看起来更厚。 我相信(未经测试)您需要
轴([-0.5,0.5,-0.5,0.5],“相等”)

在执行每个绘图时,即在
animate
函数中。

在play()函数末尾添加轴相等命令解决了问题。谢谢大卫·P·桑德斯

以下是更新的代码:

 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

非常感谢。但这并不能改变人们的行为。你能展示这个问题的图片吗?Python中是否发生了一些不同的情况?我添加了两个图像进行解释,正如我所预测的,这看起来只是纵横比的问题。(即-0.4和-0.2之间的距离在x轴和y轴上不同。)尝试移动
命令(或添加更多相同命令),直到找到正确的位置。无论如何,这是一个关于matplotlib的问题,而不是Julia。