这是我之前的 post 的延续.
我想出了一种使用 Bezier 包绘制 x 轴和 y 轴之间的弧线的方法。
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()
手册中不清楚,但我想将所有曲线添加到单个图中。上面的代码生成两个单独的图。有什么帮助吗?
最佳答案
当您调用curve.plot(100)
时,它会返回matplotlib
Axes
它在其上创建绘图的对象。你只需要捕获那个 handle :
ax = curve.plot(100)
如果将该 Axes
对象提供给后续的 curve2.plot
调用,则两条曲线应绘制在同一轴上。
curve2.plot(100, ax=ax)
您可以看到documentation here 。这是完整的脚本:
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()
要继续添加更多曲线,只需确保将 ax=ax
添加到每个 .plot
命令即可:
nodes2 = np.asfortranarray([
[0 , 30.0],
[1500, 30],
[3000 , 0],
])
print nodes2
curve3 = bezier.Curve(nodes2, degree=2)
curve3.plot(100, ax=ax)
关于python - 在python中向贝塞尔图添加多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492907/