【发布时间】: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