【问题标题】:Missing polish characters in matplotlib.pyplot labels (regardless of using UTF-8) [duplicate]matplotlib.pyplot 标签中缺少波兰字符(无论使用 UTF-8)[重复]
【发布时间】:2016-12-04 08:00:58
【问题描述】:

我正在尝试使用matplotlib 库创建直方图。

我将编码设置为 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()

结果:

【问题讨论】:

标签: python matplotlib character-encoding


【解决方案1】:

根据 cmets - 是的,这是使用不同字体的问题。 工作代码:

#!/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()

【讨论】:

    猜你喜欢
    • 2018-09-06
    • 1970-01-01
    • 1970-01-01
    • 2018-06-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 2018-05-31
    相关资源
    最近更新 更多