python 中的多个数据直方图

Multiple data histogram in python

我对多数据直方图有疑问。

我有一个数组列表,每个数组包含不同数量的值。我想要一个堆叠直方图,我确实得到了它,但我希望这个值是堆叠的 'one after an other',这意味着直方图底部的所有蓝色,蓝色上方的所有青色等。但我的直方图将它们混合在一起。

import matplotlib.pyplot as plt

bins_1=[25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170]
x=[array([ -17.39386525,   48.368168  ,   79.47025691,   81.56601668,
         89.18470488,   95.41711067,  100.        ,  101.37656251,
        101.66780064]), array([  74.01649726,   76.57421598,   83.82263397,  101.09723897,
        101.35599967,  106.20473323,  125.32481435]), array([  80.30799141,   80.69495682,   90.64266387,  100.59333806,
        110.92342467,  114.03759007]), array([  75.73950779,   82.78756325,   91.12237053,   99.16053347,
        102.75900509,  111.29184111,  112.53084035]), array([  90.76141555,   95.50630177,   98.47127058,  101.53835677,
        118.05901363,  121.97579472,  123.93998025]), array([ 84.83641744,  88.25589465,  88.73029233,  90.67304666,
        92.45594435,  94.53857766]), array([  90.02129774,   97.47537826,   98.1094508 ,  104.90845359,
        123.88128883]), array([  70.45117675,   86.58666341,   88.58116115,   96.40940807,
        108.82477152,  119.2192689 ,  121.29656905]), array([  85.41581466,   89.11517158,   99.48923389,  105.79105149,
        107.17832611,  109.12150105,  117.93740188]), array([  84.9374751 ,   86.59808353,   89.4279953 ,   89.62824778,
         95.06305366,   96.73698641,   97.22749937,   98.11341876,
        100.59178895,  105.20938707]), array([  59.80245368,   82.96922252,   96.88431443,   98.08075879,
         99.1774299 ,  101.28416133,  127.44875806]), array([  97.39841438,  101.87313756,  103.74805137,  110.38644243,
        110.73198861,  125.97443118,  134.71140893,  148.63492245]), array([  88.60244669,   99.29194404,   99.82668617,  105.2816237 ]), array([  91.31362803,   94.91830313,   97.50844764,  111.59560735]), array([  79.45212159,   80.56841471,   90.74986538,   92.88847337,
         94.9665391 ,  100.98072459,  101.80697666,  111.38101874,
        112.75886621]), array([  17.04198007,   97.23402024,  100.54257276,  113.17293093,
        114.26809621,  121.08638797,  123.64111202]), array([], dtype=float64)]
n, bins, patches = plt.hist(x,bins_1,histtype='barstacked')#, stacked=True)#,facecolor='green')
plt.show()

Here is an an example of result

哈哈,问题是 matplotlib 正在重用颜色。所以情节如你所愿,你只需要更多的颜色。这是我对你的代码的修改。

import matplotlib.pyplot as plt
import numpy as np
from numpy import array


bins_1=[25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170]
x=[array([ -17.39386525,   48.368168  ,   79.47025691,   81.56601668,
     89.18470488,   95.41711067,  100.        ,  101.37656251,
    101.66780064]), array([  74.01649726,   76.57421598,   83.82263397,  101.09723897,
    101.35599967,  106.20473323,  125.32481435]), array([  80.30799141,   80.69495682,   90.64266387,  100.59333806,
    110.92342467,  114.03759007]), array([  75.73950779,   82.78756325,   91.12237053,   99.16053347,
    102.75900509,  111.29184111,  112.53084035]), array([  90.76141555,   95.50630177,   98.47127058,  101.53835677,
    118.05901363,  121.97579472,  123.93998025]), array([ 84.83641744,  88.25589465,  88.73029233,  90.67304666,
    92.45594435,  94.53857766]), array([  90.02129774,   97.47537826,   98.1094508 ,  104.90845359,
    123.88128883]), array([  70.45117675,   86.58666341,   88.58116115,   96.40940807,
    108.82477152,  119.2192689 ,  121.29656905]), array([  85.41581466,   89.11517158,   99.48923389,  105.79105149,
    107.17832611,  109.12150105,  117.93740188]), array([  84.9374751 ,   86.59808353,   89.4279953 ,   89.62824778,
     95.06305366,   96.73698641,   97.22749937,   98.11341876,
    100.59178895,  105.20938707]), array([  59.80245368,   82.96922252,   96.88431443,   98.08075879,
     99.1774299 ,  101.28416133,  127.44875806]), array([  97.39841438,  101.87313756,  103.74805137,  110.38644243,
    110.73198861,  125.97443118,  134.71140893,  148.63492245]), array([  88.60244669,   99.29194404,   99.82668617,  105.2816237 ]), array([  91.31362803,   94.91830313,   97.50844764,  111.59560735]), array([  79.45212159,   80.56841471,   90.74986538,   92.88847337,
     94.9665391 ,  100.98072459,  101.80697666,  111.38101874,
    112.75886621]), array([  17.04198007,   97.23402024,  100.54257276,  113.17293093,
    114.26809621,  121.08638797,  123.64111202]),]
n, bins, patches = plt.hist(x,bins_1, histtype='bar', stacked=True, normed=1, color=['b','g','y','purple','pink','crimson','burlywood','chartreuse','magenta','firebrick','cyan','black','darkgoldenrod', 'gray','g','k' ])
plt.show()

注意每种颜色都是指定的。

color=['b','g','y','purple','pink','crimson','burlywood','chartreuse','magenta','firebrick','cyan','black','darkgoldenrod', 'gray','g','k' ]

如果您想验证这是真的发生了什么,请在您自己的代码中设置 cumulative=True,您会看到颜色对齐。只有你不想要累积图,所以指定颜色可能是目前最好的选择。

更新: 使用 mptools(你需要下载)你可以更容易地做到这一点。 要安装,如果 linux 只需键入 sudo pip install mpltools

这是一个可行的例子:

import matplotlib.pyplot as plt
import numpy as np
from numpy import array
from mpltools import layout
from mpltools import color



bins_1=[25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170]
x=[array([ -17.39386525,   48.368168  ,   79.47025691,   81.56601668,
     89.18470488,   95.41711067,  100.        ,  101.37656251,
    101.66780064]), array([  74.01649726,   76.57421598,   83.82263397,  101.09723897,
    101.35599967,  106.20473323,  125.32481435]), array([  80.30799141,   80.69495682,   90.64266387,  100.59333806,
    110.92342467,  114.03759007]), array([  75.73950779,   82.78756325,   91.12237053,   99.16053347,
    102.75900509,  111.29184111,  112.53084035]), array([  90.76141555,   95.50630177,   98.47127058,  101.53835677,
    118.05901363,  121.97579472,  123.93998025]), array([ 84.83641744,  88.25589465,  88.73029233,  90.67304666,
    92.45594435,  94.53857766]), array([  90.02129774,   97.47537826,   98.1094508 ,  104.90845359,
    123.88128883]), array([  70.45117675,   86.58666341,   88.58116115,   96.40940807,
    108.82477152,  119.2192689 ,  121.29656905]), array([  85.41581466,   89.11517158,   99.48923389,  105.79105149,
    107.17832611,  109.12150105,  117.93740188]), array([  84.9374751 ,   86.59808353,   89.4279953 ,   89.62824778,
     95.06305366,   96.73698641,   97.22749937,   98.11341876,
    100.59178895,  105.20938707]), array([  59.80245368,   82.96922252,   96.88431443,   98.08075879,
     99.1774299 ,  101.28416133,  127.44875806]), array([  97.39841438,  101.87313756,  103.74805137,  110.38644243,
    110.73198861,  125.97443118,  134.71140893,  148.63492245]), array([  88.60244669,   99.29194404,   99.82668617,  105.2816237 ]), array([  91.31362803,   94.91830313,   97.50844764,  111.59560735]), array([  79.45212159,   80.56841471,   90.74986538,   92.88847337,
     94.9665391 ,  100.98072459,  101.80697666,  111.38101874,
    112.75886621]), array([  17.04198007,   97.23402024,  100.54257276,  113.17293093,
    114.26809621,  121.08638797,  123.64111202]),]
n_lines = len(x)
color.cycle_cmap(n_lines, cmap='cool')
n, bins, patches = plt.hist(x,bins_1, histtype='bar', stacked=True, normed=1) 
plt.show()

here is a list of cmaps you can choose from and here 是此答案的来源文档。

这是 color.cycle_cmap(n_lines, cmap='Dark2')