繁体   English   中英

在Matplotlib中使用Unicode希腊字母

[英]Use Unicode Greek letters in Matplotlib

在MATLAB中,如果在轴标签中键入“ \\ alpha”,它将呈现“纯文本”希腊字符alpha,而无需Latex。 我希望对Matplotlib的Pyplot做同样的事情。

以下如何在matplotlib中使用unicode符号? ,我尝试了以下方法:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties

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

x = np.linspace(-1,1,21)

plt.figure()
plt.plot(x,x)
plt.xlabel(u"\u03b1")        # Attempt at creating an axis label with a Unicode alpha
# plt.xlabel(r'$\alpha$')     # I'm aware that Latex is possible
plt.show()

但是,我收到一条错误消息,结尾为

IOError: [Errno 2] No such file or directory: 'STIXGeneral.ttf'

如果我省略第3-10行,则不会收到错误消息,但是该图显示的是正方形而不是希腊字母alpha:

在此处输入图片说明

使用Matplotlib可以做到这一点吗? (我知道可以显示Latex,但希望使用“纯文本”选项)。

通常,我认为这是由于您正在Windows中运行它,对吗?

Windows ,您需要指定字体目录的确切位置,而不仅仅是文件名。

安装了font之后,这就是我的工作 我转到control panel-> font以找到该control panel-> font的确切位置,在本例中为'C:\\Windows\\Fonts\\STIXMath-Regular.otf' 然后我将代码更改为

if __name__ == "__main__":
    plt.figure()
    prop = FontProperties(fname='C:\Windows\Fonts\STIXMath-Regular.otf')
#    prop.set_file('STIXMath-Regular.otf')
    plt.xlabel(u"\u2736", fontproperties=prop)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM