def freeze_color_cycle(ax):
"""A function that freezes the color cycle. This is useful for example when
the twinx() command is used and the color cycle would normally be reseted.
Usage:
import matplotlib.pyplot as plt
import numpy as np
plt.close('all')
for i in range(3):
plt.plot(np.random.random(20))
ax=plt.gca()
cc=freeze_color_cycle(ax)
plt.twinx()
ax=plt.gca()
ax.set_color_cycle(cc)
#plot some more on the new twined axes
for i in range(3):
plt.plot(np.random.random(20))
#When we set-up a new figure we start with blue again
fig=figure()
for i in range(3):
plot(np.random.random(20))
"""
import matplotlib as mpl
if mpl.__version__ >= '1.5.1':
next_color=ax._get_lines.prop_cycler.next()['color']
else:
next_color=next(ax._get_lines.color_cycle)
ix=plt.rcParams['axes.color_cycle'].index(next_color)
color_cycle=plt.rcParams['axes.color_cycle'][ix:]+plt.rcParams['axes.color_cycle'][:ix]
return color_cycle
评论列表
文章目录