python - 在python中向贝塞尔图添加多条线

标签 python matplotlib bezier

这是我之前的 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()

enter image description here

要继续添加更多曲线,只需确保将 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)

enter image description here

关于python - 在python中向贝塞尔图添加多条线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492907/

相关文章:

python - 枚举一个值,直到达到 pandas 中的特定值

python - matplotlib 中的极地等高线图 - 最好的(现代)方法?

python - Matplotlib:事后更改颜色图

svg - 如何在SVG中使用贝塞尔路径近似半余弦曲线?

audio - 是否可以基于x值获得三次贝塞尔曲线的y值?

c++ - 贝塞尔曲线评估

python pptx 在饼图中显示类别值

python - 在python中将日期时间转换为时间

python - 运行函数时导入的模块变为 None

python - 如何在 Python 中使用 Pandas 绘制条形图以比较具有多个变量的多个系统