gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:14:37 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com