matplotlib legend字体
时间: 2023-09-08 07:08:28 浏览: 142
你可以使用 `matplotlib` 中的 `legend` 函数的 `prop` 参数来设置字体样式。例如,要将字体设置为宋体,大小为12,可以使用以下代码:
```python
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
# 设置字体
font = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)
# 绘制图像
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
# 添加图例
plt.legend(["数据"], prop=font)
# 显示图像
plt.show()
```
在这个例子中,我们使用 `FontProperties` 类来指定字体样式,其中 `fname` 参数指定了字体文件路径,`size` 参数指定了字体大小。然后,我们将 `FontProperties` 对象传递给 `legend` 函数的 `prop` 参数,以应用字体样式。
相关问题
matplotlib设置legend字体
可以使用以下代码设置legend字体:
```
import matplotlib.pyplot as plt
plt.rcParams['legend.fontsize'] = 12
plt.rcParams['legend.fontfamily'] = 'Times New Roman'
```
其中,`legend.fontsize`可以设置字体大小,`legend.fontfamily`可以设置字体类型。
matplotlib legend加粗
可以使用 `set_weight()` 方法来设置 legend 的字体粗细。示例代码如下:
```python
import matplotlib.pyplot as plt
# 绘制图形
x = [1, 2, 3]
y = [4, 5, 6]
plt.plot(x, y)
# 添加 legend
legend = plt.legend(['line'], loc='best')
legend.texts[0].set_weight('bold')
plt.show()
```
其中,`legend.texts` 表示获取 legend 中的第一个文本对象,然后使用 `set_weight()` 方法设置其字体粗细为粗体。
阅读全文
相关推荐
















