无法使用rotate_deg_around()将matplotlib补丁对象绕特定点旋转 [英] Unable to rotate a matplotlib patch object about a specific point using rotate_deg_around( )

查看:70
本文介绍了无法使用rotate_deg_around()将matplotlib补丁对象绕特定点旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 rotate_deg_around() 函数围绕其左下角旋转一个 matplotlib 矩形补丁对象.但是,补丁总是绕着一些不同的点旋转.知道为什么rotate_deg_around()函数不能产生期望的结果吗?

我的代码如下:

  f,(ax1)= plt.subplots(1,1,figsize =(6,6))f.subplots_adjust(hspace=0,wspace=0)ts = ax1.transData坐标= ts.transform([1,1])tr = mpl.transforms.Affine2D().rotate_deg_around(coords[0], coords[1], 10)t = ts + trrec0 =补丁.Rectangle((1,1),3,2,linewidth = 1,edgecolor ='r',facecolor ='none')ax1.add_patch(rec0)#旋转矩形补丁rect1 =补丁.Rectangle((1,1),3,2,linewidth = 1,edgecolor ='b',facecolor ='none',transform = t)ax1.add_patch(rect1)#矩形左下角plt.plot([1],[1],marker ='o',markersize = 3,color ="green")plt.grid(真)ax1.set_xlim(0,6)ax1.set_ylim(-1,4)

产生下图:

我关注了

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 different point. Any idea why the rotate_deg_around() function is not producing the desired result?

My code is as 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 the following figure:

I have followed Unable to rotate a matplotlib patch object about a specific point using rotate_around( )

Any help is much appreciated.

解决方案

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()

这篇关于无法使用rotate_deg_around()将matplotlib补丁对象绕特定点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆