我认为目前没有公共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()
![enter image description here](https://istack.dev59.com/REiCS.webp)
c = np.take(colors, c, mode='wrap')
比循环/切片组合更简单。 - perimosocordiae