gthm - 3 years ago 221
Python Question

# Add multiple lines to bezier plot in python

Its a continuation of my previous post.

I have figured one way to plot the arcs between x and y axis using Bezier package.

``````import bezier
import numpy as np
import matplotlib.pyplot as plt

nodes = np.asfortranarray([
[0  , 10.0],
[500, 10],
[1000  , 0],
])

print nodes
curve = bezier.Curve(nodes, degree=2)
curve.plot(100)

nodes1 = np.asfortranarray([
[0  , 20.0],
[1000, 20],
[2000  , 0],
])

print nodes1
curve2 = bezier.Curve(nodes1, degree=2)
curve2.plot(100)

plt.show()
``````

Its not clear from the manual, but I want to add the all the curves to a single plot. The above code generates two separate plots. Any help ?

When you call `curve.plot(100)`, it returns the `matplotlib` `Axes` object that it created the plot on. You just need to grab that handle:

``````ax = curve.plot(100)
``````

If you feed that `Axes` object to the subsequent `curve2.plot` call, both curves should be plotted on the same axes.

``````curve2.plot(100, ax=ax)
``````

You can see the documentation here. Here's the complete script:

``````import bezier
import numpy as np
import matplotlib.pyplot as plt

nodes = np.asfortranarray([
[0  , 10.0],
[500, 10],
[1000  , 0],
])

print nodes
curve = bezier.Curve(nodes, degree=2)
ax = curve.plot(100)

nodes1 = np.asfortranarray([
[0  , 20.0],
[1000, 20],
[2000  , 0],
])

print nodes1
curve2 = bezier.Curve(nodes1, degree=2)
curve2.plot(100, ax=ax)

plt.show()
``````

To keep adding more curves, just make sure you add `ax=ax` to each `.plot` command:

``````nodes2 = np.asfortranarray([
[0  , 30.0],
[1500, 30],
[3000  , 0],
])

print nodes2
curve3 = bezier.Curve(nodes2, degree=2)
curve3.plot(100, ax=ax)
``````

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download