gpt4 book ai didi

python - get_path() 从 matplotlib.patches 返回一个 Circle

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

有谁知道 matplotlib.patchesCircleget_path() 返回什么?圆的 get_path() 返回的内容与原始圆不同,这可以从下面代码的结果中看出。从附图中可以看出,原始的橙色圆圈与原始圆圈的get_path()中的蓝色圆圈完全不同。

import numpy as np
import matplotlib
from matplotlib.patches import Circle, Wedge, Polygon, Ellipse
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
import matplotlib.patches as matpatches


fig, ax = plt.subplots(figsize=(8, 8))
patches = []


circle = Circle((2, 2), 2)
patches.append(circle)

print patches[0].get_path()
print patches[0].get_verts()

polygon = matpatches.PathPatch(patches[0].get_path())
patches.append(polygon)


colors = 2*np.random.rand(len(patches))
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4)
p.set_array(np.array(colors))
ax.add_collection(p)

plt.axis([-10, 10, -10, 10])

plt.show()

fig.savefig('test.png')

contain2 = patches[0].get_path().contains_points([[0.5, 0.5], [1.0, 1.0]])
print contain2
contain3 = patches[0].contains_point([0.5, 0.5])
print contain3
contain4 = patches[0].contains_point([1.0, 1.0])
print contain4

最佳答案

圆的路径是单位圆,matplotlib 将其显示为具有您指定的圆心和半径的圆的方式是通过 2D 仿射变换。如果您想要转换路径,则需要同时获取路径和变换并将变换应用于路径。

# Create the initial circle
circle = Circle([2,2], 2);

# Get the path and the affine transformation
path = circle.get_path()
transform = circle.get_transform()

# Now apply the transform to the path
newpath = transform.transform_path(path)

# Now you can use this
polygon = matpatches.PathPatch(newpath)
patches.append(polygon)

关于python - get_path() 从 matplotlib.patches 返回一个 Circle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36636686/

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