所以你的要求是“很多颜色”和“打印时没有两种颜色应该映射到相同的灰度值”,对吧?任何“顺序”颜色图(亮度单调增加或减少)都应满足第二个标准。我认为在 matplotlib 中的所有选择中,您只剩下 cubehelix(已经提到过)、gnuplot 和 gnuplot2:
白线是每种颜色的亮度,因此可以看到每种颜色在打印时会映射到不同的灰度值。黑线是色调,表示它们在各种颜色之间循环。
注意,cubehelix 实际上是一个函数 (from matplotlib._cm import cubehelix),您可以调整 helix 的参数以产生更广泛的颜色,as shown here。换句话说,cubehelix 不是颜色图,它是一组颜色图。这里有 2 个变体:
对于变化较少的颜色(对许多事情来说更令人愉悦,但可能不适用于您的条形图),不妨试试 ColorBrewer 3 色地图,YlOrRd、PuBuGn、YlGnBu:
https://www.flickr.com/photos/omegatron/7298887952/
不过,我不建议只使用这种颜色来识别条形图。您应该始终使用文本标签作为主要标识符。另请注意,其中一些会生成与背景完全融合的白条,因为它们用于热图,而不是图表颜色:
from matplotlib import pyplot as plt
import pandas, numpy as np # I find np.random.randint to be better
# Make the data
x = [{i:np.random.randint(1,5)} for i in range(10)]
df = pandas.DataFrame(x)
# Make a list by cycling through the colors you care about
# to match the length of your data.
cmap = plt.get_cmap('cubehelix')
indices = np.linspace(0, cmap.N, len(x))
my_colors = [cmap(int(i)) for i in indices]
# Specify this list of colors as the `color` option to `plot`.
df.plot(kind='bar', stacked=True, color=my_colors)
这些是新人: