【问题标题】:how to access custom prop_cycle colors? [duplicate]如何访问自定义 prop_cycle 颜色? [复制]
【发布时间】:2017-09-12 05:39:34
【问题描述】:

我在一个文本文件中定义了一个自定义颜色循环器,并通过plt.style.context() 调用它。我定义的循环有 12 种颜色。在注释绘图时,如果我尝试使用颜色字符串访问我的自定义循环器颜色,'C0''C9' 可以工作,但'C10' 会抛出ValueError: Invalid RGBA argument: 'C10'。所有 12 个值肯定都在其中。样式文件内容style.yaml

axes.prop_cycle : cycler('color', ['332288', 'CC6677', 'DDCC77', '117733', '88CCEE', 'AA4499', '44AA99', '999933', '882255', '661100', '6699CC', 'AA4466'])

测试脚本:

import numpy as np
import matplotlib.pyplot as plt
data = np.c_[np.zeros(13), np.arange(13)].T
labels = list('abcdefghijklm')

with plt.style.context('style.yaml'):
    # just to prove that the colors are loading correctly:
    cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
    print('{} colors: {}'.format(len(cycle), cycle))
    # now the actual plotting:
    fig, ax = plt.subplots()
    ax.plot(data)
    for ix in range(data.shape[1]):
        ax.text(x=1, y=data[1, ix], s=labels[ix], color='C{}'.format(ix))

结果(尽管ValueError 在 10 个标签后停止绘图)是线条遵循我的自定义循环器颜色,但文本标签使用 matplotlib 默认循环器颜色(见屏幕截图)。如何使用外部 YAML 文件中定义的自定义循环器颜色显示文本?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    首先,您只能通过"CN"-notation 访问颜色循环的前 10 种颜色。作为matplotlibstates it

    为了在属性循环之外访问这些颜色,添加了颜色“CN”的符号,其中 N 取值 0-9,以表示 mpl.rcParams['axes.prop_cycle'] 中的前 10 种颜色

    其次,CN 的值不会在样式上下文中更新,因此不能用于这种情况。

    不过,您当然可以直接使用循环仪中的颜色:
    (请注意,我在这里忽略了问题的 yaml 部分。)

    import numpy as np
    import matplotlib.pyplot as plt
    from cycler import cycler
    data = np.c_[np.zeros(13), np.arange(13)].T
    labels = list('abcdefghijklm')
    
    plt.rcParams["axes.prop_cycle"] = cycler('color', 
                        ['#332288', '#CC6677', '#DDCC77', '#117733', '#88CCEE', '#AA4499', 
                         '#44AA99', '#999933', '#882255', '#661100', '#6699CC', '#AA4466'])
    
    cycle = plt.rcParams['axes.prop_cycle'].by_key()['color']
    
    fig, ax = plt.subplots()
    ax.plot(data)
    for ix in range(data.shape[1]):
        ax.text(x=1, y=data[1, ix], s=labels[ix], color=cycle[ix%len(cycle)])
    
    plt.show()
    

    另一种方法可能是使用next 循环通过循环器:

    import numpy as np
    import matplotlib.pyplot as plt
    from cycler import cycler
    
    data = np.c_[np.zeros(13), np.arange(13)].T
    labels = list('abcdefghijklm')
    
    plt.rcParams["axes.prop_cycle"] = cycler('color', 
                        ['#332288', '#CC6677', '#DDCC77', '#117733', '#88CCEE', '#AA4499', 
                         '#44AA99', '#999933', '#882255', '#661100', '#6699CC', '#AA4466'])
    
    
    fig, ax = plt.subplots()
    ax.plot(data)
    
    ax.set_prop_cycle(None)
    cycler = ax._get_lines.prop_cycler
    plt.gca().set_prop_cycle(None)
    for ix in range(data.shape[1]):
        ax.text(x=1, y=data[1, ix], s=labels[ix], color=next(cycler)['color']) 
    
    plt.show()
    

    这两个代码都产生了以下情节:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2014-02-26
    • 2011-12-06
    相关资源
    最近更新 更多