为文本添加描边效果
我们可以使用 withStroke
路径效果为文本添加描边效果。在这个例子中,我们将为绘图中的文本注释添加描边效果。
## 创建绘图并添加带有描边效果的文本注释
fig, ax = plt.subplots()
ax.imshow(arr)
txt = ax.annotate("test", (1., 1.), (0., 0),
arrowprops=dict(arrowstyle="->",
connectionstyle="angle3", lw=2),
size=20, ha="center",
path_effects=[patheffects.withStroke(linewidth=3,
foreground="w")])
txt.arrow_patch.set_path_effects([
patheffects.Stroke(linewidth=5, foreground="w"),
patheffects.Normal()])
## 添加带有描边效果的网格
pe = [patheffects.withStroke(linewidth=3,
foreground="w")]
ax.grid(True, linestyle="-", path_effects=pe)
plt.show()