python - 如何在 matplotlib 中使用 unicode 符号?

标签 python unicode matplotlib

import matplotlib.pyplot as pyplot

pyplot.figure()
pyplot.xlabel(u"\u2736")
pyplot.show()

这是我可以创建的最简单的代码来说明我的问题。轴标签符号本应是一个六角星,但它显示为一个方框。我如何更改它以便显示星星?我试过添加评论:

#-*- coding: utf-8 -*-

就像之前建议的答案一样,但它没有用,使用 matplotlib.rcmatplotlib.rcParams 也没有用。帮助将不胜感激。

最佳答案

您需要一种具有给定 unicode 字符的字体,STIX字体应包含星号。您需要找到或下载 STIX 字体,当然任何其他带有给定符号的 ttf 文件都应该没问题。

import matplotlib.pyplot as pyplot
from matplotlib.font_manager import FontProperties

if __name__ == "__main__":
    pyplot.figure() 
    prop = FontProperties()
    prop.set_file('STIXGeneral.ttf')
    pyplot.xlabel(u"\u2736", fontproperties=prop)
    pyplot.show()

关于python - 如何在 matplotlib 中使用 unicode 符号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905938/

相关文章:

perl - 在 Perl 中将 UTF8 字符串转换为数值

javascript - 使用 PHP 对 unicode 字符进行转义

python - Matplotlib:极坐标等高线图颜色条上的固定边界

python - Matplotlib 等同于 ggplot geom_ribbon?

python - python中如何根据需求生成JWT

python - GRPC:远程错误和消息中缺少参数

php - 将多个正则表达式合并为一个

python - 如何将绘制的线保持在补丁下?

python - 使用groupby将 Pandas 数据框转换为Python中的嵌套字典

python - Flask 开发服务器重新加载时向前端发送消息