matplotlib 高阶之patheffect (阴影,强调)

博客介绍了在Python中通过path修饰Artist,利用set_path_effects添加阴影,使Artist更有意思。还提到利用stroke可让Artist变得突出,此外还给出了相关内容的转载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们可以通过path来修饰Artist, 通过set_path_effects

import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects

fig = plt.figure(figsize=(5, 1.5))
ax = fig.add_axes([0, 0, 1, 1])
ax.set_visible(False)
text = fig.text(0.5, 0.5, 'Hello path effects world!\nThis is the normal '
                          'path effect.\nPretty dull, huh?',
                ha='center', va='center', size=20)
text.set_path_effects([path_effects.Normal()])
plt.show()

在这里插入图片描述

添加阴影

比之Normal更加有意思的是添加阴影

import matplotlib.patheffects as path_effects

text = plt.text(0.5, 0.5, 'Hello path effects world!',
                path_effects=[path_effects.withSimplePatchShadow()])

plt.plot([0, 3, 2, 5], linewidth=5, color='blue',
         path_effects=[path_effects.SimpleLineShadow(),
                       path_effects.Normal()])
plt.show()

在这里插入图片描述

注意到,上面的例子中,我们用path_effects属性,分别为text和line2D添加了阴影

使Artist变得突出

想要完成这一步不敌,利用stroke是不错的选择,虽然我不知道这是什么东西

fig = plt.figure(figsize=(7, 1))
ax = fig.add_axes([0, 0, 1, 1])
ax.set_visible(False)
text = fig.text(0.5, 0.5, 'This text stands out because of\n'
                          'its black border.', color='white',
                          ha='center', va='center', size=30)
text.set_path_effects([path_effects.Stroke(linewidth=3, foreground='black'),
                       path_effects.Normal()])
plt.show()

在这里插入图片描述

更多效果

fig = plt.figure(figsize=(8, 1))
ax = fig.add_axes([0, 0, 1, 1])
ax.set_visible(False)
t = fig.text(0.02, 0.5, 'Hatch shadow', fontsize=75, weight=1000, va='center')
t.set_path_effects([path_effects.PathPatchEffect(offset=(4, -4), hatch='xxxx',
                                                 facecolor='gray'),
                    path_effects.PathPatchEffect(edgecolor='white', linewidth=1.1,
                                                 facecolor='black')])
plt.show()

在这里插入图片描述

转载于:https://www.cnblogs.com/MTandHJ/p/10951696.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值