1

Answers to Get default line colour cycle are quite helpful and indicate there's been a change at around version 1.5.

I'm wondering if there is a simple way to "pause", "resume" and "reset" the way that Matplotlib cycles through default colors without implementing it manually.

Example below is not a good way to do it manually, but just illustrates how hypothetical colorpause(), colorresume(), and colorreset() might be used.

colorpause, colorresume, colorreset illustration

def colorpause():
    global increment_me
    increment_me = False

def colorresume():
    global increment_me
    increment_me = True

def colorreset():
    global icolor
    icolor   = 0

import matplotlib.pyplot as plt

xx = [[0 + 0.1*d, 1 + 0.1*d] for d in range(20)]
y  = [1, 0]

if True:
    icolorz = []
    plt.figure()
    colorz       = plt.rcParams['axes.prop_cycle'].by_key()['color']
    increment_me = True
    icolor       = 0
    plt.subplot(2, 1, 1)
    for i, x in enumerate(xx):
        plt.plot(x, y, colorz[icolor], linewidth=2)
        icolorz.append(icolor)
        icolor += increment_me
        icolor = icolor%len(colorz)
        if i == 5:
            colorpause()
        if i == 10:
            colorresume()
        if i >=12 and not i%3:
            colorreset()
    plt.subplot(2, 1, 2)
    plt.plot(icolorz)
    plt.ylim(-0.5, 6.5)
    plt.title('color number used')
    plt.show()
uhoh
  • 3,713
  • 6
  • 42
  • 95
  • 1
    The short answer is "no". – ImportanceOfBeingErnest Oct 08 '18 at 15:19
  • @ImportanceOfBeingErnest I had a hunch that was so. I'd done some further poking around but couldn't find anything. Thanks for the help! – uhoh Oct 08 '18 at 15:20
  • @ImportanceOfBeingErnest I'm not a good source for matplotlib information so a "no" answer from me would not be well-supported. If you would be willing to repeat your short but helpful answer as an answer, I can accept. Thanks! – uhoh Nov 15 '18 at 07:34
  • 1
    I would see it this way: The answer to the question with the appendix "without implementing it manually" is "no". But of course the next step would be to leave that restriction out. So why not leave this question unanswered, and maybe one day someone will answer it with a cool way of subclassing `cycler` to implement some of the desired functionality? – ImportanceOfBeingErnest Nov 15 '18 at 13:28
  • @ImportanceOfBeingErnest okay, that sounds like a very reasonable approach. – uhoh Nov 15 '18 at 13:57
  • 1
    @uhoh have you implemented it manually, by chance? I need the same thing – riddleculous Jan 10 '20 at 15:06
  • @riddleculous no I'm sorry I haven't. I don't think it would be very hard though. But these days there are several new sets of color available, including an xkcd version, so I think something so interesting should be written for any set of colors. I think it would make an excellent new question and would probably receive several answers! – uhoh Jan 11 '20 at 05:54

0 Answers0