Answers.Body: <p>One approach is <code>density</code>, traditionally the code would be ...</p>
  <pre><code>kwargs = dict(histtype='stepfilled', alpha=0.4, normed=True, bins=50)
  
  plt.hist(d1, **kwargs)
  plt.hist(d2, **kwargs)
  plt.hist(d3, **kwargs);
  </code></pre>
  <p><code>alpha</code> is transparency allowing overlapping distributions.</p>
  <p><strong>Note</strong> <code>normed</code> is deprecated, the blurb <a href="https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html" rel="nofollow noreferrer">here</a> describes <code>density</code> now replacing it.</p>
  <p>The blurb is here:</p>
  <blockquote>
  <p>matplotlib.pyplot.hist(x, bins=None, range=None, density=False,
  weights=None, ... **kwargs)</p>
  </blockquote>
  <p>The definition of <code>density</code> is here</p>
  <blockquote>
  <p>densitybool, default: False If True, draw and return a probability
  density: each bin will display the bin's raw count divided by the
  total number of counts and the bin width (density = counts /
  (sum(counts) * np.diff(bins))), so that the area under the histogram
  integrates to 1 (np.sum(density * np.diff(bins)) == 1). If stacked is
  also True, the sum of the histograms is normalized to 1.</p>
  </blockquote>
  <p>A different approach is relative frequency, via the <code>weights</code> command.</p>
  <pre><code> plt.hist(mydata, weights= *)
  </code></pre>
  <p>*, relative counts per data type / total counts over all data types</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-25T18:35:30.207Z
Answers.LastActivityDate: 2022-06-25T18:35:30.207Z
Answers.UserDisplayName: M__
Body: <p>I am trying to create a histogram plot with <code>matplotlib</code>. I am using <code>bmi</code> column from four different data frames (d1, d2, d2, d4).</p>
  <p>The issue is that these 4 datasets have different sample sizes. So there is a disparity between the heights of the bar.</p>
  <p>Is there any additional option in the matplotlib to normalize the heights of the bar?</p>
  <pre><code>import matplotlib.pyplot as plt
  
  def bmiplot(graphWidth, graphHeight):
      f = plt.figure(figsize=(graphWidth/110.0, graphHeight/150.0), dpi=100)
      axes = f.add_subplot(111)
  
      d1 = dm_group1
      d2 = dm_group2
      d3 = dm_group3
      d4 = dm_group4
  
      plt.hist([d1.bmi.values.flatten(), d2.bmi.values.flatten(), d3.bmi.values.flatten(), d4.bmi.values.flatten()], 
      label=['bmi_dm1', 'bmi_dm2', 'bmi_dm3', 'bmi_dm4'])
      plt.legend()
      plt.rc('xtick')
      plt.rc('ytick')
      
  
  graphWidth = 700
  graphHeight = 800
  bmiplot(graphWidth, graphHeight)
  
  <span class="math-container">```</span>
  </code></pre>
  
Tags: <python><graphs>
Title: How to normalise the histogram height in matplotlib
ThreadText: # How to normalise the histogram height in matplotlib
  
  (Asked by: username_0 on 2022-06-25)
  
  I am trying to create a histogram plot with `matplotlib`. I am using `bmi` column from four different data frames (d1, d2, d2, d4).
  
  The issue is that these 4 datasets have different sample sizes. So there is a disparity between the heights of the bar.
  
  Is there any additional option in the matplotlib to normalize the heights of the bar?
  
  ```
  import matplotlib.pyplot as plt
  
  def bmiplot(graphWidth, graphHeight):
      f = plt.figure(figsize=(graphWidth/110.0, graphHeight/150.0), dpi=100)
      axes = f.add_subplot(111)
  
      d1 = dm_group1
      d2 = dm_group2
      d3 = dm_group3
      d4 = dm_group4
  
      plt.hist([d1.bmi.values.flatten(), d2.bmi.values.flatten(), d3.bmi.values.flatten(), d4.bmi.values.flatten()], 
      label=['bmi_dm1', 'bmi_dm2', 'bmi_dm3', 'bmi_dm4'])
      plt.legend()
      plt.rc('xtick')
      plt.rc('ytick')
      
  
  graphWidth = 700
  graphHeight = 800
  bmiplot(graphWidth, graphHeight)
  
  ```
  
  ```
  
  **Tags:** python, graphs
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-06-25. Score: 1 | Accepted answer
  
  One approach is `density`, traditionally the code would be ...
  
  ```
  kwargs = dict(histtype='stepfilled', alpha=0.4, normed=True, bins=50)
  
  plt.hist(d1, **kwargs)
  plt.hist(d2, **kwargs)
  plt.hist(d3, **kwargs);
  
  ```
  
  `alpha` is transparency allowing overlapping distributions.
  
  **Note** `normed` is deprecated, the blurb [here](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html) describes `density` now replacing it.
  
  The blurb is here:
  
  > matplotlib.pyplot.hist(x, bins=None, range=None, density=False,
  > weights=None, ... \*\*kwargs)
  
  The definition of `density` is here
  
  > densitybool, default: False If True, draw and return a probability
  > density: each bin will display the bin's raw count divided by the
  > total number of counts and the bin width (density = counts /
  > (sum(counts) \* np.diff(bins))), so that the area under the histogram
  > integrates to 1 (np.sum(density \* np.diff(bins)) == 1). If stacked is
  > also True, the sum of the histograms is normalized to 1.
  
  A different approach is relative frequency, via the `weights` command.
  
  ```
   plt.hist(mydata, weights= *)
  
  ```
  
  \*, relative counts per data type / total counts over all data types
  
  
  ---
  
  