Pyplot允许您创建多个数据集的堆叠直方图(如this one)。
但是,如果直方图中有超过7个数据集,则会重复颜色。
有没有办法区分7种不同的颜色?
我尝试使用可选的填充参数(documented here),但它只需要一个填充样式用于所有条形,而不是每个条形图的一个填充样式。
# 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'])
答案 0 :(得分:4)
Matplotlib使用具有预定义颜色的颜色循环。您可以根据自己的喜好修改此颜色循环,但如果直接指定hist
调用中的颜色,则会更清晰。手动指定颜色很繁琐,因此您可以使用matplotlibs颜色图之一来生成它们。在下面的示例中,我还使用了colorbrewer的色彩映射,因为它们也非常好。
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()
结果: