Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 441
Daniel Kocevski Avatar asked Jul 02 '26 07:07

Daniel Kocevski


1 Answers

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()
like image 123
Till Hoffmann Avatar answered Jul 03 '26 19:07

Till Hoffmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!