python - Unable to rotate a matplotlib patch object about a specific point using rotate_deg_around( ) -
i want rotate matplotlib rectangular patch object lower left corner using rotate_deg_around() function. however, patch rotating different point. idea why rotate_deg_around() function not producing desired result?
my code follows:
f,(ax1) = plt.subplots(1,1,figsize=(6,6)) f.subplots_adjust(hspace=0,wspace=0) ts = ax1.transdata coords = ts.transform([1,1]) tr = mpl.transforms.affine2d().rotate_deg_around(coords[0], coords[1], 10) t = ts + tr rec0 = patches.rectangle((1,1),3,2,linewidth=1,edgecolor='r',facecolor='none') ax1.add_patch(rec0) #rotated rectangle patch rect1 = 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)
resulting in following figure:
i have followed unable rotate matplotlib patch object specific point using rotate_around( )
any appreciated.
the short answer is: need change axis limits before doing transformations. else rotate point on canvas has been @ position (1,1)
(upper right corner) before setting limits different.
the better answer following: in order rotate point in data coordinates, 1 should not use display coordinates definition of center of rotation data coodinates themselves.
so instead of first converting display coordinates , rotating, 1 should first rotate , transform 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 not matter if , axis limits set, , independend of figure resizing etc.):
import matplotlib.pyplot 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()
Comments
Post a Comment