10分钟
path effect
matplotlib
的patheffects
模块提供了一些函数来绘制path effect
,该模块还定义了很多effect
类。可以应用path effect
的Artist
有:Patch
、Line2D
、Collection
以及Text
。每个Artist
的path effect
可以通过.set_path_effects()
方法控制,其参数是一个可迭代对象,迭代的结果是AbstractPathEffect
实例;也可以通过Artist
构造函数的path_effects=
关键字参数控制。 注意:effect
类的关键字参数比如背景色、前景色等与Artist
不同。因为这些effect
类是更低层次的操作。- 所有的
effect
类都继承自matplotlib.patheffects.AbstractPathEffect
类。AbstractPathEffect
的子类要覆盖AbstractPathEffect
类的.draw_path(...)
方法。AbstractPathEffect
类的构造函数有个offset
关键字参数,表示effect
偏移(默认为(0,0)
) - 最简单的
effect
是normal effect
,它是matplotlib.patheffects.Normal
类。它简单的绘制Artist
,不带任何effect
。 如:
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
fig = plt.figure(figsize=(5, 1.5))
text = fig.text(0.5, 0.5, 'Hello path effects world!\nThis is the normal path effect.',
ha='center', va='center', size=20)
text.set_path_effects([path_effects.Normal()])
plt.show()
学员评价