我不認爲存在獲取當前顏色週期一個公共API,而是通過模仿set_prop_cycle
可以定義get_prop_cycle
這樣:
rcParams = plt.matplotlib.rcParams
def get_prop_cycle():
prop_cycler = rcParams['axes.prop_cycle']
if prop_cycler is None and 'axes.color_cycle' in rcParams:
clist = rcParams['axes.color_cycle']
prop_cycler = cycler('color', clist)
return prop_cycler
一旦你的顏色prop_cycler
,您可以在顏色週期圖c
將顏色:
colors = [item['color'] for item in get_prop_cycle()]
c = np.take(colors, c, mode='wrap')
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.rcsetup import cycler
np.random.seed(2016)
rcParams = plt.matplotlib.rcParams
def get_prop_cycle():
prop_cycler = rcParams['axes.prop_cycle']
if prop_cycler is None and 'axes.color_cycle' in rcParams:
clist = rcParams['axes.color_cycle']
prop_cycler = cycler('color', clist)
return prop_cycler
fig, ax = plt.subplots(nrows=2)
x,y = np.random.normal(size=(2,250))
theta = np.arctan2(y,x)
c = np.digitize(theta, np.histogram(theta)[1])
ax[0].scatter(x,y,c=c)
ax[0].set_title('using default colormap')
colors = [item['color'] for item in get_prop_cycle()]
c = np.take(colors, c, mode='wrap')
ax[1].scatter(x,y,c=c)
ax[1].set_title('using color cycle')
plt.show()
謝謝,這正是我所需要的!我發現使用'c = np.take(colors,c,mode ='wrap')',而不是循環/ islice組合更簡單一些。 – perimosocordiae
@perimosocordiae:感謝您的巨大改進。 – unutbu