python中怎么使用ttf使用?

###python中使用ttf
###大佬们,这该怎么办纯萌新一个

img

img

该回答引用ChatGPT
请参考下面的方法,如果可行还请点击 采纳,感谢!
使用ttf字体文件在python程序中显示文本,可以使用Matplotlib库中的font_manager。


import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

# 指定ttf文件的路径
font_path = "path/to/font.ttf"
# 创建字体属性
font_prop = FontProperties(fname=font_path)

# 在图形中使用字体
plt.title("标题", fontproperties=font_prop)
plt.xlabel("x轴", fontproperties=font_prop)
plt.ylabel("y轴", fontproperties=font_prop)
plt.plot([1,2,3,4])
plt.show()

如果你要在文本中使用字体,可以使用matplotlib.text模块。示例代码如下:

import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import matplotlib.text as text

# 指定ttf文件的路径
font_path = "path/to/font.ttf"
# 创建字体属性
font_prop = FontProperties(fname=font_path)

# 在图形中使用字体
plt.title("标题", fontproperties=font_prop)
plt.xlabel("x轴", fontproperties=font_prop)
plt.ylabel("y轴", fontproperties=font_prop)
text.Text(0, 0, "文本", fontproperties=font_prop)
plt.show()