如何在Jupyter Notebook中绘制一个圆形位于长方形上方,同时确保它们都在同一绘图区域?请提供完整的Python代码示例,使用Matplotlib或类似库来实现这个图形展示。
时间: 2024-12-13 09:17:11 浏览: 20
在Jupyter Notebook中,你可以使用Matplotlib库的`Rectangle`和`Circle`函数来分别创建长方形和圆形,并将它们添加到同一个图形上下文中。以下是具体的Python代码示例:
```python
import matplotlib.pyplot as plt
import numpy as np
# 创建新的图形
fig, ax = plt.subplots()
# 创建长方形
left, bottom, width, height = -5, -5, 10, 20
rectangle = plt.Rectangle((left, bottom), width, height, edgecolor='black', facecolor='none') # 绘制长方形边框,不填色
ax.add_patch(rectangle)
# 创建圆形并调整位置使其位于长方形上方
radius = 7
circle = plt.Circle((0, rectangle.get_height() + 5), radius, color='blue') # 圆形中心位于长方形底部上方
ax.add_patch(circle)
# 调整坐标轴范围,显示整个图形
ax.set_xlim(left - 1, left + width + 1)
ax.set_ylim(bottom - 1, bottom + height + radius + 1)
# 显示图形
plt.show()
阅读全文
相关推荐
















