Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b97396

Browse files
committed
Merge pull request #6158 from has2k1/fix-pandas-iterables
Fix: pandas series of strings
2 parents 196f344 + 6ef3583 commit 2b97396

File tree

6 files changed

+42
-2
lines changed

6 files changed

+42
-2
lines changed

lib/matplotlib/cbook.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,17 @@ def is_sequence_of_strings(obj):
715715
return True
716716

717717

718+
def is_hashable(obj):
719+
"""
720+
Returns true if *obj* can be hashed
721+
"""
722+
try:
723+
hash(obj)
724+
except TypeError:
725+
return False
726+
return True
727+
728+
718729
def is_writable_file_like(obj):
719730
'return true if *obj* looks like a file object with a *write* method'
720731
return hasattr(obj, 'write') and six.callable(obj.write)

lib/matplotlib/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def set_linestyle(self, ls):
532532
The line style.
533533
"""
534534
try:
535-
if cbook.is_string_like(ls):
535+
if cbook.is_string_like(ls) and cbook.is_hashable(ls):
536536
ls = cbook.ls_mapper.get(ls, ls)
537537
dashes = [mlines.get_dash_pattern(ls)]
538538
elif cbook.iterable(ls):

lib/matplotlib/colors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,8 @@ def __init__(self, colors, name='from_list', N=None):
821821
if N is None:
822822
N = len(self.colors)
823823
else:
824-
if cbook.is_string_like(self.colors):
824+
if (cbook.is_string_like(self.colors) and
825+
cbook.is_hashable(self.colors)):
825826
self.colors = [self.colors] * N
826827
self.monochrome = True
827828
elif cbook.iterable(self.colors):

lib/matplotlib/tests/test_cbook.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ def test_is_sequence_of_strings():
4848
assert cbook.is_sequence_of_strings(y)
4949

5050

51+
def test_is_hashable():
52+
s = 'string'
53+
assert cbook.is_hashable(s)
54+
55+
lst = ['list', 'of', 'stings']
56+
assert not cbook.is_hashable(lst)
57+
58+
5159
def test_restrict_dict():
5260
d = {'foo': 'bar', 1: 2}
5361
d1 = cbook.restrict_dict(d, ['foo', 1])

lib/matplotlib/tests/test_collections.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,11 +629,13 @@ def test_pandas_indexing():
629629
index = [11, 12, 13]
630630
ec = fc = pd.Series(['red', 'blue', 'green'], index=index)
631631
lw = pd.Series([1, 2, 3], index=index)
632+
ls = pd.Series(['solid', 'dashed', 'dashdot'], index=index)
632633
aa = pd.Series([True, False, True], index=index)
633634

634635
Collection(edgecolors=ec)
635636
Collection(facecolors=fc)
636637
Collection(linewidths=lw)
638+
Collection(linestyles=ls)
637639
Collection(antialiaseds=aa)
638640

639641

lib/matplotlib/tests/test_colors.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from distutils.version import LooseVersion as V
77

88
from nose.tools import assert_raises, assert_equal, assert_true
9+
from nose.tools import assert_sequence_equal
910

1011
import numpy as np
1112
from numpy.testing.utils import assert_array_equal, assert_array_almost_equal
13+
from nose.plugins.skip import SkipTest
1214

1315
import matplotlib.colors as mcolors
1416
import matplotlib.cm as cm
@@ -563,6 +565,22 @@ def _azimuth2math(azimuth, elevation):
563565
return theta, phi
564566

565567

568+
def test_pandas_iterable():
569+
try:
570+
import pandas as pd
571+
except ImportError:
572+
raise SkipTest("Pandas not installed")
573+
574+
# Using a list or series yields equivalent
575+
# color maps, i.e the series isn't seen as
576+
# a single color
577+
lst = ['red', 'blue', 'green']
578+
s = pd.Series(lst)
579+
cm1 = mcolors.ListedColormap(lst, N=5)
580+
cm2 = mcolors.ListedColormap(s, N=5)
581+
assert_sequence_equal(cm1.colors, cm2.colors)
582+
583+
566584
if __name__ == '__main__':
567585
import nose
568586
nose.runmodule(argv=['-s', '--with-doctest'], exit=False)

0 commit comments

Comments
 (0)