Daniel Kocevski
Daniel Kocevski

Reputation: 381

Rotate a matplotlib Path object

I'm using a matplotlib Path object to create a custom plotting marker as described here. I'd like to rotate the resulting path about its center by an arbitrary angle. Any suggestions on how to do this would be greatly appreciated.

Here's the code I'm using to create and plot the custom marker

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

This is what I get:

enter image description here

This is the change I'd like to affect:

enter image description here

Upvotes: 3

Views: 1639

Answers (2)

Till Hoffmann
Till Hoffmann

Reputation: 9887

You could call the 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()

Upvotes: 5

user206545
user206545

Reputation:

You could run the vertices list you have through a rotation matrix using an angle of 45 degrees to generate a new list of vertices. Something like this:

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

Upvotes: 0

Related Questions