Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c78ca18

Browse filesBrowse files
committed
Merge pull request #6158 from has2k1/fix-pandas-iterables
Fix: pandas series of strings Conflicts: lib/matplotlib/collections.py Manually merged changes
1 parent 7266b4f commit c78ca18
Copy full SHA for c78ca18

File tree

Expand file treeCollapse file tree

6 files changed

+42
-2
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+42
-2
lines changed

‎lib/matplotlib/cbook.py

Copy file name to clipboardExpand all lines: lib/matplotlib/cbook.py
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,17 @@ def is_sequence_of_strings(obj):
728728
return True
729729

730730

731+
def is_hashable(obj):
732+
"""
733+
Returns true if *obj* can be hashed
734+
"""
735+
try:
736+
hash(obj)
737+
except TypeError:
738+
return False
739+
return True
740+
741+
731742
def is_writable_file_like(obj):
732743
'return true if *obj* looks like a file object with a *write* method'
733744
return hasattr(obj, 'write') and six.callable(obj.write)

‎lib/matplotlib/collections.py

Copy file name to clipboardExpand all lines: lib/matplotlib/collections.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def set_linestyle(self, ls):
520520
"""
521521
try:
522522
dashd = backend_bases.GraphicsContextBase.dashd
523-
if cbook.is_string_like(ls):
523+
if cbook.is_string_like(ls) and cbook.is_hashable(ls):
524524
ls = cbook.ls_mapper.get(ls, ls)
525525
if ls in dashd:
526526
dashes = [dashd[ls]]

‎lib/matplotlib/colors.py

Copy file name to clipboardExpand all lines: lib/matplotlib/colors.py
+2-1Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_cbook.py
+8Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_collections.py
+2Lines 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

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_colors.py
+18Lines 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
@@ -558,6 +560,22 @@ def _azimuth2math(azimuth, elevation):
558560
return theta, phi
559561

560562

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.