旋转 matplotlib Path 对象

3
我正在使用matplotlib Path对象创建自定义绘图标记,如此处所述。 我想以任意角度围绕其中心旋转结果路径。 有关如何执行此操作的建议将不胜感激。
这是我用于创建和绘制自定义标记的代码。
import matplotlib.path
import matplotlib.pylab as plt

def getCustomMarker():

    verts = [( -5 , -15 ),
                ( -5 , -5 ),
                ( -15 , -5 ),
                ( -15 , 5 ),
                ( -5 , 5 ),
                ( -5 , 15 ),
                ( 5 , 15 ),
                ( 5 , 5 ),
                ( 15 , 5 ),
                ( 15 , -5 ),
                ( 5 , -5 ),
                ( 5 , -15 ),
                ( -5 , -15 )]

    verts = verts

    codes = [matplotlib.path.Path.MOVETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.LINETO,
                matplotlib.path.Path.CLOSEPOLY]

    path = matplotlib.path.Path(verts, codes)

    return path

plt.scatter([0,0],[0,0], marker=getCustomMarker(), s=5000)
plt.show()

这是我得到的内容:

enter image description here

这是我想要实现的更改:

enter image description here

2个回答

5
你可以在路径对象上调用transformed方法,并使用旋转变换
import matplotlib as mpl

marker = getCustomMarker()
marker = marker.transformed(mpl.transforms.Affine2D().rotate_deg(45))
plt.scatter([0,0],[0,0], marker=marker, s=5000)
plt.show()

非常好。谢谢。 - Daniel Kocevski

0
您可以通过使用45度的旋转矩阵将顶点列表进行旋转,从而生成一个新的顶点列表。具体操作如下:
theta = 45. * pi / 180.
verts_rotated = [ (x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta)) for x,y in verts]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接