python - matplotlib colororder and twinx -
i adjusted colororder plot rcparams['axes.color_cycle'] = [some nice , chosen colours]
but when utilize twinx sec axes colororder reset:
matplotlib import pyplot plt matplotlib import rcparams rcparams['axes.color_cycle'] = ['r','g','k'] fig = plt.figure() ax = fig.add_subplot(1,1,1) ax1 = plt.twinx(ax) ax.plot([1,2,3],[4,5,6]) ax.plot([1,2,3],[7,6,4]) ax1.plot([1,2,3],[5,3,1])
is there way circumvent that? line plotted on ax1
should black.
here's way advance color-order sec axis.
from matplotlib import pyplot plt matplotlib import rcparams rcparams['axes.color_cycle'] = ['r','g','k'] fig = plt.figure() ax = fig.add_subplot(1,1,1) ax1 = plt.twinx(ax) line = [] line.append(ax.plot([1,2,3],[4,5,6])[0]) line.append(ax.plot([1,2,3],[7,6,4])[0]) advance in range(len(line)): ax1._get_lines.color_cycle.next() ax1.plot([1,2,3],[5,3,1])
if run example, line on sec axis black.
what happens this:
inline
maintain track of numer of lines have plotted on first axis. note, need append first entry of matplotlib.lines.line2d object
, hence [0]
@ end of ax.plot([1,2,3],[4,5,6])
the for
-loop may bit clumsy. however, advances color-cycle of sec axis number of lines have on first axis. done through calling next()
on ax1._get_lines.color_cycle
you don't acutally need loop counter advance
, however, makes code more readable. now, lines on sec axis go on next color in cycle after 1 left off on first axis. if wanted go first axis, you'd have count number of lines on ax1
, too. an interesting question read in context is: get matplotlib color cycle state
python matplotlib
No comments:
Post a Comment