1

I am using bezier to generate a Bezier curve. Below is the sample code:

import bezier
nodes = np.array([
     [0.0 ,  0.0],
     [0.25,  2.0],
     [0.5 , -2.0],
     [0.75,  2.0],
     [1.0 ,  0.3],
 ])
curve = bezier.Curve.from_nodes(nodes)

import matplotlib.pyplot as plt
curve.plot(num_pts=256)
plt.show()

Please see below the generated plot. enter image description here

I want to save this trajectory (let say to a file). In other words, I want to save x,y value of each point in this curve. I was expecting it to return a numpy array but it is not. kindly suggest.

ravi
  • 6,140
  • 18
  • 77
  • 154

1 Answers1

2
ax = plt.gca() 
line = ax.lines[0]
x = line.get_xdata()
y = line.get_ydata()
xy = np.vstack([x,y]).transpose()
np.save('bezier_data', xy)

I worked it out from the answer here. Those xvals and yvals are NumPy arrays you can put them into one if you need it.

Might help to not execute plt.show() before these lines.

cardamom
  • 6,873
  • 11
  • 48
  • 102