Python Pyplot historgram具有7个以上的数据集

Python Pyplot historgram具有7个以上的数据集,python,matplotlib,plot,histogram,bar-chart,Python,Matplotlib,Plot,Histogram,Bar Chart,Pyplot允许您创建多个数据集的堆叠直方图(如) 但是,如果直方图中有7个以上的数据集,它会重复颜色 有没有一种方法可以区分7种不同的颜色 我尝试使用可选的hatch参数(),但它只对所有条使用一种图案填充样式,而不是对每个条使用一种图案填充样式 # This applies one hatch-style to all bars plt.hist(data, label=label, normed=True, stacked=True, hatch='/') # This doesn't

Pyplot允许您创建多个数据集的堆叠直方图(如)

但是,如果直方图中有7个以上的数据集,它会重复颜色

有没有一种方法可以区分7种不同的颜色

我尝试使用可选的hatch参数(),但它只对所有条使用一种图案填充样式,而不是对每个条使用一种图案填充样式

# This applies one hatch-style to all bars
plt.hist(data, label=label, normed=True, stacked=True, hatch='/')

# This doesn't apply different hatch styles to different bars.
# It throws an error
plt.hist(data, label=label, normed=True, stacked=True, hatch=
         ['/', '\\', '|', '-', '+', 'x', 'o', 'O', '.', '*', 'oo', 'xx'])

Matplotlib使用具有预定义颜色的颜色循环。您可以根据自己的喜好修改此颜色循环,但如果在调用
hist
时直接指定颜色,则颜色循环会更清晰。手动指定颜色非常繁琐,因此可以使用MatPlotLib的一个颜色贴图来生成颜色。在下面的示例中,我还使用了来自的颜色贴图,因为它们也很不错

import matplotlib.pyplot as plt
import numpy as np
import brewer2mpl

colors_brewer = brewer2mpl.get_map('Paired', 'Qualitative', 12).mpl_colors
colors_jet = plt.cm.jet(np.linspace(0,1,12))

# random data 
data = np.random.rand(100,12)

# plot it
fig, ax = plt.subplots(1,2)
ax[0].hist(data, bins=10, stacked=True, color=colors_brewer)
ax[1].hist(data, bins=10, stacked=True, color=colors_jet)
plt.show()
结果: