python - how to access custom prop_cycle colors? -
i've defined custom color cycler in text file, , invoking via plt.style.context()
. cycle i've defined has 12 colors. when annotating plot, if try access custom cycler colors using color strings, 'c0'
through 'c9'
work 'c10'
throws valueerror: invalid rgba argument: 'c10'
. 12 values in there. contents of style file style.yaml
:
axes.prop_cycle : cycler('color', ['332288', 'cc6677', 'ddcc77', '117733', '88ccee', 'aa4499', '44aa99', '999933', '882255', '661100', '6699cc', 'aa4466'])
test script:
import numpy np import matplotlib.pyplot plt data = np.c_[np.zeros(13), np.arange(13)].t labels = list('abcdefghijklm') plt.style.context('style.yaml'): # prove colors loading correctly: cycle = plt.rcparams['axes.prop_cycle'].by_key()['color'] print('{} colors: {}'.format(len(cycle), cycle)) # actual plotting: fig, ax = plt.subplots() ax.plot(data) ix in range(data.shape[1]): ax.text(x=1, y=data[1, ix], s=labels[ix], color='c{}'.format(ix))
the result (notwithstanding valueerror
stops plotting after 10 labels) lines follow custom cycler colors, text labels use matplotlib default cycler colors (see screenshot). how can text appear using custom cycler colors defined in external yaml file?
first, can access first 10 colors of colorcycle via "cn"
-notation. matplotlib states it:
to access these colors outside of property cycling notation colors 'cn', n takes values 0-9, added denote first 10 colors in mpl.rcparams['axes.prop_cycle']
second, values of cn
not updated within style context , hence cannot used such cases.
however, can of course use colors cycler directly:
(note i'm ignoring yaml part of question here.)
import numpy np import matplotlib.pyplot plt 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) ix in range(data.shape[1]): ax.text(x=1, y=data[1, ix], s=labels[ix], color=cycle[ix%len(cycle)]) plt.show()
a different method may cycle through cycler next
:
import numpy np import matplotlib.pyplot plt 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) ix in range(data.shape[1]): ax.text(x=1, y=data[1, ix], s=labels[ix], color=next(cycler)['color']) plt.show()
both codes produce following plot:
Comments
Post a Comment