Skip to content

Commit be94b91

Browse files
committed
BUG: fix plot unicode+MultiIndex, close #1685
1 parent 5771612 commit be94b91

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

pandas/tests/test_graphics.py

+16
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,22 @@ def test_plot(self):
190190
index=MultiIndex.from_tuples(tuples))
191191
_check_plot_works(df.plot, use_index=True)
192192

193+
# unicode
194+
index = MultiIndex.from_tuples([(u'\u03b1', 0),
195+
(u'\u03b1', 1),
196+
(u'\u03b2', 2),
197+
(u'\u03b2', 3),
198+
(u'\u03b3', 4),
199+
(u'\u03b3', 5),
200+
(u'\u03b4', 6),
201+
(u'\u03b4', 7)], names=['i0', 'i1'])
202+
columns = pandas.MultiIndex.from_tuples([('bar', u'\u0394'),
203+
('bar', u'\u0395')], names=['c0', 'c1'])
204+
df = pandas.DataFrame(np.random.randint(0, 10, (8, 2)),
205+
columns=columns,
206+
index=index)
207+
df.plot(title=u'\u03A3')
208+
193209
@slow
194210
def test_plot_xy(self):
195211
df = tm.makeTimeDataFrame()

pandas/tools/plotting.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ def normalize(series):
207207
line = ax.scatter(to_plot[class_][0],
208208
to_plot[class_][1],
209209
color=random_color(class_),
210-
label=str(class_), **kwds)
210+
label=com._stringify(class_), **kwds)
211211
ax.legend()
212212

213213
ax.add_patch(patches.Circle((0.0, 0.0), radius=1.0, facecolor='none'))
@@ -272,8 +272,8 @@ def random_color(column):
272272
f = function(row)
273273
y = [f(t) for t in x]
274274
label = None
275-
if str(class_col[i]) not in used_legends:
276-
label = str(class_col[i])
275+
if com._stringify(class_col[i]) not in used_legends:
276+
label = com._stringify(class_col[i])
277277
used_legends.add(label)
278278
ax.plot(x, y, color=random_color(class_col[i]), label=label)
279279
ax.legend(loc='upper right')
@@ -378,8 +378,8 @@ def random_color(column):
378378
y = row
379379
label = None
380380
kls = class_col.iget_value(i)
381-
if str(kls) not in used_legends:
382-
label = str(kls)
381+
if com._stringify(kls) not in used_legends:
382+
label = com._stringify(kls)
383383
used_legends.add(label)
384384
ax.plot(x, y, color=random_color(kls), label=label, **kwds)
385385

@@ -685,10 +685,11 @@ def legend_title(self):
685685
if not isinstance(self.data.columns, MultiIndex):
686686
name = self.data.columns.name
687687
if name is not None:
688-
name = str(name)
688+
name = com._stringify(name)
689689
return name
690690
else:
691-
stringified = map(str, self.data.columns.names)
691+
stringified = map(com._stringify,
692+
self.data.columns.names)
692693
return ','.join(stringified)
693694
else:
694695
return None
@@ -741,13 +742,13 @@ def _get_index_name(self):
741742
if isinstance(self.data.index, MultiIndex):
742743
name = self.data.index.names
743744
if any(x is not None for x in name):
744-
name = ','.join([str(x) for x in name])
745+
name = ','.join([com._stringify(x) for x in name])
745746
else:
746747
name = None
747748
else:
748749
name = self.data.index.name
749750
if name is not None:
750-
name = str(name)
751+
name = com._stringify(name)
751752

752753
return name
753754

@@ -881,7 +882,7 @@ def _make_plot(self):
881882
if re.match('[a-z]+', style) is None:
882883
kwds['color'] = colors[i % len(colors)]
883884

884-
label = com._stringify(label)
885+
label = _stringify(label)
885886

886887
mask = com.isnull(y)
887888
if mask.any():
@@ -1385,9 +1386,9 @@ def plot_group(grouped, ax):
13851386

13861387
def _stringify(x):
13871388
if isinstance(x, tuple):
1388-
return '|'.join(str(y) for y in x)
1389+
return '|'.join(com._stringify(y) for y in x)
13891390
else:
1390-
return str(x)
1391+
return com._stringify(x)
13911392

13921393

13931394
def format_date_labels(ax, rot):
@@ -1426,8 +1427,8 @@ def plot_group(group, ax):
14261427
else:
14271428
fig = ax.get_figure()
14281429
plot_group(data, ax)
1429-
ax.set_ylabel(str(y))
1430-
ax.set_xlabel(str(x))
1430+
ax.set_ylabel(com._stringify(y))
1431+
ax.set_xlabel(com._stringify(x))
14311432

14321433
ax.grid(grid)
14331434

@@ -1640,7 +1641,7 @@ def _grouped_plot(plotf, data, column=None, by=None, numeric_only=True,
16401641
if numeric_only and isinstance(group, DataFrame):
16411642
group = group._get_numeric_data()
16421643
plotf(group, ax)
1643-
ax.set_title(str(key))
1644+
ax.set_title(com._stringify(key))
16441645

16451646
return fig, axes
16461647

@@ -1674,7 +1675,7 @@ def _grouped_plot_by_column(plotf, data, columns=None, by=None,
16741675
gp_col = grouped[col]
16751676
plotf(gp_col, ax)
16761677
ax.set_title(col)
1677-
ax.set_xlabel(str(by))
1678+
ax.set_xlabel(com._stringify(by))
16781679
ax.grid(grid)
16791680

16801681
byline = by[0] if len(by) == 1 else by

0 commit comments

Comments
 (0)