Repository: erwanp/publib

Issue:
Error with keep_color()  : axes.get_lines.prop_cycler was made internal in matplotlib 3.8.0 
Same error as here  https://github.com/matplotlib/matplotlib/issues/26831 



Discussion:

Patch:
diff --git a/publib/__version__.txt b/publib/__version__.txt
index 448a0fa..60a2d3e 100644
--- a/publib/__version__.txt
+++ b/publib/__version__.txt
@@ -1,1 +1,1 @@
-0.3.4
\ No newline at end of file
+0.4.0
\ No newline at end of file
diff --git a/publib/tools/colors.py b/publib/tools/colors.py
index b944c9e..4570acb 100644
--- a/publib/tools/colors.py
+++ b/publib/tools/colors.py
@@ -6,7 +6,7 @@
 
 from __future__ import absolute_import, division, print_function, unicode_literals
 
-import matplotlib as mpl
+import matplotlib.pyplot as plt
 
 # Colors available for import
 colors = ['#5DA5DA',
@@ -30,19 +30,30 @@ def keep_color(ax=None):
 
     Note: when setting color= it looks like the color cycle state is not called
 
-    TODO: maybe implement my own cycle structure '''
+    TODO: maybe implement my own cycle structure 
+    
+    .. warning::
+        only works if all colors in a cycle are unique
+    '''
 
     if ax is None:
-        ax = mpl.pyplot.gca()
+        ax = plt.gca()
 
     i = 1  # count number of elements
-    cycle = ax._get_lines.prop_cycler
-    a = next(cycle)     # a is already the next one.
-    while(a != next(cycle)):
+    next_color = ax._get_lines.get_next_color()
+    a = ax._get_lines.get_next_color()     # a is already the next next color.
+    # do a full cycle :
+    while(a != next_color):
         i += 1
+        a = ax._get_lines.get_next_color()
+    print("guessed that cycle has size ", i)
     # We want a-1 to show up on next call to next. So a-2 must be set now
     for j in range(i - 2):
-        next(cycle)
+        ax._get_lines.get_next_color()
+
+    print("Expected color is ", next_color)
+
+    return None
 
 
 def get_next_color(ax=None, nonintrusive=True):
@@ -53,22 +64,25 @@ def get_next_color(ax=None, nonintrusive=True):
 
     If nonintrusive is True, then leave the color cycle in the same state as 
     before    
+
+    .. warning::
+        only works if all colors in a cycle are unique
     '''
 
     if ax is None:
-        ax = mpl.pyplot.gca()
+        ax = plt.gca()
 
     i = 1  # count number of elements
-    cycle = ax._get_lines.prop_cycler  # color_cycle
-    color = None
-    a = next(cycle)     # a is already the next one.
-    while(a != next(cycle)):
+    next_color = ax._get_lines.get_next_color()
+    a = ax._get_lines.get_next_color()     # a is already the next next color.
+    # do a full cycle :
+    while(a != next_color):
         i += 1
-        color = a['color']
+        a = ax._get_lines.get_next_color()
 
     if nonintrusive:
         # We want a-1 to show up on next call to next. So a-2 must be set now
         for j in range(i - 1):
-            next(cycle)
+            ax._get_lines.get_next_color()
 
-    return color
+    return next_color
diff --git a/setup.py b/setup.py
index 138960a..bf64601 100644
--- a/setup.py
+++ b/setup.py
@@ -25,7 +25,7 @@ setup(name='publib',
       packages=find_packages(),
       keywords=["origin", "styles", "matplotlib"],
       install_requires=[
-          'matplotlib>=1.4.1',
+          'matplotlib>=3.8.0',
            #'numpy', # for testing only. Should make this an optional requirement. 
           'six', 
 		  ],


Test patch:
diff --git a/publib/test/test_tools.py b/publib/test/test_tools.py
index ab78e27..adda4c5 100644
--- a/publib/test/test_tools.py
+++ b/publib/test/test_tools.py
@@ -18,12 +18,12 @@ def test_keep_color(*args, **kwargs):
     plt.figure()
     plt.plot(0,1,'o')
     
-    current_color = get_next_color()
+    current_color = get_next_color(nonintrusive=True)
+    keep_color()
     
     plt.plot(1,1,'o')
     
-    keep_color()
-    assert get_next_color() == current_color
+    assert get_next_color(nonintrusive=True) == current_color