python画图找两组数组交点
时间: 2023-10-22 10:05:15 浏览: 154
你可以使用Python中的matplotlib库来绘制图形并找到两组数组的交点。下面是一个简单的示例代码:
```python
import matplotlib.pyplot as plt
# 定义两组数组
x1 = [1, 2, 3, 4, 5]
y1 = [2, 4, 6, 8, 10]
x2 = [3, 4, 5, 6, 7]
y2 = [5, 4, 3, 2, 1]
# 绘制两组数组的散点图
plt.scatter(x1, y1, color='red', label='Array 1')
plt.scatter(x2, y2, color='blue', label='Array 2')
# 查找交点
intersection = [] # 存储交点的坐标
for i in range(len(x1)):
for j in range(len(x2)):
if x1[i] == x2[j] and y1[i] == y2[j]:
intersection.append((x1[i], y1[i]))
# 绘制交点
if intersection:
x_int, y_int = zip(*intersection)
plt.scatter(x_int, y_int, color='green', label='Intersection')
# 添加图例和标题
plt.legend()
plt.title('Intersection of Two Arrays')
# 展示图形
plt.show()
```
这段代码会绘制两组数组的散点图,并在图中标出交点。你可以根据实际需要修改数组的数值和样式。
阅读全文