簡體   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