代码之家  ›  专栏  ›  技术社区  ›  uhoh Jacob

除了手动之外,在Matplotlib中“暂停”、“恢复”和“重置”默认颜色循环的方法?

  •  0
  • uhoh Jacob  · 技术社区  · 6 年前

    Get default line colour cycle 非常有帮助,并且表明在1.5版左右有一个更改。

    不需要手动执行 .

    下面的例子并不是一个手工操作的好方法,但只是说明了如何假设 colorpause() , colorresume() colorreset()

    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()
    
    0 回复  |  直到 6 年前