在 Matplotlib 中为圆/块的交叉点着色
pythonserver side programmingprogramming更新于 2025/8/7 16:12:17
要在 Matplotlib 中为圆/块的交叉点着色,我们可以采取以下步骤 −
- 设置图形大小并调整子图之间和周围的填充。
- 创建 a 和 b 点。
- 从两个点 a 和 b 获取左、右和中间区域。
- 使用 gca() 方法获取当前轴
- 添加具有不同颜色和部分的块。
- 设置 X 和 Y 轴比例。
- 设置相等的纵横比。
- 关闭轴。
- 要显示图形,请使用 show()方法。
示例
import shapely.geometry as sg
import matplotlib.pyplot as plt
import descartes
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
a = sg.Point(-.5, 0).buffer(1.)
b = sg.Point(0.5, 0).buffer(1.)
left = a.difference(b)
right = b.difference(a)
middle = a.intersection(b)
ax = plt.gca()
ax.add_patch(descartes.PolygonPatch(left, fc='b', ec='k'))
ax.add_patch(descartes.PolygonPatch(right, fc='r', ec='k'))
ax.add_patch(descartes.PolygonPatch(middle, fc='g', ec='k'))
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_aspect('equal')
ax.axis('off')
plt.show()
输出

相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

