我遇到了多重数据直方图的问题。
我有一个数组列表,每个数组包含不同数量的值。我想要一个堆叠的直方图,我确实得到了它,但我希望这个值能够一个接一个地堆叠,意味着直方图底部的所有蓝色,所有的青色都在蓝色的但我的直方图将它们混合在一起。
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()
答案 0 :(得分:0)
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
,你会看到颜色排成一行。只有你不想要一个累积图表,所以指定颜色可能是目前最好的选择。
更新:
使用mpltools(您需要下载),您可以更容易地做到这一点。
要安装,如果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是您可以选择的cmaps列表,here是此答案来源的文档。
这是color.cycle_cmap(n_lines, cmap='Dark2')