Closed
Description
Try the following on Python2.7 and mpl 1.4.3 (1.3.1 is not affected), has been tested on Win7/32 bit:
# -*- coding: utf-8 -*-
"""mpl issue with list of hist legend labels in unicode"""
from matplotlib import pyplot as plt
data1 = [4, 4, 5, 5, 5, 5, 5, 5, 6]
data2 = [0, 1, 1, 1, 1, 1, 1, 2, 2]
label1 = u'pure ascii'
label2 = u'很漂亮, römän chäráctèrs'
# show unicode label, works
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.hist(data2, label=label2)
ax.legend()
# show unicode labels in list
fig = plt.figure()
ax = fig.add_subplot(111)
res = ax.hist([data1, data2], label=[label1, label2])
ax.legend()
plt.show()
The first histogram will show unicode characters (at least the roman ones) …
… while the second one will throw an error:
res = ax.hist([data1, data2], label=[label1, label2])
File "C:\Users\xxx\venv\ipython\lib\site-packages\matplotlib\axes\_axes.py", line 5883, in hist
labels = [str(lab) for lab in label]
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
in _axes.Axes.hist
:
if label is None:
labels = [None]
elif is_string_like(label):
labels = [label]
else:
labels = [str(lab) for lab in label]
there is an explicit str
which will of course not work on unicode. In 1.3.1 it was just assuming this was string-like and concatenated it.
Proposal:
- Use one of the
six
methods or another python future - Do not use str at all and have labels to be string-like as before
Metadata
Metadata
Assignees
Labels
No labels