【发布时间】:2023-03-10 00:15:01
【问题描述】:
我正在尝试生成 N 个随机颜色 P 次。例如,我有 10 个不同的地块,每个地块有 20 个条,我想为每个条分配不同的颜色。如何为每个条生成 20 种随机颜色 10 次?
我已经编写了这段代码,但它显示错误。
def generate_n_random_cmap(how_many=20,list_len=10):
'''
Generate list of 20 colors 10 times by default.
NOTE: Colors can be duplicated if lit_len is greater than length of plt.colormap()
'''
import random
import matplotlib.pyplot as plt
c_list = []
while len(c_list)!=list_len:
try:
c_list.append(plt.cm.get_cmap(random.choice(plt.colormaps()),how_many).colors)
except :
None
return random.shuffle(c_list)
我得到的错误是:
'LinearSegmentedColormap' object has no attribute 'colors'
还有其他方法吗?颜色本质上应该是随机的。
【问题讨论】:
标签: python python-3.x matplotlib seaborn