Home > OS >  How to increase the number of vertices?
How to increase the number of vertices?

Time:12-23

I need a parametric form for a matplotlib.path.Path. So I used the .vertices attribute, and it works fine except that the number of points given is too low for the use I want. Here is a code to illustrate :

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

How to increase the number of points (red) to fetch better the path (green)? Or, how can I increase the len of path?

Thanks in advance!

CodePudding user response:

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

CodePudding user response:

It's not ideal, but you can just loop over many shorter arcs, e.g.,:

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