[英]Use Unicode Greek letters in Matplotlib
In MATLAB, if you type "\\alpha" in an axis label it will render a 'plain-text' Greek character alpha, without the need for Latex. 在MATLAB中,如果在轴标签中键入“ \\ alpha”,它将呈现“纯文本”希腊字符alpha,而无需Latex。 I hoping to do the same with Matplotlib's Pyplot.
我希望对Matplotlib的Pyplot做同样的事情。
Following How to use unicode symbols in matplotlib? 以下如何在matplotlib中使用unicode符号? , I've tried the following:
,我尝试了以下方法:
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()
However, I get an error message with ends with 但是,我收到一条错误消息,结尾为
IOError: [Errno 2] No such file or directory: 'STIXGeneral.ttf'
If I omit lines 3-10, I don't get an error message but the plot shows a square instead of a Greek letter alpha: 如果我省略第3-10行,则不会收到错误消息,但是该图显示的是正方形而不是希腊字母alpha:
Is it possible to do this with Matplotlib? 使用Matplotlib可以做到这一点吗? (I'm aware of the possibility to display Latex, but would prefer a 'plain text' option).
(我知道可以显示Latex,但希望使用“纯文本”选项)。
Generally, I think it is due to that your are running it in Windows
, Am I right? 通常,我认为这是由于您正在
Windows
中运行它,对吗?
In Windows
, you will need to specify the exact location of your font directory, instead of just the file name. 在
Windows
,您需要指定字体目录的确切位置,而不仅仅是文件名。
Here is what I do, after the installation of the font . 安装了font之后,这就是我的工作 。 I went to the
control panel-> font
to find the exact location of the font, which is 'C:\\Windows\\Fonts\\STIXMath-Regular.otf'
in my case. 我转到
control panel-> font
以找到该control panel-> font
的确切位置,在本例中为'C:\\Windows\\Fonts\\STIXMath-Regular.otf'
。 Then I just change the code to be 然后我将代码更改为
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.