【问题标题】:get_path() return of a Circle from matplotlib.patchesget_path() 从 matplotlib.patches 返回一个 Circle
【发布时间】:2016-04-15 10:26:25
【问题描述】:

有谁知道来自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

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-04-14
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 2021-11-07
      • 2012-02-03
      • 2018-07-31
      • 2014-10-21
      相关资源
      最近更新 更多