如何增加顶点数?

2024-03-01

我需要一个参数化形式matplotlib.path.Path。所以我用了.vertices属性,它工作得很好,只是给出的点数对于我想要的用途来说太低了。这是一段代码来说明:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.patches as mpat

fig, ax = plt.subplots()
ax.set(xlim=(-6, 6), ylim=(-6, 6))


# generate a circular path
circle = mpat.Arc((0, 0), 10, 10, theta1=20, theta2=220, color='green')
path = circle.get_transform().transform_path(circle.get_path()).cleaned().vertices[:-3]  # get_path is not enough because of the transformation, so we apply to the path the same transformation as the circle has got from the identity circle
ax.add_patch(circle)

# plot path vertices
plt.scatter(x=path[:, 0], y=path[:, 1], color='red', s=2)

shape = len(path)

plt.show()

如何增加点数(红色)以更好地获取路径(绿色)?或者,我怎样才能增加len of path?

提前致谢!


这并不理想,但您可以循环许多较短的弧,例如:

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.patches as mpat

fig, ax = plt.subplots()
ax.set(xlim=(-6, 6), ylim=(-6, 6))


# generate a circular path
dr = 30  # change this to control the number of points
path = np.empty((0, 2))

for i in range(20, 221, dr):
    circle = mpat.Arc((0, 0), 10, 10, theta1=i, theta2=(i + dr), color='green')
    tmppath = circle.get_transform().transform_path(circle.get_path()).cleaned().vertices[:-1]
    path = np.vstack((path, tmppath[:, 0:2]))

    ax.add_patch(circle)

# plot path vertices
plt.scatter(x=path[:, 0], y=path[:, 1], color='red', s=2)

plt.show()

Update

另一种选择是破解Arc https://matplotlib.org/stable/api/_as_gen/matplotlib.patches.Arc.html类,以便在初始化 https://github.com/matplotlib/matplotlib/blob/v3.6.2/lib/matplotlib/patches.py#L1898-L2143, the Path.arc https://github.com/matplotlib/matplotlib/blob/dc0328fb9c1c8817b07a5121a106a06be3571433/lib/matplotlib/path.py#L926采取在n关键词。例如。,

from matplotlib.patches import Path, Arc

class NewArc(Arc):
    def __init__(self, xy, width, height, angle=0.0, theta1=0.0, theta2=360.0, n=30, **kwargs):
        """
        An Arc class with the n keyword argument
        """
    
        fill = kwargs.setdefault('fill', False)
        if fill:
            raise ValueError("Arc objects can not be filled")

        super().__init__(xy, width, height, angle=angle, **kwargs)

        self.theta1 = theta1
        self.theta2 = theta2
        (self._theta1, self._theta2, self._stretched_width,
         self._stretched_height) = self._theta_stretch()

        # add in the n keyword here
        self._path = Path.arc(self._theta1, self._theta2, n=n)

然后可以使用原始代码(没有 for 循环)并增加点数,例如,

from matplotlib import pyplot as plt

fig, ax = plt.subplots()
ax.set(xlim=(-6, 6), ylim=(-6, 6))

# generate a circular path using NewArc and the n keyword
circle = NewArc((0, 0), 10, 10, theta1=20, theta2=220, n=40, color='green')
path = circle.get_transform().transform_path(circle.get_path()).cleaned().vertices[:-3]
ax.add_patch(circle)

# plot path vertices
ax.scatter(x=path[:, 0], y=path[:, 1], color='red', s=2)

fig.show()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何增加顶点数? 的相关文章

随机推荐