python 设置字体
时间: 2023-07-01 13:13:49 浏览: 86
在 Python 中,可以使用 matplotlib 库来设置图形、文本等的字体。具体步骤如下:
1. 导入 matplotlib 和 FontProperties:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
```
2. 创建一个 FontProperties 对象,设置需要使用的字体:
```python
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=14)
```
其中,`fname` 参数是字体文件的路径,可以根据自己的需要修改。
3. 使用 `rcParams` 函数设置全局字体,以及使用 `fontproperties` 参数设置单个文本的字体:
```python
plt.rcParams['font.family'] = 'sans-serif' # 设置全局字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 设置全局字体为黑体
plt.title('标题', fontproperties=font) # 设置文本字体为 font
plt.xlabel('横坐标', fontproperties=font) # 设置文本字体为 font
plt.ylabel('纵坐标', fontproperties=font) # 设置文本字体为 font
```
这样就可以将图形、文本等的字体设置为指定的字体了。