import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
#解决中文乱码问题
plt.rcParams["font.sans-serif"] = ["SimHei"]
#解决负号显示问题
plt.rcParams["axes.unicode_minus"] = False
# 绘制正常文字
def plot_norm_font():
#设置图的大小
fig = plt.figure(figsize=(5,1.5))
#设置字在图片中显示的位置,宽和高的百分比,距离图左上角顶点的距离
text = fig.text(0.5,0.5,u"hello python\nmatplotlib\n中文不乱码",
ha="center",va="center",size=20)
#设置正常字体显示
text.set_path_effects([path_effects.Normal()])
plt.show()
# 绘制带有阴影效果的文字
def plot_shadow():
#设置字体带有阴影
text = plt.text(0.5,0.5,"matplotlib 中文",
path_effects=[path_effects.withSimplePatchShadow()])
#通过设置一个列表的path_effects,来间隔显示不同的效果
plt.plot([0,4,2,6],linewidth=5,color="blue",
path_effects=[path_effects.SimpleLineShadow(),path_effects.Normal()])
plt.show()
def plot_stoke():
fig = plt.figure(figsize=(5,2))
text = fig.text(0.5,0.5,"Python 大家好!",
color="white",ha="center",va="center",size=30)
text.set_path_effects([path_effects.Stroke(linewidth=5,foreground="blue"),path_effects.Normal()])
plt.show()
def plot_artText():
fig = plt.figure(figsize=(10,2))
t = fig.text(0.02,0.5,"matplotlib 艺术字",fontsize=75,weight=1000,va="center")
t.set_path_effects([
path_effects.PathPatchEffect(offset=(4,-4),hatch="xxxx",facecolor="grey"),
path_effects.PathPatchEffect(edgecolor="white",linewidth=1.1,facecolor="black")
])
plt.show()
plot_norm_font()
plot_shadow()
plot_stoke()
plot_artText()
正常的字
绘制带有阴影效果的文字
艺术字: