使matplot显示支持中文和负号

输入下面的代码::

python 复制代码
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.set_title('这是一个测试标题')
ax.plot([-1, -2, -3], [4, 5, 6])
plt.show()

显示如下:

中文和负号都显示不出来。

解决方法:

python 复制代码
import matplotlib
print(matplotlib.matplotlib_fname())

比如印出的结果是:

python 复制代码
C:\Users\DY\.conda\envs\yolo\lib\site-packages\matplotlib\mpl-data\matplotlibrc

用文本编辑器打开这个文件:

查找到#font.family这一行:

去掉#号将其修改为font.family: SimHei

同样的方法,查找axes.unicode_minus:True将其修改为False:

再次运行测试代码:

正常显示。

Ubuntu系统可能没有雅黑字体,使用

python 复制代码
fc-list :lang=zh

列出所有支持中文的字体,然后在字体中选择一种,例如显示为:

python 复制代码
/usr/share/fonts/truetype/arphic/uming.ttc: AR PL UMing TW MBE:style=Light

将AR PL UMing TW MBE复制下来,设置:

python 复制代码
font.family:  AR PL UMing TW MBE

即可。

相关推荐
用户77833661321114 小时前
用 React Hook 封装搜索数据:useSerp 的防抖、缓存与错误处理
python·api
65岁退休Coder18 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
ikun_文21 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes21 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明21 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn21 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困21 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao1 天前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼1 天前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君1 天前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python