How to plot a stacked histogram with two arrays in python
<p>I am trying to create a stacked histogram showing the clump thickness for malignant and benign tumors, with the malignant class colored red and the benign class colored blue. </p>

<p>I got the clump_thickness_array and benign_or_malignant_array. The benign_or_malignant_array consists of 2s and 4s.</p>

<ol>
<li>If benign_or_malignant equals 2 it is benign(blue colored). </li>
<li>If it equals 4 it is malignant(red colored).</li>
</ol>

<p>I can not figure out how to color the benign and malignant tumors. My Histogram is showing something other than what I try to achieve.</p>

<p>This is my code and my histogram so far:</p>

<pre><code>fig, ax = plt.subplots(figsize=(12,8))
tmp = list()
for i in range(2):
indices = np.where(benign_or_malignant&gt;=i )
tmp.append(clump_thickness[indices])

ax.hist(tmp,bins=10,stacked=True,color = ['b',"r"],alpha=0.73)
</code></pre>

<p><a href="http://i.stack.imgur.com/XHru8.png" rel="nofollow"><img src="http://i.stack.imgur.com/XHru8.png" alt="enter image description here"></a></p>

<p>to obtain a stacked histogram using lists of different length for each group, you need to assemble a list of lists. This is what you are doing with your <code>tmp</code> variable. However, I think you are using the wrong indexes in your for loop. Above, you state that you want to label your data according to the variable <code>benign_or_malignant</code>. You want to test if it is exactly 2 or exactly 4. If you really just want these two possibilities, rewrite like this:</p>

<pre><code>for i in [2,4]:
   indices = np.where(benign_or_malignant==i )
   tmp.append(clump_thickness[indices])
</code></pre>

python matplotlib histogram