mirror of
https://github.com/Estom/notes.git
synced 2026-04-04 11:27:59 +08:00
matplotlib & pandas
This commit is contained in:
40
Python/matplotlab/gallery/misc/bbox_intersect.md
Normal file
40
Python/matplotlab/gallery/misc/bbox_intersect.md
Normal file
@@ -0,0 +1,40 @@
|
||||
# 改变与盒子相交的线条的颜色
|
||||
|
||||
与矩形相交的线条用红色着色,而其他线条用蓝色线条留下。此示例展示了intersect_bbox函数。
|
||||
|
||||

|
||||
|
||||
```python
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.transforms import Bbox
|
||||
from matplotlib.path import Path
|
||||
|
||||
# Fixing random state for reproducibility
|
||||
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()
|
||||
```
|
||||
|
||||
## 下载这个示例
|
||||
|
||||
- [下载python源码: bbox_intersect.py](https://matplotlib.org/_downloads/bbox_intersect.py)
|
||||
- [下载Jupyter notebook: bbox_intersect.ipynb](https://matplotlib.org/_downloads/bbox_intersect.ipynb)
|
||||
Reference in New Issue
Block a user