旋转 matplotlib Path 对象

Rotate a matplotlib Path object

我正在使用 matplotlib Path object to create a custom plotting marker as described here。我想将生成的路径围绕其中心旋转任意角度。任何有关如何执行此操作的建议将不胜感激。

这是我用来创建和绘制自定义标记的代码

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()

这是我得到的:

这是我想要影响的更改:

您可以 运行 通过使用 45 度角的旋转矩阵生成新的顶点列表的顶点列表。像这样:

theta = 45. * pi / 180.
verts_rotated = [ (x * cos(theta) - y * sin(theta), x * sin(theta) + y * cos(theta)) for x,y in verts]

您可以调用 transformed method on your path object with a rotational transform

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()