Python绘画蓝色填充粉色框的箭头
时间: 2024-10-18 14:18:21 浏览: 41
在Python中,可以使用matplotlib库来进行绘图,包括添加填充颜色和绘制箭头。首先,你需要安装matplotlib,如果还没有安装,可以使用pip install matplotlib命令。
为了创建一个蓝色填充的粉色框,并在其内部画出箭头,你可以这样做:
```python
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from matplotlib.path import Path
from matplotlib.projections import register_projection
from mpl_toolkits.axes_grid1.inset_locator import zoomed_inset_axes
# 创建自定义投影
class ArrowRectangle(Axes):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ax = self.figure.add_subplot(axes_class=self)
def fill_between(self, x, y, facecolor='blue', alpha=0.5):
path = Path(zip(x, y))
patch = PathPatch(path, facecolor=facecolor, alpha=alpha)
self.add_patch(patch)
def draw_arrow(self, start, end, color='pink'):
ax = self.figure.add_subplot(axes_class=Axes)
ax.arrow(start[0], start[1], end[0] - start[0], end[1] - start[1], head_width=0.1, length_includes_head=True, color=color)
register_projection(ArrowRectangle)
# 使用自定义箭头矩形
fig = plt.figure()
ax = fig.add_subplot(projection=ArrowRectangle)
x = [0, 1, 1, 0, 0]
y = [0, 0, 1, 1, 0]
ax.fill_between(x, y, facecolor='blue')
start = (0.3, 0.4)
end = (0.7, 0.6)
ax.draw_arrow(start, end, color='pink')
# 显示图形
plt.show()
阅读全文