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

2
  • stackoverflow.com/questions/13338550/… Commented Dec 4, 2016 at 8: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. Commented Dec 4, 2016 at 10:15

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.