title: panda dataframe visualize the percentage of different values of a categorial column within different bins of a numerical column
body: <p>I have a pandas dataframe with two columns <code>col1</code> and <code>class</code>. <code>class</code> is binary. I want to plot a histogram and visualize the percentage of each one of <code>class</code> values on different bins of <code>col1</code> column. Here are my attempts:</p>
  
  <p>1- Two histograms, one for each value of <code>class</code> column:</p>
  
  <pre><code>df.col1[df.class == 0].hist()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/cpRvS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cpRvS.png" alt="enter image description here"></a></p>
  
  <pre><code>df.col1[df.class == 1].hist()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/I37Ro.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I37Ro.png" alt="enter image description here"></a></p>
  
  <p>2- Put them all (two values of class) together in one chart</p>
  
  <pre><code>df.groupby('class').col1.hist(alpha=0.9)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/g6DTX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/g6DTX.png" alt="enter image description here"></a></p>
  
  <p>As you can see from the first two graphs, those rows with <code>class==1</code> are rare comparing to another <code>class==0</code> and when we put them together (third graph), we don't see their effect (look at those tiny orange areas in the chart). One solution is using the percentage of each value of <code>class</code> within each bin. I tried this one:</p>
  
  <pre><code>df.groupby('class').col1.transform(lambda x: x/sum(x)).hist(alpha=0.9)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/4JdMY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4JdMY.png" alt="enter image description here"></a></p>
  
  <p>and apparently didn't work. I'm looking for a way to visualize the percentage of each class value within different bins.</p>