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

import numpy as npimport matplotlib.pyplot as pltfrom matplotlib.transforms import Bboxfrom matplotlib.path import Path# Fixing random state for reproducibilitynp.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.0path = Path(vertices)if path.intersects_bbox(bbox):color = 'r'else:color = 'b'ax.plot(vertices[:, 0], vertices[:, 1], color=color)plt.show()
