2016-12-04 54 views
0

我試圖使用matplotlib庫創建直方圖。缺少matplotlib.pyplot標籤中的波蘭語字符(不管使用UTF-8)

我將編碼設置爲UTF-8,但某些波蘭字符(不是全部,只有一些)缺失。 我試過玩encodedecode,但沒有成功。

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 

import matplotlib.pyplot as plt 

#sampleData 
data=[[ 0.13837092, 0.14755699, 0.17719047, 0.1998352 ], 
     [ 0.19318216, 0.15863521, 0.17136143, 0.18979461]] 

n, bins, patches = plt.hist(data, facecolor='green') 

# I've tried also adding: .encode('UTF8') and .decode('UTF8'), but without success 
plt.xlabel("Wartości ą, ć, ę, ł, ń, ó, ś, ź, ż.") 
plt.ylabel("Prawdopodobieństwo ą, ć, ę, ł, ń, ó, ś, ź, ż.") 
plt.title("Tytuł ą, ć, ę, ł, ń, ó, ś, ź, ż.") 
plt.grid(True) 
plt.savefig("sampleHist.png") 
plt.clf() 

結果: sampleHist.png - some polish chatacters are not visible

+0

http://stackoverflow.com/questions/13338550/typing-greek-letters-etc-in-python-plots – Jakub

+0

的編碼是好​​的。但是您使用的字體不包含這些字符。重複的問題解釋瞭如何更改matlabplot中使用的字體。 –

回答

0

根據意見 - 是的,它是用不同字體的問題。 工作代碼:

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

import matplotlib 
import matplotlib.pyplot as plt 

# Set font which contains polish characters: 
matplotlib.rc('font', family='Arial') 


#sampleData 
data=[[ 0.13837092, 0.14755699, 0.17719047, 0.1998352 ], 
     [ 0.19318216, 0.15863521, 0.17136143, 0.18979461]] 

n, bins, patches = plt.hist(data, facecolor='green') 

plt.xlabel(u"Wartości ą, ć, ę, ł, ń, ó, ś, ź, ż.") 
plt.ylabel(u"Prawdopodobieństwo ą, ć, ę, ł, ń, ó, ś, ź, ż.") 
plt.title(u"Tytuł ą, ć, ę, ł, ń, ó, ś, ź, ż.") 
plt.grid(True) 
plt.savefig("sampleHist.png") 
plt.clf() 

sampleHist.png