解决matplotlib 堆叠图中文乱码

本文指导如何在Matplotlib中安装缺失的SimHei字体,并演示了如何设置图表的字体样式。通过`rebuild()`函数和配置参数,确保中文显示正确,包括设置中文标签和坐标轴。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装缺失的字体,最后记住 _rebuild() #reload一下

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

_rebuild() #reload一下


matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
# matplotlib.font_manager.FontProperties(fname=r"/home/romulus/Downloads")
# matplotlib.font_manager.FontProperties(directory=r"/home/romulus/Downloads/SimHei.ttf")
#
# import matplotlib
# matplotlib.rcParams['font.family'] = 'sans-serif'
# matplotlib.rcParams['font.sans-serif'] = 'NSimSun,Times New Roman'# 中文设置成宋体,除此之外的字体设置成New Roman
#



label_list = ['2014', '2015', '2016', '2017']
num_list1 = [20, 30, 15, 35]
num_list2 = [15, 30, 40, 20]
x = range(len(num_list1))
rects1 = plt.bar(left=x, height=num_list1, width=0.45, alpha=0.8, color='red', label="一部门")
rects2 = plt.bar(left=x, height=num_list2, width=0.45, color='green', label="二部门", bottom=num_list1)
plt.ylim(0, 80)
plt.ylabel("num")
# plt.ylabel("数量")
plt.xticks(x, label_list)
plt.xlabel("year")
# plt.xlabel("年份")
plt.title("XXcompany")
# plt.title("某某公司")

plt.legend()
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值