python在绘制matplotlib.table时设置表格里面显示中文字体
时间: 2024-03-21 10:37:48 浏览: 65
要在matplotlib.table中设置表格里面显示中文字体,可以使用以下代码:
```python
import matplotlib.pyplot as plt
import matplotlib.font_manager as fm
import numpy as np
# 设置中文显示
plt.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
# 生成数据
data = np.random.rand(4, 2)
columns = ['第一列', '第二列']
rows = ['第一行', '第二行', '第三行', '第四行']
# 设置字体
font = fm.FontProperties(fname='path/to/font.ttf', size=14)
# 绘制表格
fig, ax = plt.subplots()
ax.axis('off')
table = ax.table(cellText=data, colLabels=columns, rowLabels=rows, loc='center', cellLoc='center')
table.auto_set_font_size(False)
table.set_fontsize(14)
for cell in table._cells:
table._cells[cell].set_text_props(fontproperties=font)
plt.show()
```
其中,`plt.rcParams['font.sans-serif'] = ['SimHei']`指定了默认字体为中文字体SimHei,`font = fm.FontProperties(fname='path/to/font.ttf', size=14)`可以设置自定义字体,`table._cells[cell].set_text_props(fontproperties=font)`设置每个单元格的字体。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![ttc](https://img-home.csdnimg.cn/images/20250102104920.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)