0

I'm trying to create a histogram using matplotlib library.

I set encoding to UTF-8, but some of the polish characters (not all, only some) are missing. I've tried to play around encode and decode, but without success.

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

Result: sampleHist.png - some polish chatacters are not visible

matandked
  • 1,527
  • 4
  • 26
  • 51
  • http://stackoverflow.com/questions/13338550/typing-greek-letters-etc-in-python-plots – jkr Dec 04 '16 at 08:22
  • The encoding is fine. But you are using a font that doesn't contain those characters. The duplicate question explains how to change the font used in matlabplot. – Håken Lid Dec 04 '16 at 10:15

1 Answers1

1

According to comments - yes it was a matter of using different font. Working code:

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

matandked
  • 1,527
  • 4
  • 26
  • 51