Reputation: 381
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:
This is the change I'd like to affect:
Upvotes: 3
Views: 1639
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
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