Matplotlib: rotating a patch

后端 未结 2 1272
栀梦
栀梦 2020-12-06 05:03

I wanted to rotate a Rectangle in matplotlib but when I apply the transformation, the rectangle doesn\'t show anymore:

rect = mpl.patches.Rectangle((0.0120,0         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 05:37

    Apparently the transforms on patches are composites of several transforms for dealing with scaling and the bounding box. Adding the transform to the existing plot transform seems to give something more like what you'd expect. Though it looks like there's still an offset to work out.

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as patches
    import matplotlib as mpl
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    rect = patches.Rectangle((0.0120,0),0.1,1000)
    
    t_start = ax.transData
    t = mpl.transforms.Affine2D().rotate_deg(-45)
    t_end = t_start + t
    
    rect.set_transform(t_end)
    
    print repr(t_start)
    print repr(t_end)
    ax.add_patch(rect)
    
    plt.show()
    

提交回复
热议问题