user5909208
user5909208

Reputation: 95

How to plot a stacked histogram with two arrays in python

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.

I got the clump_thickness_array and benign_or_malignant_array. The benign_or_malignant_array consists of 2s and 4s.

  1. If benign_or_malignant equals 2 it is benign(blue colored).
  2. If it equals 4 it is malignant(red colored).

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.

This is my code and my histogram so far:

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

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

enter image description here

Upvotes: 1

Views: 2323

Answers (1)

jlarsch
jlarsch

Reputation: 2307

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 tmp 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 benign_or_malignant. You want to test if it is exactly 2 or exactly 4. If you really just want these two possibilities, rewrite like this:

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

Upvotes: 1

Related Questions