miga89
miga89

Reputation: 438

Rotate rectangle about specific point using PyPlot in Julia

I am trying to draw and rotate a rectangle about a specific point in Julia using matplotlib for plotting. However, it seems like I have to combine two transformations to make that work, which I don't know how to do.

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

fig = figure(1)
ax = gca()
axis([-4,4,-4,4])

# specify non-rotated rectangle
length = 4
width = 2
rect = patches.Rectangle([1,1],length,width,color="blue",alpha=0.3)
rect_rotated = patches.Rectangle([1,1],length,width,color="red",alpha=0.3)
 # rotate about the following point
point = [3,2]

# try to rotate rectangle using matplotlib's transformations
t1 = mpl.transforms[:Affine2D]()
t1[:rotate_deg_around](point[1],point[2], -30)

# apparently one also has to transform between data coordinate system and display coordinate system
t2 = ax[:transData]

What I would like to do now in order to combine the transformations:

t3 = t1 + t2
rect_rotated[:set_transform](t3)
ax[:add_patch](rect)
ax[:add_patch](rect_rotated)

However, I get the following error

ERROR: LoadError: MethodError: no method matching +(::PyCall.PyObject, ::PyCall.PyObject)

which I believe is because of the PyPlot-Wrapper that doesn't support the "+"-sign to combine the underlying transformations.

Does anyone know how to make this work? Thank you

Upvotes: 2

Views: 735

Answers (2)

miga89
miga89

Reputation: 438

With the remark of @DavidP.Sanders, the working code looks like this:

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

fig = figure(1)
ax =gca()


# specify non-rotated rectangle
length = 4
width = 2
rect = patches.Rectangle([1,1],length,width,color="blue",alpha=0.3)
rect_rotated = patches.Rectangle([1,1],length,width,color="red",alpha=0.3)
 # rotate about the following point
point = [3,2]

# try to rotate rectangle using matplotlib's transformations
t1 = mpl.transforms[:Affine2D]()
t1[:rotate_deg_around](point[1],point[2], -30)

# apparently one also has to transform between data coordinate system and display coordinate system
t2 = ax[:transData]
t3 = t1[:__add__](t2)
rect_rotated[:set_transform](t3)
ax[:add_patch](rect)
ax[:add_patch](rect_rotated)
axis([-1,6,-1,6],"equal")

Result: Rotated rectangle

Upvotes: 1

David P. Sanders
David P. Sanders

Reputation: 5325

Python implements operator overloading using methods of the first object, e.g. t3 = t1 + t2 is equivalent to t3 = t1.__add__(t2).

In Julia, this becomes t3 = t1[:__add__](t2)

Upvotes: 1

Related Questions