gpt4 book ai didi

python - 为什么 matplotlib circle/patchCollection 的旋转点会改变

转载 作者:行者123 更新时间:2023-12-04 00:00:02 33 4
gpt4 key购买 nike

3种不同的旋转度数:

enter image description here

嗨,我正在尝试旋转 Matplotlib Collections.PatchCollection(圆圈)。我无法保持相同的旋转点。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections
import matplotlib as mpl

num = 5
sizes = 0.01, 0.01, 0.01, 0.01, 0.01
xy = (.5,.7),(.5,.6),(.5,.5),(.5,.4),(.5,.3)


print(xy)


# Note that the patches won't be added to the axes, instead a collection will
patches = [plt.Circle(center, size) for center, size in zip(xy, sizes)]
patches2 = [plt.Circle(center, size) for center, size in zip(xy, sizes)]

fig, ax = plt.subplots(1,1)

coll = matplotlib.collections.PatchCollection(patches, facecolors='none')
coll2 = matplotlib.collections.PatchCollection(patches2, facecolors='None')
ax.add_collection(coll)
ax.add_collection(coll2)

t2 = mpl.transforms.Affine2D().rotate_deg(12.5) + ax.transData
coll2.set_transform(t2)



plt.show()

旋转后如何保持同一个旋转点?提前致谢

最佳答案

如果您不指定旋转中心,则旋转将围绕 (0,0)。要绕某个点旋转,可以先减去它的坐标,然后旋转,然后再加上这些坐标。

下面的例子说明了区别:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.collections
import matplotlib as mpl

sizes = [0.01, 0.01, 0.01, 0.01, 0.01]
xy = [(.5, .7), (.5, .6), (.5, .5), (.5, .4), (.5, .3)]
xc, yc = xy[-1] # (.5, .3)

fig, axes = plt.subplots(ncols=2, figsize=(12, 4))
for ax in axes:
patches = [plt.Circle(center, size) for center, size in zip(xy, sizes)]
coll = matplotlib.collections.PatchCollection(patches, facecolors='none')
ax.add_collection(coll)
cmap = plt.get_cmap('rainbow')
for angle in np.arange(12.5, 360, 12.5):
patches2 = [plt.Circle(center, size) for center, size in zip(xy, sizes)]
coll2 = matplotlib.collections.PatchCollection(patches2, facecolors=cmap(angle / 360))
if ax == axes[0]:
t2 = mpl.transforms.Affine2D().rotate_deg(angle) + ax.transData
ax.set_title('rotation around (0, 0)')
else:
t2 = mpl.transforms.Affine2D().translate(-xc, -yc).rotate_deg(angle).translate(xc, yc) + ax.transData
ax.set_title(f'rotation around {xc, yc}')
coll2.set_transform(t2)
ax.add_collection(coll2)
ax.autoscale()
plt.show()

plot

关于python - 为什么 matplotlib circle/patchCollection 的旋转点会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63265342/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com