改变与盒子相交的线条的颜色
与矩形相交的线条用红色着色,而其他线条用蓝色线条留下。此示例展示了intersect_bbox函数。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| import numpy as np import matplotlib.pyplot as plt from matplotlib.transforms import Bbox from matplotlib.path import Path
np.random.seed(19680801)
left, bottom, width, height = (-1, -1, 2, 2) rect = plt.Rectangle((left, bottom), width, height, facecolor="#aaaaaa")
fig, ax = plt.subplots() ax.add_patch(rect)
bbox = Bbox.from_bounds(left, bottom, width, height)
for i in range(12): vertices = (np.random.random((2, 2)) - 0.5) * 6.0 path = Path(vertices) if path.intersects_bbox(bbox): color = 'r' else: color = 'b' ax.plot(vertices[:, 0], vertices[:, 1], color=color)
plt.show()
|
下载这个示例