我正在尝试使用matplotlib绘制并旋转围绕Julia中特定点的矩形进行绘图。但是,似乎我必须结合两个转换来完成这项工作,我不知道该怎么做。
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]
我现在要做的是为了结合转换:
t3 = t1 + t2
rect_rotated[:set_transform](t3)
ax[:add_patch](rect)
ax[:add_patch](rect_rotated)
但是,我收到以下错误
错误:LoadError:MethodError:没有匹配的方法+(:: PyCall.PyObject,:: PyCall.PyObject)
我认为是因为PyPlot-Wrapper不支持“+” - 符号来组合底层转换。
有谁知道如何使这项工作?谢谢
答案 0 :(得分:1)
Python使用第一个对象的方法实现运算符重载,例如t3 = t1 + t2
相当于t3 = t1.__add__(t2)
。
在朱莉娅,这变成了
t3 = t1[:__add__](t2)
答案 1 :(得分:1)
有了@ DavidP.Sanders的评论,工作代码如下所示:
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")
<强>结果:强> Rotated rectangle