Unable to rotate a matplotlib patch object about a specific point using rotate_deg_around( )

后端 未结 1 680
傲寒
傲寒 2020-12-21 15:48

I want to rotate a matplotlib rectangular patch object about its lower left corner using the rotate_deg_around() function. However, the patch is always rotating about some d

相关标签:
1条回答
  • 2020-12-21 16:15

    The short answer is: You need to change the axis limits before doing the transformations. Else you rotate about a point on the canvas which has been at position (1,1) (upper right corner) before setting the limits to something different.


    The better answer is actually the following: In order to rotate about a point in data coordinates, one should not use display coordinates for the definition of the center of rotation but exactly the data coodinates themselves.

    So instead of first converting to display coordinates and then rotating, one should first rotate and then transform to display coordinates

    ts = ax1.transData
    coords = [1,1]
    tr = matplotlib.transforms.Affine2D().rotate_deg_around(coords[0],coords[1], 10)
    t = tr + ts
    

    Complete code (where it does not matter if and where axis limits are set, and which is independend of figure resizing etc.):

    import matplotlib.pyplot as plt
    import matplotlib
    
    f,(ax1) = plt.subplots(1,1,figsize=(3,3))
    f.subplots_adjust(hspace=0,wspace=0)
    
    
    ts = ax1.transData
    coords = [1,1]
    tr = matplotlib.transforms.Affine2D().rotate_deg_around(coords[0],coords[1], 10)
    t = tr + ts
    rec0 = matplotlib.patches.Rectangle((1,1),3,2,linewidth=1,edgecolor='r',facecolor='none')
    ax1.add_patch(rec0)
    #Rotated rectangle patch
    rect1 = matplotlib.patches.Rectangle((1,1),3,2,linewidth=1,edgecolor='b',facecolor='none',transform=t)
    ax1.add_patch(rect1)
    # Rectangles lower left corner
    plt.plot([1], [1], marker='o', markersize=3, color="green")
    
    plt.grid(True)
    ax1.set_xlim(0,6)
    ax1.set_ylim(-1,4)
    
    plt.show()
    

    0 讨论(0)
提交回复
热议问题